<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>technology articles - life articles - health articles - love articles &#187; Jquery</title>
	<atom:link href="http://www.oneclickme.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oneclickme.com</link>
	<description>Knowledge Magazine - Technology - Life - Health - Love</description>
	<lastBuildDate>Mon, 30 Jan 2012 03:35:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>check keycode enter use javascript</title>
		<link>http://www.oneclickme.com/check-keycode-enter-use-javascript/</link>
		<comments>http://www.oneclickme.com/check-keycode-enter-use-javascript/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 09:19:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Jquery]]></category>
		<category><![CDATA[kinh nghiệm]]></category>
		<category><![CDATA[Technology articles]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.oneclickme.com/?p=113</guid>
		<description><![CDATA[This function use to check enter on form with jquery function check_key(e) { var keycode; if (window.event) keycode = window.event.keyCode; else if (e) keycode = e.which; if(keycode == 13){ return false; } }]]></description>
			<content:encoded><![CDATA[<blockquote><p>This function use to check enter on form with jquery</p></blockquote>
<blockquote></blockquote>
<blockquote><p>function check_key(e)<br />
{<br />
var keycode;<br />
if (window.event) keycode = window.event.keyCode;<br />
else if (e) keycode = e.which;<br />
if(keycode == 13){<br />
return false;<br />
}</p>
<p>}</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.oneclickme.com/check-keycode-enter-use-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP, JSON and JavaScript usage</title>
		<link>http://www.oneclickme.com/php-json-and-javascript-usage/</link>
		<comments>http://www.oneclickme.com/php-json-and-javascript-usage/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 06:44:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Jquery]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Technology articles]]></category>

		<guid isPermaLink="false">http://www.oneclickme.com/?p=80</guid>
		<description><![CDATA[Today i want to introduce you to jSON (JavaScript Object Notation), in short, it is a simple format designed to exchange data between different programming languages. I will show you how to create JavaScript object, convert it to JSON string, and send to PHP script, which will decode jSON string to readable format (for PHP). But that’s not all, PHP script will create it’s own data object encode it to jSON string and send it [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>Today i want to introduce you to jSON (JavaScript Object Notation), in short, it is a simple format designed to exchange data between different programming languages. I will show you how to create JavaScript object, convert it to JSON string, and send to PHP script, which will decode jSON string to readable format (for PHP). But that’s not all, PHP script will create it’s own data object encode it to jSON string and send it back. All communication between JavaScript and PHP will be done thru AJAX.</p>
<p>If you haven’t heared about jSON yet, then you can visit <a href="http://en.wikipedia.org/wiki/JSON">Wikipedia</a> for more information. The other technology you need to be familiar with before reading this article is AJAX. If you need to, you can read my Introduction to AJAX post. Also if you are looking professional top notch PHP&amp;AJAX knowledge i recommend you one of books below</p>
<ul>
<li><a href="http://www.amazon.com/gp/product/0470109491?ie=UTF8&amp;tag=webdeveblog-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=0470109491">Professional Ajax, 2nd Edition (Programmer to Programmer)</a><img style="border: medium none  ! important; margin: 0px ! important;" src="http://www.assoc-amazon.com/e/ir?t=webdeveblog-20&amp;l=as2&amp;o=1&amp;a=0470109491" border="0" alt="" width="1" height="1" /></li>
<li><a href="http://www.amazon.com/gp/product/047022780X?ie=UTF8&amp;tag=webdeveblog-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=047022780X">Professional JavaScript for Web Developers (Wrox Programmer to Programmer)</a><img style="border: medium none  ! important; margin: 0px ! important;" src="http://www.assoc-amazon.com/e/ir?t=webdeveblog-20&amp;l=as2&amp;o=1&amp;a=047022780X" border="0" alt="" width="1" height="1" /></li>
<li>My <a href="http://ditio.net/2009/02/22/advanced-ajax-best-practices-book-review/">Advanced AJAX: Best Practices book review</a></li>
</ul>
<p><script src="http://www.assoc-amazon.com/s/link-enhancer?tag=webdeveblog-20&amp;o=1" type="text/javascript">
</script><script src="http://www.assoc-amazon.com/s/link-enhancer-common.js" type="text/javascript"></script><script src="http://www.amazon.com/gp/associates/previews/bootstrap.html?assoc_tag=webdeveblog-20" type="text/javascript"></script><script src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/amazonJQ/amazonJQ-combined-core-48319._V231938215_.js" type="text/javascript"></script><script src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/assoc_pp/assoc_pp-previews-41820._V224417644_.js" type="text/javascript"></script><img src="http://www.assoc-amazon.com/e/ir?l=pv3&amp;t=webdeveblog-20&amp;o=1" alt="" /><img src="http://www.assoc-amazon.com/e/ir?t=webdeveblog-20&amp;l=as3&amp;o=1&amp;creative=373489&amp;camp=211189&amp;i=2" alt="" /><img src="http://www.assoc-amazon.com/e/ir?t=webdeveblog-20&amp;l=as2&amp;o=1&amp;creative=9325&amp;camp=211189&amp;i=-2" alt="" /><br />
<noscript>&amp;lt;br /&amp;gt;     &amp;lt;img src=&#8221;http://www.assoc-amazon.com/s/noscript?tag=webdeveblog-20&#8243; mce_src=&#8221;http://www.assoc-amazon.com/s/noscript?tag=webdeveblog-20&#8243; alt=&#8221;" /&amp;gt;&amp;lt;br /&amp;gt; </noscript></p>
<p>Last note before we start, i am constantly working to make my articles as useful for you as possible, so beginning from this tutorial in each of my programming tutorials, at the end of the article you will find complete source code for download.</p>
<h4>JSON objects</h4>
<p>Usually when it comes to JSON we have an encoded string in mind, however JSON is a subset of JavaScript and in this programming language it can be used as is, to create objects. Simple JavaScript object created using JSON notation can look like this:</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</pre>
</td>
<td>
<pre style="font-family: monospace;"><span style="color: #003366; font-weight: bold;">var</span> JSONstring <span style="color: #339933;">=</span>
<span style="color: #009900;">{</span>
    <span style="color: #3366cc;">"firstname"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"Greg"</span><span style="color: #339933;">,</span>
    <span style="color: #3366cc;">"email"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"greg@fake_email.com"</span><span style="color: #339933;">,</span>
    <span style="color: #3366cc;">"hobby"</span><span style="color: #339933;">:</span>
    <span style="color: #009900;">[</span>
	<span style="color: #009900;">{</span>
	    <span style="color: #3366cc;">"hobbyName"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"sport"</span><span style="color: #339933;">,</span>
            <span style="color: #3366cc;">"isHobby"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"true"</span>
	<span style="color: #009900;">}</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">{</span>
		<span style="color: #3366cc;">"hobbyName"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"reading"</span><span style="color: #339933;">,</span>
	        <span style="color: #3366cc;">"isHobby"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"true"</span>
	<span style="color: #009900;">}</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">{</span>
		<span style="color: #3366cc;">"hobbyName"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"music"</span><span style="color: #339933;">,</span>
	        <span style="color: #3366cc;">"isHobby"</span><span style="color: #339933;">:</span> <span style="color: #3366cc;">"false"</span>
	<span style="color: #009900;">}</span>
    <span style="color: #009900;">]</span>
<span style="color: #009900;">}</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Accessing fields is done like in any other JS object (mainly because it is “normal” JavaScript object), if we want to know if hobby “reading” was checked then we would have to write:</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1</pre>
</td>
<td>
<pre style="font-family: monospace;">JSONstring.<span style="color: #660066;">hobby</span><span style="color: #009900;">[</span><span style="color: #cc0000;">1</span><span style="color: #009900;">]</span>.<span style="color: #660066;">isHobby</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Creating JavaScript Objects</h4>
<p>Before we start, we will need something to work with. We will create HTML form with “validate” button, when someone clicks this button the whole proccess i described in the first paragraph will start. Also, despite JSON is a subset of JavaScript there are no built in function for converting JavaScript object into JSON string, so we will use already created class available at JSON homepage, the file is located <a href="http://www.json.org/json2.js">here json2.js</a>.</p>
<p>Here is the code for the HTML form, i think there is no need to explain it:</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19</pre>
</td>
<td>
<pre style="font-family: monospace;"><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">TITLE</span>&gt;</span>ditio.net jSon Tutorial<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">TITLE</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"http://www.json.org/json2.js"</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
// JavaScript source code will be here
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"personal"</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">""</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"POST"</span>&gt;</span>
Name <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"firstname"</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span>&gt;</span>
Email <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"email"</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span>&gt;</span>
Hobby
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"checkbox"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"hobby"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"sport"</span>&gt;</span> Sport
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"checkbox"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"hobby"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"reading"</span>&gt;</span> Reading
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"checkbox"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"hobby"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"music"</span>&gt;</span> Music
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"button"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"valid"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"Validate"</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"validate()"</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>After clicking “validate” button validate() function will be called, we need to add it, in head section for example:</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</pre>
</td>
<td>
<pre style="font-family: monospace;"><span style="color: #003366; font-weight: bold;">function</span> validate<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
    <span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span><span style="color: #009900;">[</span><span style="color: #3366cc;">'personal'</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>

    <span style="color: #003366; font-weight: bold;">var</span> JSONObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #339933;">;</span>
    JSONObject.<span style="color: #660066;">firstname</span> <span style="color: #339933;">=</span> p<span style="color: #009900;">[</span><span style="color: #3366cc;">'firstname'</span><span style="color: #009900;">]</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    JSONObject.<span style="color: #660066;">email</span> <span style="color: #339933;">=</span> p<span style="color: #009900;">[</span><span style="color: #3366cc;">'email'</span><span style="color: #009900;">]</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    JSONObject.<span style="color: #660066;">hobby</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #339933;">;</span>

    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #cc0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #cc0000;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">)</span>
    <span style="color: #009900;">{</span>
        JSONObject.<span style="color: #660066;">hobby</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #339933;">;</span>
	JSONObject.<span style="color: #660066;">hobby</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>.<span style="color: #660066;">hobbyName</span> <span style="color: #339933;">=</span> p<span style="color: #009900;">[</span><span style="color: #3366cc;">'hobby'</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
	JSONObject.<span style="color: #660066;">hobby</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>.<span style="color: #660066;">isHobby</span> <span style="color: #339933;">=</span> p<span style="color: #009900;">[</span><span style="color: #3366cc;">'hobby'</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>.<span style="color: #660066;">checked</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">}</span>

    JSONstring <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">(</span>JSONObject<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    runAjax<span style="color: #009900;">(</span>JSONstring<span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Code is quite easy to understand. First i assign whole form to variable p just to make further access to this form data easier. In next lines JavaScript object is created, as you can see it consists only from Object and Array data objects.</p>
<p>Note that this is completely the same object as in first listing of this tutorial, however different method was used to create it.</p>
<h4>Sending JSON object to PHP with AJAX</h4>
<p>I do not want ot get into AJAX here because it is not the topic of this tutorial, i will use code from my <a href="http://ditio.net/2007/07/14/your-very-first-ajax-web-application/">Introduction to AJAX post</a>, if you do not want ot go there then remember that at the end of this article there is whole source code waiting to be downloaded.</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30</pre>
</td>
<td>
<pre style="font-family: monospace;"><span style="color: #003366; font-weight: bold;">var</span> request<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> runAjax<span style="color: #009900;">(</span>JSONstring<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
    <span style="color: #006600; font-style: italic;">// function returns "AJAX" object, depending on web browser</span>
    <span style="color: #006600; font-style: italic;">// this is not native JS function!</span>
    request <span style="color: #339933;">=</span> getHTTPObject<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    request.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> sendData<span style="color: #339933;">;</span>
    request.<span style="color: #000066;">open</span><span style="color: #009900;">(</span><span style="color: #3366cc;">"GET"</span><span style="color: #339933;">,</span> <span style="color: #3366cc;">"parser.php?json="</span><span style="color: #339933;">+</span>JSONstring<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    request.<span style="color: #660066;">send</span><span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

<span style="color: #006600; font-style: italic;">// function is executed when var request state changes</span>
<span style="color: #003366; font-weight: bold;">function</span> sendData<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
    <span style="color: #006600; font-style: italic;">// if request object received response</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">(</span>request.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #cc0000;">4</span><span style="color: #009900;">)</span>
    <span style="color: #009900;">{</span>
	<span style="color: #006600; font-style: italic;">// parser.php response</span>
	<span style="color: #003366; font-weight: bold;">var</span> JSONtext <span style="color: #339933;">=</span> request.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// convert received string to JavaScript object</span>
	<span style="color: #003366; font-weight: bold;">var</span> JSONobject <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">(</span>JSONtext<span style="color: #009900;">)</span><span style="color: #339933;">;</span>

	<span style="color: #006600; font-style: italic;">// notice how variables are used</span>
	<span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> <span style="color: #3366cc;">"Number of errors: "</span><span style="color: #339933;">+</span>JSONobject.<span style="color: #660066;">errorsNum</span><span style="color: #339933;">+</span>
		<span style="color: #3366cc;">"<span style="color: #000099; font-weight: bold;">\n</span>- "</span><span style="color: #339933;">+</span>JSONobject.<span style="color: #660066;">error</span><span style="color: #009900;">[</span><span style="color: #cc0000;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">+</span>
		<span style="color: #3366cc;">"<span style="color: #000099; font-weight: bold;">\n</span>- "</span><span style="color: #339933;">+</span>JSONobject.<span style="color: #660066;">error</span><span style="color: #009900;">[</span><span style="color: #cc0000;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>

	<span style="color: #000066;">alert</span><span style="color: #009900;">(</span>msg<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>That’s it we are half way there, now we need to create PHP script to handle AJAX request.</p>
<h4>JSON and PHP</h4>
<p>Decoding JSON string is very simple with PHP only one line of code is needed to parse string into object. Similary only one function is needed to encode PHP object or array into JSON string, look at the code:</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22</pre>
</td>
<td>
<pre style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>

<span style="color: #666666; font-style: italic;">// decode JSON string to PHP object</span>
<span style="color: #000088;">$decoded</span> <span style="color: #339933;">=</span> json_decode<span style="color: #009900;">(</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'json'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #666666; font-style: italic;">// do something with data here</span>

<span style="color: #666666; font-style: italic;">// create response object</span>
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$json</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'errorsNum'</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$json</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$json</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Wrong email!'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$json</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Wrong hobby!'</span><span style="color: #339933;">;</span>

<span style="color: #666666; font-style: italic;">// encode array $json to JSON string</span>
<span style="color: #000088;">$encoded</span> <span style="color: #339933;">=</span> json_encode<span style="color: #009900;">(</span><span style="color: #000088;">$json</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #666666; font-style: italic;">// send response back to index.html</span>
<span style="color: #666666; font-style: italic;">// and end script execution</span>
<span style="color: #990000;">die</span><span style="color: #009900;">(</span><span style="color: #000088;">$encoded</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>It is also interesting what is inside $decode variable</p>
<div>
<table border="0">
<tbody>
<tr>
<td>
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26</pre>
</td>
<td>
<pre style="font-family: monospace;">stdClass Object
(
    [firstname] =&gt; fgfg
    [email] =&gt;
    [hobby] =&gt; Array
        (
            [0] =&gt; stdClass Object
                (
                    [hobbyName] =&gt; sport
                    [isHobby] =&gt; 1
                )

            [1] =&gt; stdClass Object
                (
                    [hobbyName] =&gt; reading
                    [isHobby] =&gt;
                )

            [2] =&gt; stdClass Object
                (
                    [hobbyName] =&gt; music
                    [isHobby] =&gt;
                )

        )
)</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>PHP finished execution, then “request” object status in JavaScript is now equal to 4. Response text (in sendData() function) will be parsed with JSON class to object and used to display message on the screen. Note that instead of using JSON.parse() we could use JavaScriipt eval() function.</p>
<h4>Conclusion</h4>
<p>This tutorial was intended to introduce you to JSON, and i wanted to make this tutorial as clear as possible so i intentionally used the simplest methods to achieve my goal. However this tutorial wouldn’t be complete if i wouldn’t give you further resources from which you can learn more.</p>
<p>First you should check out is <a href="http://framework.zend.com/manual/en/zend.json.html">Zend_Json</a> class (a part of Zend Framework), it has the same functionality as json_decode() and json_decode(), but can handle more complicated JSON strings then those two functions.</p>
<p>Second is json.org home of JSON, check especially <a href="http://www.json.org/js.html">this tutorial</a>, it has got great examples of more advanced JSON class usage.</p>
<blockquote><p><a href="http://www.oneclickme.com/wp-content/uploads/2009/09/json.zip">Download code &#8211; json -javascript-php</a></p></blockquote>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.oneclickme.com/php-json-and-javascript-usage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Ways to Instantly Increase Your jQuery Performance</title>
		<link>http://www.oneclickme.com/10-ways-to-instantly-increase-your-jquery-performance/</link>
		<comments>http://www.oneclickme.com/10-ways-to-instantly-increase-your-jquery-performance/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 03:24:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Jquery]]></category>
		<category><![CDATA[Technology articles]]></category>

		<guid isPermaLink="false">http://www.oneclickme.com/?p=76</guid>
		<description><![CDATA[1. Always Use the Latest Version jQuery is in constant development and improvement. John and his team are always researching new ways to improve program performances. As a sidenote, just a few months ago, he released Sizzle, a selector library that&#8217;s said to improve program performances up to 3 times in Firefox. If you want to stay up to date without having to download the library a thousand times, GIYF (Google Is Your Friend), in [...]]]></description>
			<content:encoded><![CDATA[<h3 id="list">1. Always Use the Latest Version</h3>
<div><img src="http://nettuts.s3.amazonaws.com/359_jqueryTips/j2.png" alt="" /></div>
<p id="item">jQuery is in constant development and improvement. <a href="http://ejohn.org/">John</a> and his team are always researching new ways to improve program performances. As a sidenote, just a few months ago, he released <a href="http://sizzlejs.com/">Sizzle</a>, a selector library that&#8217;s said to improve program performances up to 3 times in Firefox.</p>
<p>If you want to stay up to date without having to download the library a thousand times, GIYF (Google Is Your Friend), in this situation too. Google provides a lot of <a href="http://code.google.com/apis/ajaxlibs/">Ajax libraries</a> from which to choose.</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>&lt;!&#8211; get the API with a simple script tag &#8211;&gt;</span><span> </span></span></li>
<li><span><span>&lt;</span><span>script</span><span> </span><span>type</span><span>=</span><span>&#8220;text/javascript&#8221;</span><span> </span><span>src</span><span>=</span><span>&#8220;http://www.google.com/jsapi&#8221;</span><span>&gt;</span><span>&lt;/</span><span>script</span><span>&gt;</span><span> </span></span></li>
<li><span><span>&lt;</span><span>script</span><span> </span><span>type</span><span>=</span><span>&#8220;text/javascript&#8221;</span><span>&gt;</span><span> </span></span></li>
<li><span> /* and load minified jQuery v1.3.2 this way */ </span></li>
<li><span> google.load (&#8220;jquery&#8221;, &#8221;1.3.2&#8243;, {uncompressed: false}); </span></li>
<li><span> </span></li>
<li><span> /* this is to display a message box </span></li>
<li><span> when the page is loaded */ </span></li>
<li><span> function onLoad () { </span></li>
<li><span> alert (&#8220;jQuery + Google API!&#8221;); </span></li>
<li><span> } </span></li>
<li><span> </span></li>
<li><span> google.setOnLoadCallback (onLoad); </span></li>
<li><span><span>&lt;/</span><span>script</span><span>&gt;</span><span> </span></span></li>
</ol>
</div>
<pre style="display: none;">	&lt;!-- get the API with a simple script tag --&gt;
	&lt;script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
	&lt;script type="text/javascript"&gt;
		/* and load minified jQuery v1.3.2 this way */
		google.load ("jquery", "1.3.2", {uncompressed: false});

		/* this is to display a message box
		   when the page is loaded */
		function onLoad () {
			alert ("jQuery + Google API!");
		}

		google.setOnLoadCallback (onLoad);
	&lt;/script&gt;</pre>
<p><strong><em>* Editor&#8217;s Note: </em></strong>Perhaps, the quicker and easier method is to simply link to the script directly. Rather than hard-coding the specific version of jQuery directly (1.3.2), you should instead use 1, which will automatically reference the most recent version of the library.</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>&lt;</span><span>script</span><span> </span><span>type</span><span>=</span><span>&#8220;text/javascript&#8221;</span><span> </span><span>src</span><span>=</span><span>&#8220;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js&#8221;</span><span>&gt;</span><span>&lt;/</span><span>script</span><span>&gt;</span><span> </span></span></li>
</ol>
</div>
<pre style="display: none;">	&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"&gt;&lt;/script&gt;</pre>
<h3>2. Combine and Minify Your Scripts</h3>
<div><img style="width: 600px; display: inline;" src="http://nettuts.s3.amazonaws.com/359_jqueryTips/jquery.png" alt="Minify" /></div>
<p>The majority of browsers are not able to process more than one script concurrently so they queue them up &#8212; and load times increase. Assuming the scripts are to be loaded on every page of your website, you should consider putting them all into a single file and use a compression tool (such as <a href="http://dean.edwards.name/packer/">Dean Edwards&#8217;</a>) to minify them. Smaller file sizes equal faster load times.</p>
<blockquote><p>The goal of JavaScript and CSS minification is always to preserve the operational qualities of the code while reducing its overall byte footprint (both in raw terms and after gzipping, as most JavaScript and CSS served from production web servers is gzipped as part of the HTTP protocol). &#8212; From <a href="http://developer.yahoo.com/yui/compressor/">YUI compressor</a>, an excellent tool <strong>jQuery officially reccomends</strong> to minify scripts.</p></blockquote>
<h3>3. Use For Instead of Each</h3>
<p>Native functions are always faster than any helper counterparts. Whenever you&#8217;re looping through an object received as JSON, you&#8217;d better <strong>rewrite</strong> your JSON and make it return an array through which you can loop easier.</p>
<p>Using <a href="http://getfirebug.com/">Firebug</a>, it&#8217;s possible to measure the time each of the two functions takes to run.</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>var</span><span> array = </span><span>new</span><span> Array (); </span></span></li>
<li><span><span>for</span><span> (</span><span>var</span><span> i=0; i&lt;10000; i++) { </span></span></li>
<li><span> array[i] = 0; </span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>console.time(<span>&#8216;native&#8217;</span><span>); </span></span></li>
<li><span><span>var</span><span> l = array.length; </span></span></li>
<li><span><span>for</span><span> (</span><span>var</span><span> i=0;i&lt;l; i++) { </span></span></li>
<li><span> array[i] = i; </span></li>
<li><span>} </span></li>
<li><span>console.timeEnd(<span>&#8216;native&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span>console.time(<span>&#8216;jquery&#8217;</span><span>); </span></span></li>
<li><span>$.each (array, <span>function</span><span> (i) { </span></span></li>
<li><span> array[i] = i; </span></li>
<li><span>}); </span></li>
<li><span>console.timeEnd(<span>&#8216;jquery&#8217;</span><span>); </span></span></li>
</ol>
</div>
<pre style="display: none;">	var array = new Array ();
	for (var i=0; i&lt;10000; i++) {
		array[i] = 0;
	}

	console.time('native');
	var l = array.length;
	for (var i=0;i&lt;l; i++) {
		array[i] = i;
	}
	console.timeEnd('native');

	console.time('jquery');
	$.each (array, function (i) {
		array[i] = i;
	});
	console.timeEnd('jquery');</pre>
<div><img style="display: inline;" src="http://chartgizmo.com/GenerateChart?id=6757" alt="" /></div>
<p>The above results are 2ms for native code, and 26ms for jQuery&#8217;s &#8220;each&#8221; method. Provided I tested it on my local machine and they&#8217;re not actually doing anything (just a mere array filling operation), jQuery&#8217;s each function takes over 10 times as long as JS native &#8220;for&#8221; loop. This will certainly increase when dealing with more complicated stuff, like setting CSS attributes or other DOM manipulation operations.</p>
<h3>4. Use IDs Instead of Classes</h3>
<p>It&#8217;s much better to select objects by ID because of the library&#8217;s behavior: jQuery uses the browser&#8217;s native method, getElementByID(), to retrieve the object, resulting in a very fast query.</p>
<p>So, instead of using the very handy class selection technique, it&#8217;s worth using a more complex selector (which jQuery certainly doesn&#8217;t fail to provide), write your own selector (yes, this is possible, if you don&#8217;t find what you need), or specify a container for the element you need to select.</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>// Example creating a list and filling it with items</span><span> </span></span></li>
<li><span><span>//  and selecting each item once</span><span> </span></span></li>
<li><span> </span></li>
<li><span>console.time(<span>&#8216;class&#8217;</span><span>); </span></span></li>
<li><span><span>var</span><span> list = $(</span><span>&#8216;#list&#8217;</span><span>); </span></span></li>
<li><span><span>var</span><span> items = </span><span>&#8216;&lt;ul&gt;&#8217;</span><span>; </span></span></li>
<li><span> </span></li>
<li><span><span>for</span><span> (i=0; i&lt;1000; i++) { </span></span></li>
<li><span> items += <span>&#8216;&lt;li class=&#8221;item&#8217;</span><span> + i + </span><span>&#8216;&#8221;&gt;item&lt;/li&gt;&#8217;</span><span>; </span></span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>items += <span>&#8216;&lt;/ul&gt;&#8217;</span><span>; </span></span></li>
<li><span>list.html (items); </span></li>
<li><span> </span></li>
<li><span><span>for</span><span> (i=0; i&lt;1000; i++) { </span></span></li>
<li><span> <span>var</span><span> s = $(</span><span>&#8216;.item&#8217;</span><span> + i); </span></span></li>
<li><span>} </span></li>
<li><span>console.timeEnd(<span>&#8216;class&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span>console.time(<span>&#8216;id&#8217;</span><span>); </span></span></li>
<li><span><span>var</span><span> list = $(</span><span>&#8216;#list&#8217;</span><span>); </span></span></li>
<li><span><span>var</span><span> items = </span><span>&#8216;&lt;ul&gt;&#8217;</span><span>; </span></span></li>
<li><span> </span></li>
<li><span><span>for</span><span> (i=0; i&lt;1000; i++) { </span></span></li>
<li><span> items += <span>&#8216;&lt;li id=&#8221;item&#8217;</span><span> + i + </span><span>&#8216;&#8221;&gt;item&lt;/li&gt;&#8217;</span><span>; </span></span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>items += <span>&#8216;&lt;/ul&gt;&#8217;</span><span>; </span></span></li>
<li><span>list.html (items); </span></li>
<li><span> </span></li>
<li><span><span>for</span><span> (i=0; i&lt;1000; i++) { </span></span></li>
<li><span> <span>var</span><span> s = $(</span><span>&#8216;#item&#8217;</span><span> + i); </span></span></li>
<li><span>} </span></li>
<li><span>console.timeEnd(<span>&#8216;id&#8217;</span><span>); </span></span></li>
</ol>
</div>
<pre style="display: none;">	// Example creating a list and filling it with items
	//  and selecting each item once

	console.time('class');
	var list = $('#list');
	var items = '&lt;ul&gt;';

	for (i=0; i&lt;1000; i++) {
		items += '&lt;li&gt;item&lt;/li&gt;';
	}

	items += '&lt;/ul&gt;';
	list.html (items);

	for (i=0; i&lt;1000; i++) {
		var s = $('.item' + i);
	}
	console.timeEnd('class');

	console.time('id');
	var list = $('#list');
	var items = '&lt;ul&gt;';

	for (i=0; i&lt;1000; i++) {
		items += '&lt;li id="item' + i + '"&gt;item&lt;/li&gt;';
	}

	items += '&lt;/ul&gt;';
	list.html (items);

	for (i=0; i&lt;1000; i++) {
		var s = $('#item' + i);
	}
	console.timeEnd('id');</pre>
<p>The above code really shows the differences between the two ways of selecting elements, highlighting a never-ending over 5 seconds time to load the class driven snippet.</p>
<div><img style="display: inline;" src="http://chartgizmo.com/GenerateChart?id=6758" alt="" /></div>
<h3>5. Give your Selectors a Context</h3>
<p>As stated in <a href="http://docs.jquery.com/Core/context">jQuery&#8217;s documentation</a>,</p>
<blockquote><p>The DOM node context originally passed to jQuery() (if none was passed then context will be equal to the document). 	It should be used in conjunction with the selector to determine the exact query used.</p></blockquote>
<p>So, if you must use classes to target your elements, at least prevent jQuery from traversing the whole DOM using selectors <a href="http://docs.jquery.com/Core/context">appropriately</a>.</p>
<p>Instead of</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>$(</span><span>&#8216;.class&#8217;</span><span>).css (</span><span>&#8216;color&#8217;</span><span> </span><span>&#8216;#123456&#8242;</span><span>); </span></span></li>
</ol>
</div>
<pre style="display: none;">	$('.class').css ('color' '#123456');</pre>
<p>always go for contextualized selectors in the form:</p>
<div>
<ol>
<li><span><span>$(expression, context) </span></span></li>
</ol>
</div>
<pre style="display: none;">	$(expression, context)</pre>
<p>thus yielding</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>$(</span><span>&#8216;.class&#8217;</span><span>, </span><span>&#8216;#class-container&#8217;</span><span>).css (</span><span>&#8216;color&#8217;</span><span>, </span><span>&#8216;#123456&#8242;</span><span>); </span></span></li>
</ol>
</div>
<pre style="display: none;">	$('.class', '#class-container').css ('color', '#123456');</pre>
<p>which runs much faster, because it doesn&#8217;t have to traverse the entire DOM &#8212;  just the #class-container element.</p>
<h3>6. Cache. ALWAYS.</h3>
<p>Do not make the mistake or reusing your selectors time and time again. Instead, you should cache it in a variable. That way, the DOM doesn&#8217;t have to track down your element over and over again.</p>
<p>Never select elements multiple times inside a loop EVER! It&#8217;d be a speed-killer!</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>$(</span><span>&#8216;#item&#8217;</span><span>).css (</span><span>&#8216;color&#8217;</span><span>, </span><span>&#8216;#123456&#8242;</span><span>); </span></span></li>
<li><span>$(<span>&#8216;#item&#8217;</span><span>).html (</span><span>&#8216;hello&#8217;</span><span>); </span></span></li>
<li><span>$(<span>&#8216;#item&#8217;</span><span>).css (</span><span>&#8216;background-color&#8217;</span><span>, </span><span>&#8216;#ffffff&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span><span>// you could use this instead</span><span> </span></span></li>
<li><span>$(<span>&#8216;#item&#8217;</span><span>).css (</span><span>&#8216;color&#8217;</span><span>, </span><span>&#8216;#123456&#8242;</span><span>).html (</span><span>&#8216;hello&#8217;</span><span>).css (</span><span>&#8216;background-color&#8217;</span><span>, </span><span>&#8216;#ffffff&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span><span>// or even</span><span> </span></span></li>
<li><span><span>var</span><span> item = $(</span><span>&#8216;#item&#8217;</span><span>); </span></span></li>
<li><span>item.css (<span>&#8216;color&#8217;</span><span>, </span><span>&#8216;#123456&#8242;</span><span>); </span></span></li>
<li><span>item.html (<span>&#8216;hello&#8217;</span><span>); </span></span></li>
<li><span>item.css (<span>&#8216;background-color&#8217;</span><span>, </span><span>&#8216;#ffffff&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span> </span></li>
<li><span><span>// as for loops, this is a big no-no</span><span> </span></span></li>
<li><span>console.time(<span>&#8216;no cache&#8217;</span><span>); </span></span></li>
<li><span><span>for</span><span> (</span><span>var</span><span> i=0; i&lt;1000; i++) { </span></span></li>
<li><span> $(<span>&#8216;#list&#8217;</span><span>).append (i); </span></span></li>
<li><span>} </span></li>
<li><span>console.timeEnd(<span>&#8216;no cache&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span><span>// much better this way</span><span> </span></span></li>
<li><span>console.time(<span>&#8216;cache&#8217;</span><span>); </span></span></li>
<li><span><span>var</span><span> item = $(</span><span>&#8216;#list&#8217;</span><span>); </span></span></li>
<li><span> </span></li>
<li><span><span>for</span><span> (</span><span>var</span><span> i=0; i&lt;1000; i++) { </span></span></li>
<li><span> item.append (i); </span></li>
<li><span>} </span></li>
<li><span>console.timeEnd(<span>&#8216;cache&#8217;</span><span>); </span></span></li>
</ol>
</div>
<pre style="display: none;">	$('#item').css ('color', '#123456');
	$('#item').html ('hello');
	$('#item').css ('background-color', '#ffffff');

	// you could use this instead
	$('#item').css ('color', '#123456').html ('hello').css ('background-color', '#ffffff');

	// or even
	var item = $('#item');
	item.css ('color', '#123456');
	item.html ('hello');
	item.css ('background-color', '#ffffff');

	// as for loops, this is a big no-no
	console.time('no cache');
	for (var i=0; i&lt;1000; i++) {
		$('#list').append (i);
	}
	console.timeEnd('no cache');

	// much better this way
	console.time('cache');
	var item = $('#list');

	for (var i=0; i&lt;1000; i++) {
		item.append (i);
	}
	console.timeEnd('cache');</pre>
<p>And, as the following chart exemplifies, the results of caching are evident even in relatively short iterations.</p>
<div><img style="display: inline;" src="http://chartgizmo.com/GenerateChart?id=6759" alt="" /></div>
<h3>7. Avoid DOM Manipulation</h3>
<p>DOM manipulation should be as limited as possible, since insert operations like <a href="http://docs.jquery.com/Manipulation/prepend">prepend()</a>, <a href="http://docs.jquery.com/Manipulation/append">append()</a>, <a href="http://docs.jquery.com/Manipulation/after">after()</a> are rather time-consuming.</p>
<p>The above example could be quickened using <a href="http://docs.jquery.com/Html">html()</a> and building the list beforehand.</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>var</span><span> list = </span><span>&#8221;</span><span>; </span></span></li>
<li><span> </span></li>
<li><span><span>for</span><span> (</span><span>var</span><span> i=0; i&lt;1000; i++) { </span></span></li>
<li><span> list += <span>&#8216;&lt;li&gt;&#8217;</span><span>+i+</span><span>&#8216;&lt;/li&gt;&#8217;</span><span>; </span></span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>(<span>&#8216;#list&#8217;</span><span>).html (list); </span></span></li>
</ol>
</div>
<pre style="display: none;">	var list = '';

	for (var i=0; i&lt;1000; i++) {
		list += '&lt;li&gt;'+i+'&lt;/li&gt;';
	}

	('#list').html (list);</pre>
<h3>8. No String concat(); Use join() for Longer Strings</h3>
<p>It might appear strange, but this really helps to speed things, especially when dealing with long strings of text that need to be concatenated.</p>
<p>First create an array and fill it with what you have to join together. The join() method will prove much faster than the string concat() function.</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>var</span><span> array = []; </span></span></li>
<li><span><span>for</span><span> (</span><span>var</span><span> i=0; i&lt;=10000; i++) { </span></span></li>
<li><span> array[i] = <span>&#8216;&lt;li&gt;&#8217;</span><span>+i+</span><span>&#8216;&lt;/li&gt;&#8217;</span><span>; </span></span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>$(<span>&#8216;#list&#8217;</span><span>).html (array.join (</span><span>&#8221;</span><span>)); </span></span></li>
</ol>
</div>
<pre style="display: none;">	var array = [];
	for (var i=0; i&lt;=10000; i++) {
	    array[i] = '&lt;li&gt;'+i+'&lt;/li&gt;';
	}

	$('#list').html (array.join (''));</pre>
<p>However, recent tests conducted by <a href="http://www.sitepen.com/blog/2008/05/09/string-performance-an-analysis/">Tom Trenka</a> contributed to the creation of the following chart.</p>
<div><img style="width: 600px; display: inline;" src="http://www.sitepen.com/blog/wp-content/uploads/2008/05/stringperf.png" alt="" /></div>
<blockquote><p>&#8220;The += operator is faster—even more than pushing string fragments into an array and joining them at the last minute&#8221; and &#8220;An array as a string buffer is more efficient on all browsers, with the exception of Firefox 2.0.0.14/Windows, than using String.prototype.concat.apply.&#8221; &#8212; Tom Trenka</p></blockquote>
<h3>9. Return False</h3>
<p>You may have noticed whenever your functions don&#8217;t return false, you jump to the top of the page. When dealing with longer pages, this result can be quite annoying.</p>
<p>So, instead of</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>$(</span><span>&#8216;#item&#8217;</span><span>).click (</span><span>function</span><span> () { </span></span></li>
<li><span> <span>// stuff here</span><span> </span></span></li>
<li><span>}); </span></li>
</ol>
</div>
<pre style="display: none;">	$('#item').click (function () {
		// stuff here
	});</pre>
<p>take the time to write</p>
<div>
<div>
<div><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/#">?</a></div>
</div>
<ol>
<li><span><span>$(</span><span>&#8216;#item&#8217;</span><span>).click (</span><span>function</span><span> () { </span></span></li>
<li><span> <span>// stuff here</span><span> </span></span></li>
<li><span> <span>return</span><span> </span><span>false</span><span>; </span></span></li>
<li><span>}); </span></li>
</ol>
</div>
<pre style="display: none;">	$('#item').click (function () {
		// stuff here
		return false;
	});</pre>
<h3>10. Bonus tip &#8211; Cheat-sheets and Library References</h3>
<div><img style="width: 600px; display: inline;" src="http://nettuts.s3.amazonaws.com/359_jqueryTips/cheatsheet.png" alt="" /></div>
<p>This isn&#8217;t a speed up tip, but could end up, in a round about way, being one if you take the time to find your way through <a href="http://colorcharge.com/jquery/">cheatsheets</a> and <a href="http://docs.jquery.com/">function references</a>. Save yourself some time and keep a cheat-sheet within an arm&#8217;s reach.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oneclickme.com/10-ways-to-instantly-increase-your-jquery-performance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

