blob: 55ad1890779d8487e689f7e98bc62ff6f7150c36 [file] [log] [blame]
Derek Jonesb8354c12010-03-05 12:47:30 -06001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
Derek Jonesa898fb52011-01-28 14:09:14 -06005<title>CodeIgniter User Guide : JavaScript Class</title>
Derek Jonesb8354c12010-03-05 12:47:30 -06006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../nav/nav.js"></script>
11<script type="text/javascript" src="../nav/prototype.lite.js"></script>
12<script type="text/javascript" src="../nav/moo.fx.js"></script>
13<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
14
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
Greg Akerc1ce1ce2010-11-09 14:27:06 -060027<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
Derek Jonesb8354c12010-03-05 12:47:30 -060028<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
31<td><h1>CodeIgniter User Guide Version 2.0.0</h1></td>
32<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
43<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Derek Jones42526cf2010-04-28 08:22:44 -050045Input Class
Derek Jonesb8354c12010-03-05 12:47:30 -060046</td>
47<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
48</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
Pascal Kriete3b405bf2011-01-28 14:30:50 -050058<p class="important"><strong>Note:</strong> This driver is experimental. Its feature set and implementation may change in future releases.</p><br>
Derek Jonesb8354c12010-03-05 12:47:30 -060059
60<h1>Javascript Class</h1>
Derek Jonesa898fb52011-01-28 14:09:14 -060061<p>CodeIgniter provides a library to help you with certain common functions that you may want to use with Javascript. Please note that CodeIgniter does not require the jQuery library to run, and that any scripting library will work equally well. The jQuery library is simply presented as a convenience if you choose to use it.</p>
Derek Jonesb8354c12010-03-05 12:47:30 -060062<h2>Initializing the Class</h2>
Derek Jonesa898fb52011-01-28 14:09:14 -060063<p>To initialize the Javascript class manually in your controller constructor, use the <dfn>$this-&gt;load-&gt;library</dfn> function. Currently, the only available library is jQuery, which will automatically be loaded like this:</p>
Derek Jonesb8354c12010-03-05 12:47:30 -060064
65<code>$this-&gt;load-&gt;library('javascript');</code>
66
67<p>The Javascript class also accepts parameters, <dfn>js_library_driver (string) default 'jquery'</dfn> and <dfn>autoload (bool) default TRUE</dfn>. You may override the defaults if you wish by sending an associative array:</p>
68
69<code>$this-&gt;load-&gt;library('javascript', array('js_library_driver' =&gt; 'scripto', 'autoload' =&gt; FALSE));</code>
70
71<p>Again, presently only 'jquery' is available. You may wish to set <dfn>autoload</dfn> to FALSE, though, if you do not want the jQuery library to automatically include a script tag for the main jQuery script file. This is useful if you are loading it from a location outside of CodeIgniter, or already have the script tag in your markup.</p>
72
73<p>Once loaded, the jQuery library object will be available using: <dfn>$this-&gt;javascript</dfn></p>
74<h2>Setup and Configuration</h2>
75<h3>Set these variables in your view</h3>
Derek Jonesa898fb52011-01-28 14:09:14 -060076<p>As a Javascript library, your files must be available to your application.</p>
77<p>As Javascript is a client side language, the library must be able to write content into your final output. This generally means a view. You'll need to include the following variables in the &lt;head&gt; sections of your output.</p>
Derek Jonesb8354c12010-03-05 12:47:30 -060078<p><code>&lt;?php echo $library_src;?&gt;<br />
79&lt;?php echo $script_head;?&gt;
80</code></p>
81<p>$library_src, is where the actual library file will be loaded, as well as any subsequent plugin script calls; $script_head is where specific events, functions and other commands will be rendered.</p>
82<h3>Set the path to the librarys with config items</h3>
Derek Jonesa898fb52011-01-28 14:09:14 -060083<p>There are some configuration items in Javascript library. These can either be set in application/config.php, within its own config/javascript.php file, or within any controller usings the set_item() function. </p>
Derek Jonesb8354c12010-03-05 12:47:30 -060084<p>An image to be used as an &quot;ajax loader&quot;, or progress indicator. Without one, the simple text message of &quot;loading&quot; will appear when Ajax calls need to be made.</p>
85<p><code>$config['javascript_location'] = 'http://localhost/codeigniter/themes/js/jquery/');<br />
86 $config['javascript_ajax_img'] = 'images/ajax-loader.gif';</code></p>
87<p>If you keep your files in the same directories they were downloaded from, then you need not set this configuration items.</p>
Derek Jones1cf11492010-03-11 09:39:11 -060088
Derek Jonesa898fb52011-01-28 14:09:14 -060089<h2>The jQuery Class</h2>
90
91<p>To initialize the jQuery class manually in your controller constructor, use the <dfn>$this-&gt;load-&gt;library</dfn> function:</p>
92
93<code>$this-&gt;load-&gt;library('jquery');</code>
94
95<p>You may send an optional parameter to determine whether or not a script tag for the main jQuery file will be automatically included when loading the library. It will be created by default. To prevent this, load the library as follows:</p>
96
97<code>$this-&gt;load-&gt;library('jquery', FALSE);</code>
98
99<p>Once loaded, the jQuery library object will be available using: <dfn>$this-&gt;jquery</dfn></p>
100
101<h2>jQuery Events</h2>
102
103<p>Events are set using the following syntax.</p>
104
105<p><code>$this-&gt;jquery-&gt;event('element_path', code_to_run());</code></p>
106
107<p>In the above example:</p>
108
109<ul>
110 <li>&quot;event&quot; is any of blur, change, click, dblclick, error, focus, hover, keydown, keyup, load, mousedown, mouseup, mouseover, mouseup, resize, scroll, or unload.</li>
111 <li>&quot;element_path&quot; is any valid <a href="http://docs.jquery.com/Selectors">jQuery selector</a>. Due to jQuery's unique selector syntax, this is usually an element id, or CSS selector. For example &quot;#notice_area&quot; would effect &lt;div id=&quot;notice_area&quot;&gt;, and &quot;#content a.notice&quot; would effect all anchors with a class of &quot;notice&quot; in the div with id &quot;content&quot;.</li>
112 <li>&quot;code_to_run()&quot; is script your write yourself, or an action such as an effect from the jQuery library below.</li>
113 </ul>
114
115<h2>Effects</h2>
116
117<p>The query library supports a powerful <a href="http://docs.jquery.com/Effects">Effects</a> repertoire. Before an effect can be used, it must be loaded:</p>
118
119<p><code>$this->jquery->effect([optional path] plugin name);
120// for example
121$this->jquery->effect('bounce');
122</code></p>
123
124<h3>hide() / show()</h3>
125
126<p>Each of this functions will affect the visibility of an item on your page. hide() will set an item invisible, show() will reveal it.</p>
127<p><code>$this-&gt;jquery-&gt;hide(target, optional speed, optional extra information);<br />
128 $this-&gt;jquery-&gt;show(target, optional speed, optional extra information);</code></p>
129
130<ul>
131 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
132 <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
133 <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
134</ul>
135
136<h3>toggle()</h3>
137
138<p>toggle() will change the visibility of an item to the opposite of its current state, hiding visible elements, and revealing hidden ones.</p>
139<p><code>$this-&gt;jquery-&gt;toggle(target);</code></p>
140<ul>
141 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
142</ul>
143
144<h3>animate()</h3>
145
146<p><code> $this-&gt;jquery-&gt;animate(target, parameters, optional speed, optional extra information);</code></p>
147<ul>
148 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
149 <li>&quot;paramters&quot; in jQuery would generally include a series of CSS properties that you wish to change.</li>
150 <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
151 <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
152</ul>
153<p>For a full summary, see <a href="http://docs.jquery.com/Effects/animate">http://docs.jquery.com/Effects/animate</a></p>
154<p>Here is an example of an animate() called on a div with an id of &quot;note&quot;, and triggered by a click using the jQuery library's click() event.</p>
155<p><code> $params = array(<br />
156 'height' =&gt; 80,<br />
157 'width' =&gt; '50%',<br />
158 'marginLeft' =&gt; 125<br />
159);<br />
160$this-&gt;jquery-&gt;click('#trigger', $this-&gt;jquery-&gt;animate('#note', $params, normal));</code></p>
161
162<h3>fadeIn() / fadeOut()</h3>
163
164<p><code>$this-&gt;jquery-&gt;fadeIn(target, optional speed, optional extra information);<br />
165 $this-&gt;jquery-&gt;fadeOut(target, optional speed, optional extra information);</code></p>
166<ul>
167 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
168 <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
169 <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
170</ul>
171
172<h3>toggleClass()</h3>
173
174<p>This function will add or remove a CSS class to its target.</p>
175<p><code>$this-&gt;jquery-&gt;toggleClass(target, class)</code></p>
176<ul>
177 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
178 <li>&quot;class&quot; is any CSS classname. Note that this class must be defined and available in a CSS that is already loaded.</li>
179 </ul>
180
181<h3>fadeIn() / fadeOut()</h3>
182
183<p>These effects cause an element(s) to disappear or reappear over time.</p>
184<p><code>$this-&gt;jquery-&gt;fadeIn(target, optional speed, optional extra information);<br />
185 $this-&gt;jquery-&gt;fadeOut(target, optional speed, optional extra information);</code></p>
186<ul>
187 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
188 <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
189 <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
190</ul>
191
192<h3>slideUp() / slideDown() / slideToggle()</h3>
193
194<p>These effects cause an element(s) to slide.</p>
195<p><code>$this-&gt;jquery-&gt;slideUp(target, optional speed, optional extra information);<br />
196 $this-&gt;jquery-&gt;slideDown(target, optional speed, optional extra information);<br />
197$this-&gt;jquery-&gt;slideToggle(target, optional speed, optional extra information);</code></p>
198<ul>
199 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
200 <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
201 <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
202</ul>
203
204<h2>Plugins</h2>
205
206<p>
207
208<p>Some select jQuery plugins are made available using this library.</p>
209
210<h3>corner()</h3>
211<p>Used to add distinct corners to page elements. For full details see <a href="http://www.malsup.com/jquery/corner/">http://www.malsup.com/jquery/corner/</a></p>
212<p><code>$this-&gt;jquery-&gt;corner(target, corner_style);</code></p>
213<ul>
214 <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
215 <li>&quot;corner_style&quot; is optional, and can be set to any valid style such as round, sharp, bevel, bite, dog, etc. Individual corners can be set by following the style with a space and using &quot;tl&quot; (top left), &quot;tr&quot; (top right), &quot;bl&quot; (bottom left), or &quot;br&quot; (bottom right).</li>
216</ul>
217<p><code>$this-&gt;jquery-&gt;corner(&quot;#note&quot;, &quot;cool tl br&quot;);</code></p>
218
219<h3>tablesorter()</h3>
220
221<p>description to come</p>
222
223<h3>modal()</h3>
224
225<p>description to come</p>
226
227<h3>calendar()</h3>
228
229<p>description to come</p>
230
Derek Jonesb8354c12010-03-05 12:47:30 -0600231</div>
232<!-- END CONTENT -->
233
234
235<div id="footer">
236<p>
237Previous Topic:&nbsp;&nbsp;<a href="input.html">Input Class</a>
238&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
239<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
240<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
241Next Topic:&nbsp;&nbsp;<a href="language.html">Language Class</a></p>
Derek Jones898949f2011-01-28 07:42:16 -0600242<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Jonesb8354c12010-03-05 12:47:30 -0600243</div>
244
245</body>
246</html>