<?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>Javascript Kata &#187; ajax</title>
	<atom:link href="http://www.javascriptkata.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Tue, 15 Jun 2010 14:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to write a simple undo system for your app</title>
		<link>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/</link>
		<comments>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 14:20:39 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=188</guid>
		<description><![CDATA[I really like undos. If I could undo that that last beer... Unfortunately, I can't. But, <strong>I can offer undo to users of my application</strong>. ]]></description>
			<content:encoded><![CDATA[<p>I really like undos. If I could undo that that last beer&#8230; Unfortunately, I can&#8217;t. But, <strong>I can offer undo to users of my application</strong>. </p>
<p>When I thought about undo before, I thought about a complicated Rails plugin that would keep an anonymous ID linking to any table in the database with an &#8220;action&#8221; field that would contain the action to undo. Pretty complicated stuff for something as simple.</p>
<h3>The javascript solution</h3>
<p>One day while I was writing some javascript, the solution struck me : <strong>I could handle this completely with javascript</strong>. The basic idea is that whenever an action is completed, I can save its undo function in an array and call it whenever I need it.</p>
<p>Example, if I&#8217;m saving a user in a database with Ajax :</p>
<ol>
<li>I send the data to server to save a new user</li>
<li>The server returns the ID of the new user</li>
<li>I create a function with an ajax request that will send a delete of this user to the server</li>
<li>I add the previous function to a stack of <em>undo</em> functions to be able to call it later</li>
</ol>
<p>That, when I call that last function, it will undo that user creation without having to keep it in memory server-side. </p>
<h3>Introducing jsKata.undo</h3>
<p>I wrote a little something called <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.undo.js">jsKata.undo on GitHub</a> that contains the logic describe earlier. It&#8217;s quite simple but it may grow over time. It has no requirement, not even jQuery. It&#8217;s very simple to use.</p>
<p>The <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.undo.textile">complete doc</a> is on GitHub.</p>
<h4>1. Adding an action that can you can undo</h4>
<p>I&#8217;ll use the &#8220;add a user&#8221; example with jQuery.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">post</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;http://yoursite.com/users/new&quot;</span>, <span class="co1">// The url to add a user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span><span class="kw3">name</span>:<span class="st0">&quot;John Boucher&quot;</span><span class="br0">&#125;</span>, <span class="co1">// The name of the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>newUser, textStatus, XMLHttpRequest<span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// This is called when the post is over</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> newUserId = newUser.<span class="me1">id</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We begin by creating a function to delete the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> undoNewUser = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; $.<span class="me1">post</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;http://yoursite.com/users/delete&quot;</span>, <span class="co1">// The url to delete a user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>id:newUserId<span class="br0">&#125;</span> <span class="co1">// The ID of the new user to delete</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We add the undo function to the stack</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; jskataUndo.<span class="me1">push</span><span class="br0">&#40;</span>undoNewUser<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h4>2. Undo the last action you made</h4>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">undo</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h4>3. Events</h4>
<p>Each time there&#8217;s a change, an <em>onChange</em> event is called. To assign yours, simply write :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">onChange</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Show the undo button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#undoButton&quot;</span><span class="br0">&#41;</span>.<span class="me1">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>There&#8217;s also an <em>onEmpty</em> event that is called when the stack of undoable actions is empty.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">onEmpty</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Hide the undo button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#undoButton&quot;</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>Complete demo</h3>
<p>There is a <a href="http://dsimard.github.com/jskata_examples/undo">complete demo on GitHub</a> as well as the <a href="http://github.com/dsimard/jskata_examples/blob/gh-pages/undo.html">HTML and Javascript</a> code for it. Take a look!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How to use JSON (updated with example)</title>
		<link>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/</link>
		<comments>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 15:14:48 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[librairies]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=91</guid>
		<description><![CDATA[This post is an update from the <a href="/2007/05/08/how-to-use-json/" target="_self">old post</a>. A lot of things changed since it was written and the information in the old one is a bit outdated.]]></description>
			<content:encoded><![CDATA[<p>This post is an update from the <a href="/2007/05/08/how-to-use-json/" target="_self">old post</a>. A lot of things changed since it was written and the information in the old one is a bit outdated.</p>
<p>First of all, <strong>you should never use an <em>eval()</em> when using JSON</strong> because of security reasons that I will talk about later in another post. [edit : jQuery uses <em>eval()</em> but there's a work-around that will be talked about in another post.]</p>
<h3>JSON and jQuery</h3>
<p>The simplest way to use JSON is though <a href="http://jquery.com/" target="_blank">jQuery</a>. I have been using for <a href="http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/">more than 2 years</a> and I never was disappointed.</p>
<p>Let&#8217;s say I want to call <a href="http://www.flickr.com/services/api/flickr.photos.search.html" target="_blank">Flickr search</a> with JSON. It&#8217;s simple :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">getJSON</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;http://api.flickr.com/services/rest/?jsoncallback=?&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> method : <span class="st0">&quot;flickr.photos.search&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; api_key : <span class="st0">&quot;4ef2fe2affcdd6e13218f5ddd0e2500d&quot;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; format : <span class="st0">&quot;json&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags : <span class="st0">&quot;landscape&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; ajaxCallBack</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>The <em>$.getJSON</em> takes 3 parameters.</p>
<p><strong>1. The URL</strong><br />
The URL of the remote call.</p>
<p><strong>2. The parameters</strong><br />
The parameters to complete the call. On the <a href="http://www.flickr.com/services/api/flickr.photos.search.html" target="_blank">documentation page</a> about this Flickr method, you have a list of all the parameters you can use. For a JSON call on Flickr, you have to send the <strong>api_key</strong> and the <strong>format</strong>. I added the parameter <strong>tags</strong> to send me all photos that are tagged <em>landscape</em>.</p>
<p><strong>3. The callback</strong><br />
Most API services now requires a callback, a method that will be called when the JSON is loaded. It is more secure and easier to work with. At the end of the first parameter (the URL), I added <em>jsoncallback=?</em> (flickr called this parameter <em>jsoncallback</em> but it could be named differently on other services). This is the jQuery-way of saying &#8220;When you finish loading the JSON, call the method specificied by the third argument&#8221;.</p>
<p>You may wonder how Flickr knows which function to use on callback. It&#8217;s because in jQuery, there&#8217;s a special case for that. In the URL, you can see that the <em>jsoncallback</em> is written like that <strong>jsoncallback=?</strong>. The <strong>?</strong> is replaced by your own callback method (in my case, I called it <em>ajaxCallback</em>)</p>
<h3>The callback method</h3>
<p>The callback method I used looks like this :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> ajaxCallBack<span class="br0">&#40;</span>data<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Loop throught all photos and display them</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// it uses the jquery.each method </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// doc at http://docs.jquery.com/Utilities/jQuery.each</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; $.<span class="me1">each</span><span class="br0">&#40;</span>data.<span class="me1">photos</span>.<span class="me1">photo</span>, <span class="kw2">function</span><span class="br0">&#40;</span>photoIdx, photo<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build the thumbnail url</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> thumb_url = <span class="br0">&#91;</span><span class="st0">&quot;http://farm&quot;</span>, photo.<span class="me1">farm</span>, <span class="st0">&quot;.static.flickr.com/&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.<span class="me1">server</span>, <span class="st0">&quot;/&quot;</span>, photo.<span class="me1">id</span>, <span class="st0">&quot;_&quot;</span>, photo.<span class="me1">secret</span>, <span class="st0">&quot;_t.jpg&quot;</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build the photo url</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> photo_url = <span class="br0">&#91;</span><span class="st0">&quot;http://www.flickr.com/photos/&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.<span class="me1">owner</span>, <span class="st0">&quot;/&quot;</span>, photo.<span class="me1">id</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build HTML</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;body&quot;</span><span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;&lt;a&gt;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;href&quot;</span>, photo_url<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;target&quot;</span>, <span class="st0">&quot;_blank&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">append</span><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;&lt;img&gt;&quot;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;src&quot;</span>, thumb_url<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>If you know jQuery a little, it simply creates the HTML to show the thumbnails and a link to the photo.</p>
<h3>Get the code</h3>
<p>You can get the complete code for this example on <a href="http://github.com/dsimard/jskata_examples/tree/simple" target="_blank">GitHub</a> and a <a href="http://dsimard.github.com/jskata_examples/flickr_json.html">working example</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ask Dan : More on javascript threading</title>
		<link>http://www.javascriptkata.com/2007/12/06/ask-dan-more-on-javascript-threading/</link>
		<comments>http://www.javascriptkata.com/2007/12/06/ask-dan-more-on-javascript-threading/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 14:05:20 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Ask Dan a javascript question]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=31</guid>
		<description><![CDATA[From time to time, I receive emails from desperate people who want help with their javascript problem. I also receive a lot of emails of people wanting to help me with my &#8220;manly problems&#8221;. It&#8217;s very nice from them to care about me and I take time to reply to each of them but I [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time, I receive emails from desperate people who want help with their javascript problem. I also receive a lot of emails of people wanting to help me with my &#8220;manly problems&#8221;. It&#8217;s very nice from them to care about me and I take time to reply to each of them but I don&#8217;t have that kind of problem for the moment.</p>
<p><strong>Stuart Cooper</strong> recently sent me a mail about threading in javascript and I turn to you, javascripters, to find the a solution for his problem. I <a href="http://refactormycode.com/codes/172-javascript-threading" target="_blank">sent the code on RefactorMyCode for refactorisation</a> and they will automatically show up in the comments of this post (the site is an idea of <a href="http://macournoyer.wordpress.com/" target="_blank">Marc-AndrÃ© Cournoyer</a> who is a very active developer from Montreal). <strong>Don&#8217;t be afraid to think outside the box</strong>. The solution may be completly different from what is already written.</p>
<hr /><em> I am experience trying to create a &#8220;cover page&#8221; that runs a ping to remote servers.In my code I am using setInterval() to run repeating pings (artificially quickly at the moment, ie 10 seconds rather than 1 min) and display the results to browser.  I have built my own XMLHttpRequest module (mainly as a learning exercise) and I don&#8217;t believe it to be the source of my ills.</em><em> </em><em>I have read through your article and the behaviour of alert() and confirm() fits exactly with what I am seeing when I run the following code :</em><em>[snippet]</em></p>
<p><em>function startup(){</em></p>
<p><em>setInterval(&#8220;pinger(&#8216;live&#8217;,0)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;standby&#8217;,1)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;dev&#8217;,2)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test1&#8242;,3)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test2&#8242;,4)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test3&#8242;,5)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test4&#8242;,6)&#8221;,10000);</em></p>
<p><em>}</em></p>
<p><em>function pinger(server,divit){</em></p>
<p><em>console=document.getElementById(&#8216;ping_div&#8217; + divit);<br />
console.innerHTML=&#8221;;<br />
sendRequest(&#8220;../php/ping_sys2.php?target=&#8221; + server);</em></p>
<p><em>}</em></p>
<p><em>[/snippet]</em></p>
<p><em>(each server has its own named div to return to)</em></p>
<p><em>What I am seeing when I run this is the final pinger response from the callback and nothing else.  I since added debugging into my XMLHttpRequest code so that it split out the server response progress.</em></p>
<p><em>[snippet]</em></p>
<p><em>readyXML=xmlReq.readyState;</em></p>
<p><em>[/snippet]</em></p>
<p><em>[snippet]</em></p>
<p><em>if(readyXML == 3){</em></p>
<p><em>data=&#8221;Serving &#8230;&#8221;;</em></p>
<p><em>}</em></p>
<p><em>if(readyXML == 2){</em></p>
<p><em>data=&#8221;Sent &#8230;&#8221;;</em></p>
<p><em>}</em></p>
<p><em>if(readyXML == 1){</em></p>
<p><em>data=&#8221;Opening &#8230;&#8221;;</em></p>
<p><em>}</em></p>
<p><em>[/snippet]</em></p>
<p><em>What I have observed is that when running the setInterval commands as above, all except the final request are &#8220;jammed&#8221; on readyState = 1 and appropriately responds with &#8220;Sending â€¦&#8221;</em></p>
<p><em>however,</em></p>
<p><em>when I do the following (which is messy)</em></p>
<p><em>[snippet]</em></p>
<p><em>function startup(){</em></p>
<p><em>setInterval(&#8220;pinger(&#8216;live&#8217;,0)&#8221;,1000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;standby&#8217;,1)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;dev&#8217;,2)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test1&#8242;,3)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test2&#8242;,4)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test3&#8242;,5)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test4&#8242;,6)&#8221;,10000);</em></p>
<p><em>}</em></p>
<p><em>[/snippet]</em></p>
<p><em>Each response is absolutely spot on and will continue to generate pings correctly.  So in this case the script alert interrupts are forcing the callback request to trigger, whereas without the alerts its only the last request that triggers a callback.  I have also tried using artificial timeout loops instead of alerts (which generate the odd browser &#8220;script running slowly&#8221; message) but to no avail.</em></p>
<p><em>At the moment I am almost resigned to having to create individual events (like rollovers, though I would much prefer the ping initialisation to occur window.onLoad()) that trigger the setInterval() â€¦ which also seems to work fine.</em></p>
<p><em>I am hoping that you may have come across a way of forcing the XMLHttpRequest to respond without forcing alerts or confirms on the user, in the time since the last update to the article.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/12/06/ask-dan-more-on-javascript-threading/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ajax, javascript and threads : the final truth</title>
		<link>http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/</link>
		<comments>http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/#comments</comments>
		<pubDate>Tue, 12 Jun 2007 14:44:38 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=47</guid>
		<description><![CDATA[Since I have written Ajax and javascript donâ€™t use thread, one of my reader (BK) told me that I was wrong. Here&#8217;s the final truth. What I said If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will [...]]]></description>
			<content:encoded><![CDATA[<p>Since I have written <a href="http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/">Ajax and javascript donâ€™t use thread</a>, one of my reader (BK) told me that I was wrong. Here&#8217;s the final truth.</p>
<h3>What I said</h3>
<blockquote><p>If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called).</p></blockquote>
<p>Example. We have a f() function that is incredibly long to execute (5 seconds) because it has a lot of code.</p>
<p>1. function f() is started<br />
2. f() makes an ajax call<br />
3. f() is still executing since 2 seconds<br />
4. the response from the server arrives at the third second<br />
5. f() is still in execution for 3 seconds<br />
6. the callback function for ajax is called</p>
<h3>What he said</h3>
<blockquote><p> Briefly, say I have CallPage1, CallPage2 and DoSomething. Both CallPage are async.</p>
<p>CallPage1 called<br />
CallPage2 called<br />
DoSomething called<br />
CallPage1 Returned (DoSomething is now paused)<br />
DoSomething ended<br />
CallPage2 Returned</p></blockquote>
<p>The DoSomething() function is paused when the ajax response arrives. The callback for the ajax is called and only then, DoSomething() resumes its execution.</p>
<p>That didn&#8217;t make sense to me. Javascript is really sequential and it would not do that kind of complex stuff. But, I had to be sure.</p>
<h3>The test</h3>
<p>I made a simple page to test it all.</p>
<p>1. Loop from 0 to X (user defined). X should be a big number.<br />
2. The first loop, it makes an ajax request<br />
3. The loops will finish someday<br />
4. The callback for the ajax will be called someday</p>
<p>I didn&#8217;t know what to expect : will the callback function be called <strong>before</strong> or <strong>after</strong> the loop?</p>
<h3>The result</h3>
<p>I ran <a href="http://www.javascriptkata.com/wp-content/uploads/test.php">the script</a> a lot of times with a lot of numbers and it was always the same thing : <strong>the callback function waits for the loop to be over before it is executed</strong>. It means that the current function is not paused.</p>
<h3>Now it&#8217;s your turn</h3>
<p>I want you to take a look at <a href="http://www.javascriptkata.com/wp-content/uploads/test.php">the script</a> (<a href="http://www.javascriptkata.com/wp-content/uploads/test.txt" target="_blank">complete php code</a>) (I know it&#8217;s poorly written, it&#8217;s just a test) and tell me if I&#8217;m wrong or if I&#8217;m right.</p>
<p>Thanks to you all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Ask Dan : Procedural VS object-oriented in javascript</title>
		<link>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/</link>
		<comments>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 13:27:21 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Ask Dan a javascript question]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=46</guid>
		<description><![CDATA[Since the beginning of Ask Dan a javascript question, I received a bunch of questions. Here&#8217;s the first one I received. It&#8217;s from Andrew Worcester. There seems (to me anyway) to be an overuse of the &#8220;new&#8221; syntax in libraries. If I create an Ajax object is there a functional benefit to using: &#8220;new Ajax.Request(options)&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Since the beginning of <a href="http://www.javascriptkata.com/2007/06/01/ask-dan-a-javascript-question-first-edition/" title="Ask Dan a javascript question">Ask Dan a javascript question</a>, I received a bunch of questions. Here&#8217;s the first one I received. It&#8217;s from <a href="http://amwmedia.com/" target="_blank" title="Andrew Worcester">Andrew Worcester</a>.</p>
<blockquote><p>There seems (to me anyway) to be an overuse of the &#8220;new&#8221; syntax in libraries. If I create an Ajax object is there a functional benefit to using: &#8220;new Ajax.Request(options)&#8221; verses something like &#8220;sendAjaxRequest(options)&#8221; It seems to me that by using &#8220;new&#8221; causes more hassle because you need to be sure the objects are properly disposed of. So is there a performance or functional benefit to this technique?</p></blockquote>
<p>I had a hard time understanding the question (and I was too ass hole to ask&#8230;) so I&#8217;ll rephrase it in another way : <em>is there a benefit from using </em><em>object-oriented techniques </em><em>VS </em><em>procedural</em><em> in javascript?</em></p>
<h3>   Procedural in javascript</h3>
<p>Though javascript was not a procedural language from the start, it has been overused as such in the beginning (and it&#8217;s still is). That may be because people didn&#8217;t bother learning javascript and they copy/pasted functions from sites that offers low-quality javascript snippet. All they did was &#8220;hacking&#8221; the poorly written code to suit their needs. This technique worked for a long time but with the use of ajax and DHTML, it&#8217;s nearly impossible to have clean code that is not buggy and dirty.</p>
<h3>   Object-oriented in javascript</h3>
<p>The <em>new</em> statement necessary implies the use of objects in javascript. You can have basic information about object in <a href="http://http//www.javascriptkata.com/2007/03/23/how-to-create-objects-in-object-oriented-javascript/" title="How to create objects in object-oriented javascript">How to create objects in object-oriented javascript</a> and in with the <a href="http://www.javascriptkata.com/category/object-oriented/" title="articles tagged Object-oriented">articles tagged Object-oriented</a> of this site.</p>
<h3>   The benefit of object-oriented</h3>
<p>There is one major benefit about object-oriented that creates a lot of other benefits : <strong>the cleanliness of the code</strong>. The other benefits from that are : the code is easier to maintain, to modify, to read and to explain.</p>
<h3>OO applied</h3>
<p>I&#8217;ll apply it to an example. I want to alert the sound of cat by ajax.</p>
<p>In a <strong>procedural way</strong>, you&#8217;ll approximatively have</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// Global variable</span></div>
</li>
<li class="li1">
<div class="de1">ajaxRequest = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> sendCatSoundAjaxRequest<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">ajaxRequest = <span class="kw2">new</span> XMLHttpRequest<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// Create the request in the global var</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &#8230; code for request &#8230;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> receiveCatSoundAjaxRequest<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// The sound is in clear text in the response</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>ajaxRequest.<span class="me1">responseText</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><strong>The problems?</strong> You have a global variable to keep track of the ajax request. Because it could be use by every other functions that need ajax, you can&#8217;t make more than one request at a time. Also, there are nothing that binds the two methods together apart their names that are look alike. Now, just think of the code when you have 5 or 6 different type of requests. Your code will look like garbage.</p>
<p>In a <strong>object-oriented</strong> way, you would all write this ugly code in a separated object. Your main call to see the sound of a cat could be something like</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> cat = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>cat.<span class="me1">getSound</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>The code from above still exists somewhere but the complexity is now hidden behind the <em>Cat</em> class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ajax and javascript don&#8217;t use threads</title>
		<link>http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/</link>
		<comments>http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/#comments</comments>
		<pubDate>Mon, 04 Jun 2007 12:35:49 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=45</guid>
		<description><![CDATA[Since ajax, a lot of people are thinking that asynchrone means &#8220;in a separated thread&#8221;. They are wrong! Synchronous The XMLHttpRequest object gives you the option to make a synchronous request to a server with the parameter async set to false. It means that when you call the server, all javascript executions will stop and [...]]]></description>
			<content:encoded><![CDATA[<p>Since ajax, a lot of people are thinking that asynchrone means &#8220;in a separated thread&#8221;. They are wrong!</p>
<h3>Synchronous</h3>
<p>The XMLHttpRequest object gives you the option to make a synchronous request to a server with the parameter <em>async</em> set to false. It means that when you call the server, all javascript executions will stop and wait for the server to respond to the request. I never saw it used. In fact it is more confusing that anything else because the page seems frozen and users don&#8217;t like frozen things. <strong>If you use it, you are not cool!</strong></p>
<h3><u>A</u>synchronous</h3>
<p>Now you are being cool! The asynchronous is used by everyone and it doesn&#8217;t freeze the page while the call to the server is done. So, if you have a call that takes 30 seconds before receiving the answer, your users will never know that you&#8217;re a bad server-side coder that do not optimize the code he&#8217;s writing (I&#8217;m just joking).</p>
<h3>The misconception of asynchronous</h3>
<p>People take for granted that because it&#8217;s asynchronous, it&#8217;s a thread. They are partially right. There must be a thread created by the browser to keep the javascript running while it makes a request to the server. It&#8217;s internal and you don&#8217;t have access to that thread. But, <strong>the callback function called when the server responds to the ajax request is not in a thread</strong>.</p>
<p>I&#8217;ll explain clearer. If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called). That&#8217;s because javascript itself doesn&#8217;t create a thread to execute the ajax response from the server and simply <strong>waits that all executions are terminated before starting a new one</strong>.</p>
<p>So if you&#8217;re running a lot of ajax requests simultaneously, you might get some weird behavior because they will all wait one on another before executing themselves.</p>
<p>#UPDATE 2007-06-12<br />
Following one of my readers comment (BK), I have written a sequel to this article : <a href="http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/">Ajax, javascript and threads : the final truth</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>3 reasons why I use jQuery</title>
		<link>http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/</link>
		<comments>http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/#comments</comments>
		<pubDate>Tue, 29 May 2007 14:13:15 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[dhtml]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=43</guid>
		<description><![CDATA[These days, you can&#8217;t write a web application without using one of the billionth javascript library. Two of them stood out of the crowd : prototype and jQuery. I will explain why did I choose jQuery. Prototype Prototype is comfortably installed in a lot of developers mind because of two reasons : it was the [...]]]></description>
			<content:encoded><![CDATA[<p>These days, you can&#8217;t write a web application without using one of the billionth <a href="http://www.google.com/search?q=javascript+library" target="_blank" title="javascript library">javascript library</a>. Two of them stood out of the crowd : <a href="http://www.prototypejs.org/" target="_blank" title="prototype">prototype</a> and <a href="http://docs.jquery.com/Main_Page" target="_blank" title="jQuery">jQuery</a>. I will explain why did I choose jQuery.</p>
<h3>   Prototype</h3>
<p>Prototype is comfortably installed in a lot of developers mind because of two reasons : it was the first widely used and <a href="http://script.aculo.us/" target="_blank" title="script.aculo.us">script.aculo.us</a>. Being the first accepted library is a hard thing to do and it will always help them. Scriptaculous (I hate to write it with the dots) had an incredible effect on the popularity of prototype. For the first time, we could do &#8220;flash animation&#8221; without using flash. This was instant popularity.</p>
<h3>   jQuery</h3>
<p>jQuery is one of the bastard child of prototype. It reuses the <span style="font-style: italic">$() function</span> but brings it to a level you never could have hope for. In fact, jQuery is almost completly based on the $ sign. Why? Maybe because it&#8217;s fast, maybe because it&#8217;s trendy. I don&#8217;t know. Example, you want to download a JSON from a server, <span style="font-style: italic">$.getJSON(&#8220;http://www.thesite.com/thejson&#8221;)</span>.</p>
<h3>Reason #1 : The selectors</h3>
<p>jQuery supports a lot of selectors. And when I say <span style="font-style: italic">a lot</span>, I really mean <span style="font-style: italic">a lot</span>. First, there&#8217;s <a href="http://docs.jquery.com/Selectors#CSS_Selectors" target="_blank" title="CSS selectors">CSS selectors</a>. You just have to apply what you know about CSS and you can write many complex selectors.</p>
<p>Second, there&#8217;s <a href="http://docs.jquery.com/Selectors#XPath_Selectors" target="_blank" title="XPath selectors">XPath selectors</a>. When I was young (sic!), I wrote a lot of XSL. And when I say <span style="font-style: italic">a lot</span>, I really mean <span style="font-style: italic">a lot</span>. XSL makes an intensive use of that XPath thingy. Because XHTML is basically XML, we could use XPath to easily navigate throught the DOM. The problem was that before jQuery, nothing could do that. Now, we can!</p>
<p>In comparison, prototype just has the <span style="font-style: italic">$(&#8216;theId&#8217;)</span> to select a element. Ouch!</p>
<h3>Reason #2 : The Attributes</h3>
<p>It&#8217;s easy to <a href="http://docs.jquery.com/DOM/Attributes" target="_blank" title="add/remove attributes">add/remove attributes</a> to any HTML element. I use it when I create new elements or when I need to add/remove a css class on an element. The interesting thing is that you can <a href="http://docs.jquery.com/How_jQuery_Works#Chainability_.28The_Magic_of_jQuery.29" title="chain" target="_blank">chain</a> them and it will work. Example, I want</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span><span class="st0">&quot;div&quot;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;title&quot;</span>, <span class="st0">&quot;This is a div&quot;</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&quot;newClass&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>You can add as many as you want&#8230;</p>
<h3>Reason #3 : Ajax and JSON</h3>
<p>Once again, <a href="http://docs.jquery.com/Ajax" target="_blank" title="ajax">ajax</a> is now easy as 1,2,3. I wrote an <a href="http://www.javascriptkata.com/2007/05/08/how-to-use-json/" target="_blank" title="article on JSON">article on JSON</a> and in my mind, there&#8217;s no real alternative to JSON. jQuery helps you with it.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">getJSON</span><span class="br0">&#40;</span><span class="st0">&quot;http://www.thesite.com/thejson&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>json<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;I received the json and put it in the json var : &quot;</span> + json.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>It&#8217;s a little <a href="http://www.prototypejs.org/learn/json" target="_blank" title="more complex with prototype">more complex with prototype</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>How to use JSON</title>
		<link>http://www.javascriptkata.com/2007/05/08/how-to-use-json/</link>
		<comments>http://www.javascriptkata.com/2007/05/08/how-to-use-json/#comments</comments>
		<pubDate>Tue, 08 May 2007 12:45:58 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=37</guid>
		<description><![CDATA[UPDATE : This was written more than 2 years ago, read the new post on How to use JSON. Web2.0 came with the intensive use of the XMLHttpRequest object even if it was already in our browser before then. A lot of format were tried but JSON will be the winner overall. What is JSON? [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:2.5em 0;color:red;font-weight:bold;font-size:100%;">UPDATE : This was written more than 2 years ago, read the new post on <a href="http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/">How to use JSON</a>.</div>
<p>Web2.0 came with the intensive use of the <a href="http://www.w3.org/TR/XMLHttpRequest/" title="XMLHttpRequest" target="_blank">XMLHttpRequest</a> object even if it was already in our browser before then. A lot of format were tried but JSON will be the winner overall.</p>
<h3>What is JSON?</h3>
<p>JSON is a <span style="font-weight: bold">format for communication between the server-side (PHP, ASP.NET, &#8230;) and the client-side (javascript)</span>. The magic of it is that the response from the server-side can be easily converted to an object via the use of the <span style="font-style: italic">eval()</span> function.</p>
<p><span style="font-style: italic">Eval() </span>is a function that gives you the possibility to execute some code in javascript from a string.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">eval</span><span class="br0">&#40;</span><span class="st0">&quot;alert(&#8216;This is from eval()&#8217;)&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This is a bad idea to use it in your code but so easy for JSON that you have to use it.</p>
<h3>How to JSON on the server-side?</h3>
<p>You can work like an idiot and create your own JSON marshaller for your project or you can go to <a href="http://json.org/" title="json.org" target="_blank">json.org</a> and use one of the objects that you can find.</p>
<p>I&#8217;ll take PHP as example. You want to send an object to javascript. You simply do</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$response</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$response</span><span class="br0">&#91;</span><span class="st0">&quot;id&quot;</span><span class="br0">&#93;</span> = <span class="nu0">3</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$response</span><span class="br0">&#91;</span><span class="st0">&quot;message&quot;</span><span class="br0">&#93;</span> = <span class="st0">&quot;The object was saved&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>json_encode<span class="br0">&#40;</span><span class="re0">$response</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>It will echo</p>
<pre>{id : 3, message : "The object was saved"}</pre>
<h3>How to JSON on the client-side?</h3>
<p>Use the <span style="font-style: italic">eval()</span> function.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> json = <span class="kw1">eval</span><span class="br0">&#40;</span>theServerSideJsonTextResponse<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>You can now use the <span style="font-style: italic">json</span> variable created like this</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>json.<span class="me1">id</span> + <span class="st0">&quot; : &quot;</span> + json.<span class="me1">message</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>and it will alert</p>
<pre>3 : The object was saved</pre>
<p><span style="color: #ff0000; font-weight: bold">Beware!</span><br />
There may be an <strong>invalid label</strong> error here. The solution is to add parenthesis to the response.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> json = <span class="kw1">eval</span><span class="br0">&#40;</span><span class="st0">&quot;(&quot;</span> + <span class="kw1">eval</span><span class="br0">&#40;</span>theServerSideJsonTextResponse<span class="br0">&#41;</span>; + <span class="st0">&quot;)&quot;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p><strong>#UPDATE : May 10, 2007</strong><br />
As it was pointed out in the comments, you should never use <em>eval</em> directly when parsing JSON on the client-side. Use a existing <a href="http://www.json.org/js.html" target="_blank">JSON parser</a> or a library (<a href="http://jquery.com/" target="_blank">jquery</a> is my favorite).</p>
<h3>Other formats</h3>
<p>For sure there are hundreds of ways of sending back a response to javascript but to my experience, JSON is the best. Let&#8217;s take a look at the others.</p>
<p><span style="font-weight: bold">XML is for purists with a lot of time to spend<br />
</span>Do you really want to write a XSD (or DTD) for all the calls that can be made to the server? Anyway, it&#8217;s long to develop because you have to parse it all.</p>
<p><span style="font-weight: bold">HTML is for lazy people<br />
</span>Though it&#8217;s not completly wrong, laziness is often a bad thing. What do you do if you want to reuse information that you got in a HTML? You parse the HTML to extract the info? please don&#8217;t&#8230;</p>
<p><span style="font-weight: bold">CSV is for the people that are stuck in the 90s</span><br />
CSV was the coolest thing back in the 90s along with <span style="font-style: italic">Fresh Prince of Bel-Air</span>.</p>
<h3>Watch out for BISON</h3>
<p><span style="font-style: italic">BISON</span> is nothing else than <span style="font-weight: bold">Binary JSON</span> (see <a href="http://ajaxian.com/archives/bison-binary-json" title="this article" target="_blank">this article</a>). It&#8217;s lighter than JSON but still unstable. My guess is that we&#8217;ll all use it sooner or later&#8230; but it&#8217;s just a guess&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/05/08/how-to-use-json/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
