CodeIgniter installed
[living-lab-site.git] / user_guide / libraries / javascript.html
1 <!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
5 <title>CodeIgniter User Guide : JavaScript Class</title>
6
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>
27 <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>
28 <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.2</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;
45 <a href="../general/drivers.html">Drivers</a> &nbsp;&#8250;&nbsp;
46 JavaScript Driver
47 </td>
48 <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>
49 </tr>
50 </table>
51 <!-- END BREADCRUMB -->
52
53 <br clear="all" />
54
55
56 <!-- START CONTENT -->
57 <div id="content">
58
59 <p class="important"><strong>Note:</strong> This driver is experimental. Its feature set and implementation may change in future releases.</p><br>
60
61 <h1>Javascript Class</h1>
62 <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>
63 <h2>Initializing the Class</h2>
64 <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>
65
66 <code>$this-&gt;load-&gt;library('javascript');</code>
67
68 <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>
69
70 <code>$this-&gt;load-&gt;library('javascript', array('js_library_driver' =&gt; 'scripto', 'autoload' =&gt; FALSE));</code>
71
72 <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>
73
74 <p>Once loaded, the jQuery library object will be available using: <dfn>$this-&gt;javascript</dfn></p>
75 <h2>Setup and Configuration</h2>
76 <h3>Set these variables in your view</h3>
77 <p>As a Javascript library, your files must be available to your application.</p>
78 <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>
79 <p><code>&lt;?php echo $library_src;?&gt;<br />
80 &lt;?php echo $script_head;?&gt;
81 </code></p>
82 <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>
83 <h3>Set the path to the librarys with config items</h3>
84 <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>
85 <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>
86 <p><code>$config['javascript_location'] = 'http://localhost/codeigniter/themes/js/jquery/';<br />
87                 $config['javascript_ajax_img'] = 'images/ajax-loader.gif';</code></p>
88 <p>If you keep your files in the same directories they were downloaded from, then you need not set this configuration items.</p>
89
90 <h2>The jQuery Class</h2>
91
92 <p>To initialize the jQuery class manually in your controller constructor, use the <dfn>$this-&gt;load-&gt;library</dfn> function:</p>
93
94 <code>$this-&gt;load-&gt;library('jquery');</code>
95
96 <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>
97
98 <code>$this-&gt;load-&gt;library('jquery', FALSE);</code>
99
100 <p>Once loaded, the jQuery library object will be available using: <dfn>$this-&gt;jquery</dfn></p>
101
102 <h2>jQuery Events</h2>
103
104 <p>Events are set using the following syntax.</p>
105
106 <p><code>$this-&gt;jquery-&gt;event('element_path', code_to_run());</code></p>
107
108 <p>In the above example:</p>
109
110 <ul>
111         <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>
112         <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>
113         <li>&quot;code_to_run()&quot; is script your write yourself, or an action such as an effect from the jQuery library below.</li>
114         </ul>
115
116 <h2>Effects</h2>
117
118 <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>
119
120 <p><code>$this->jquery->effect([optional path] plugin name);
121 // for example
122 $this->jquery->effect('bounce');
123 </code></p>
124
125 <h3>hide() / show()</h3>
126
127 <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>
128 <p><code>$this-&gt;jquery-&gt;hide(target,  optional speed, optional extra information);<br />
129         $this-&gt;jquery-&gt;show(target,  optional speed, optional extra information);</code></p>
130
131 <ul>
132         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
133         <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
134         <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
135 </ul>
136
137 <h3>toggle()</h3>
138
139 <p>toggle() will change the visibility of an item to the opposite of its current state, hiding visible elements, and revealing hidden ones.</p>
140 <p><code>$this-&gt;jquery-&gt;toggle(target);</code></p>
141 <ul>
142         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
143 </ul>
144
145 <h3>animate()</h3>
146
147 <p><code> $this-&gt;jquery-&gt;animate(target, parameters, optional speed, optional extra information);</code></p>
148 <ul>
149         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
150         <li>&quot;parameters&quot; in jQuery would generally include a series of CSS properties that you wish to change.</li>
151         <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
152         <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
153 </ul>
154 <p>For a full summary, see <a href="http://docs.jquery.com/Effects/animate">http://docs.jquery.com/Effects/animate</a></p>
155 <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>
156 <p><code> $params = array(<br />
157         'height' =&gt; 80,<br />
158         'width' =&gt; '50%',<br />
159         'marginLeft' =&gt; 125<br />
160 );<br />
161 $this-&gt;jquery-&gt;click('#trigger', $this-&gt;jquery-&gt;animate('#note', $params, normal));</code></p>
162
163 <h3>fadeIn() / fadeOut()</h3>
164
165 <p><code>$this-&gt;jquery-&gt;fadeIn(target,  optional speed, optional extra information);<br />
166         $this-&gt;jquery-&gt;fadeOut(target,  optional speed, optional extra information);</code></p>
167 <ul>
168         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
169         <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
170         <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
171 </ul>
172
173 <h3>toggleClass()</h3>
174
175 <p>This function will add or remove a CSS class to its target.</p>
176 <p><code>$this-&gt;jquery-&gt;toggleClass(target, class)</code></p>
177 <ul>
178         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
179         <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>
180         </ul>
181
182 <h3>fadeIn() / fadeOut()</h3>
183
184 <p>These effects cause an element(s) to disappear or reappear over time.</p>
185 <p><code>$this-&gt;jquery-&gt;fadeIn(target,  optional speed, optional extra information);<br />
186         $this-&gt;jquery-&gt;fadeOut(target,  optional speed, optional extra information);</code></p>
187 <ul>
188         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
189         <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
190         <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
191 </ul>
192
193 <h3>slideUp() / slideDown() / slideToggle()</h3>
194
195 <p>These effects cause an element(s) to slide.</p>
196 <p><code>$this-&gt;jquery-&gt;slideUp(target,  optional speed, optional extra information);<br />
197         $this-&gt;jquery-&gt;slideDown(target,  optional speed, optional extra information);<br />
198 $this-&gt;jquery-&gt;slideToggle(target,  optional speed, optional extra information);</code></p>
199 <ul>
200         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
201         <li>&quot;speed&quot; is optional, and is set to either slow, normal, fast, or alternatively a number of milliseconds.</li>
202         <li>&quot;extra information&quot; is optional, and could include a callback, or other additional information.</li>
203 </ul>
204
205 <h2>Plugins</h2>
206
207 <p>
208
209 <p>Some select jQuery plugins are made available using this library.</p>
210
211 <h3>corner()</h3>
212 <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>
213 <p><code>$this-&gt;jquery-&gt;corner(target, corner_style);</code></p>
214 <ul>
215         <li>&quot;target&quot; will be any valid jQuery selector or selectors.</li>
216         <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>
217 </ul>
218 <p><code>$this-&gt;jquery-&gt;corner(&quot;#note&quot;, &quot;cool tl br&quot;);</code></p>
219
220 <h3>tablesorter()</h3>
221
222 <p>description to come</p>
223
224 <h3>modal()</h3>
225
226 <p>description to come</p>
227
228 <h3>calendar()</h3>
229
230 <p>description to come</p>
231
232 </div>
233 <!-- END CONTENT -->
234
235
236 <div id="footer">
237 <p>
238 Previous Topic:&nbsp;&nbsp;<a href="input.html">Input Class</a>
239 &nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
240 <a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
241 <a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
242 Next Topic:&nbsp;&nbsp;<a href="language.html">Language Class</a></p>
243 <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>
244 </div>
245
246 </body>
247 </html>