<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Javascript Kata</title>
	
	<link>http://www.javascriptkata.com</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Mon, 05 Oct 2009 14:48:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/javascriptkata/no_echo_chamber" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>How to write a singleton class in javascript</title>
		<link>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/</link>
		<comments>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 12:59:54 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=166</guid>
		<description><![CDATA[If I look at my stats, a lot of people are wondering how to write a singleton class. I already <a href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/" target="_blank">wrote about it before</a> but my old solution exposed the instance of the class so more than one instance could be created thus making it not completly a singleton class.]]></description>
			<content:encoded><![CDATA[<p>If I look at my stats, a lot of people are wondering how to write a singleton class. I already <a href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/" target="_blank">wrote about it before</a> but my old solution exposed the instance of the class so more than one instance could be created thus making it not completly a singleton class.</p>
<p>Here&#8217;s the new solution using a <a href="http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/">private variable</a> for the instance.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Cats<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="kw2">var</span> names = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">singletonInstance</span> = <span class="kw2">null</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Get the instance of the Cats class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// If there&#8217;s none, instanciate one</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> getInstance = <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; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="kw1">this</span>.<span class="me1">singletonInstance</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">singletonInstance</span> = createInstance<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">singletonInstance</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// Create an instance of the Cats class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> createInstance = <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; &nbsp; <span class="co1">// Here, you return all public methods and variables</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; add : <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; names.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; names : <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; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> names;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> getInstance<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How to use it</h3>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> run<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">// Add a new cat</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> cat1 = <span class="kw2">new</span> Cats<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; cat1.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;Mistigri&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat1.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">1</span>, <span class="st0">&quot;cat1 contains 1 cat : &quot;</span> + cat1.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<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;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Use another instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> cat2 = <span class="kw2">new</span> Cats<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">1</span>, <span class="st0">&quot;cat2 contains Mistigri added in cat1 : &quot;</span> + cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add another cat in the other instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; cat2.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;Felix&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">2</span>, <span class="st0">&quot;cat2 contains Mistigri and Felix&quot;</span> + cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<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; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">2</span>, <span class="st0">&quot;cat1 also contains Mistigri and Felix&quot;</span> + cat1.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Get the <a href="http://github.com/dsimard/jskata_examples/blob/master/singleton/singleton.js" target="_blank">code on GitHub</a> and see it <a href="http://dsimard.github.com/jskata_examples/singleton.html" target="_blank">live in action</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to create an object with private variables and methods</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/</link>
		<comments>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 12:50:52 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128</guid>
		<description><![CDATA[In short, you can use private variables when you return another scope when declaring a class.]]></description>
			<content:encoded><![CDATA[<p>In short, you can use private variables when you return another scope when declaring a class.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Cats<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="kw2">var</span> nameList = <span class="br0">&#91;</span><span class="br0">&#93;</span>; <span class="co1">// private var</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is where you define another scope!</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; add:<span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; nameList.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How does it work?</h3>
<p>The magic lies in creating a different scope at the end of the class definition that does not include private variables. Then, private members are <strong>available in this scope and not outside of it</strong>, thanks to the <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/" target="_blank">power of closures</a>.</p>
<h3>Differences between private and public</h3>
<p>These two classes definition shows the difference between the a class where all members are public versus a class where some members are private.</p>
<p>This is a class where <strong>all members are public</strong>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> PublicCats<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">// This is the list of cat names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">nameList</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// This is a method that I would like to be private but can&#8217;t</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// It returns the last cat of the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">lastCat</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; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">nameList</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">nameList</span>.<span class="me1">length</span><span class="nu0">-1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Return the list of names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">names</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; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">nameList</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add a name to the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">add</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">nameList</span>.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// Return the last cat just added</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">lastCat</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This is the corresponding class where <strong>some members are private</strong>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> PrivateCats<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">// This is the list of cat names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> nameList = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// This is a private method</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> lastCat = <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; &nbsp; <span class="co1">// Note : I don&#8217;t use &quot;this&quot; to access private variables</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// thanks to the power of closures!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> nameList<span class="br0">&#91;</span>nameList.<span class="me1">length</span><span class="nu0">-1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// These are our public methods!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is where we create another scope to</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// avoid external objects to use the private variables.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; add:<span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Note : once again, I don&#8217;t use &quot;this&quot; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// to access the private variables and methods</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; nameList.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> lastCat<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; names:<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; &nbsp; &nbsp; <span class="kw1">return</span> nameList;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In the above code, line 15 makes all the difference between the two classes.</p>
<h3>On GitHub</h3>
<p>Get all the <a href="http://github.com/dsimard/jskata_examples/blob/master/private/private.js">code on GitHub</a> and see it live in action on <a href="http://dsimard.github.com/jskata_examples/private.html">my GitHub page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/feed/</wfw:commentRss>
		<slash:comments>10</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>Easy loop for every element of an array</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/</link>
		<comments>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/#comments</comments>
		<pubDate>Tue, 12 May 2009 15:24:21 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85</guid>
		<description><![CDATA[There's something bugging me with javascript <em>for</em> loop : the extra work to loop though all elements of an array. Suppose I want to <em>alert</em> each element of an array, there are three ways of doing it.]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s something bugging me with javascript <em>for</em> loop : the extra work to loop though all elements of an array. Suppose I want to <em>alert</em> each element of an array, there are three ways of doing it.</p>
<p>First, you loop with an index and assign the element to a variable.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> items = <span class="br0">&#91;</span><span class="st0">&quot;a&quot;</span>, <span class="st0">&quot;b&quot;</span>, <span class="st0">&quot;c&quot;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i = <span class="nu0">0</span>; i &lt; items.<span class="me1">length</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> <span class="kw1">item</span> = items<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>You always have to have that extra assignment at the beginning of the loop.</p>
<p>Second, you loop with a <em>for each</em>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> items = <span class="br0">&#91;</span><span class="st0">&quot;a&quot;</span>, <span class="st0">&quot;b&quot;</span>, <span class="st0">&quot;c&quot;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i <span class="kw1">in</span> items<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> <span class="kw1">item</span> = items<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In javascript, the <em>for each</em> <strong>loops though the index</strong> of the array instead of looping though the elements, like in most other languages. Thus, you also have to make the extra assignment at the beginning of the loop.</p>
<p><strong>The easiest way</strong> of doing it is the third one.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> items = <span class="br0">&#91;</span><span class="st0">&quot;a&quot;</span>, <span class="st0">&quot;b&quot;</span>, <span class="st0">&quot;c&quot;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i = <span class="nu0">0</span>, <span class="kw1">item</span>; <span class="kw1">item</span> = items<span class="br0">&#91;</span>i<span class="br0">&#93;</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">item</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>How does it work?</strong> First it uses multi-assignment on one line. In javascript, you could write <em>var i = 0, j = 1;</em> and it would create two variables, <em>i</em> and <em>j</em>.</p>
<p>The second part of the <em>for</em> is confusing : it assigns the item but it never check if <em>i</em> it is out of bounds of the array (using <em>i < items.length</em>). It works because when javascript tries to assign an item after the end of the array (in our case <em>items[3]</em>), it returns <em>null</em> which is considered by javascript as <em>false</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Be ahead of the trend, try CouchDB</title>
		<link>http://www.javascriptkata.com/2009/01/12/be-ahead-of-the-trend-try-couchdb/</link>
		<comments>http://www.javascriptkata.com/2009/01/12/be-ahead-of-the-trend-try-couchdb/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 15:49:28 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[couchdb]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=79</guid>
		<description><![CDATA[I always loved and hated databases. When I was a younger programmer, I asked to switch to DB administrator and, thank God, they didn&#8217;t agree to let me mutate in a DBA. I ended up fired like 80% of the company (except the DBA). In fact, I wanted to be DBA because I thought I [...]]]></description>
			<content:encoded><![CDATA[<p>I always loved and hated databases. When I was a younger programmer, I asked to switch to DB administrator and, thank God, they didn&#8217;t agree to let me mutate in a DBA. I ended up fired like 80% of the company (except the DBA). In fact, I wanted to be DBA because I thought I knew how to work with a DB better than them. The rest of my life proved me wrong. Nobody knows what they&#8217;re doing because relational databases are a mess.</p>
<p>Then came the non-relational database. <a href="http://aws.amazon.com/simpledb/" target="_blank">Amazon&#8217;s SimpleDB</a> was the first NRDBMS (as opposed to RDBMS) that got a public attention. When I read the specs, I really thought it was cool but I wondered &#8220;Is it scalable?&#8221;. Then I remembered that <strong>there is no such thing as a scalable database</strong>. Databases need to be smashed and hurt in order to be as scalable as we want them to. I wanted to give it a try but I wasn&#8217;t ready to <a href="http://aws.amazon.com/simpledb/#pricing" target="_blank">pay</a> for it yet.</p>
<p>Working with xmpp/jabber, I saw <a href="http://metajack.im/2008/11/14/relax-databases-are-fun-again/" target="_blank">a post by the well-known metajack</a> (his blog is mainly about xmpp) about couchdb and I knew the time as come for me to be different. I had to try it so I opened a terminal and wrote <em>sudo apt-get install couchdb</em>. This was the beginning of a great adventure for me&#8230;</p>
<p>What&#8217;s the link between CouchDB and Javascript? <strong>CouchDB is completly JSON</strong>. You can write <a href="http://jchris.mfdz.com/posts/129" target="_blank">an application that is completly JSON/Ajax</a> without a single line of PHP/Ruby/Python/whatever.</p>
<p>I&#8217;m currently writing a simple app for CouchDB and this is the first of some posts about writing an app with CouchDB. I currently use version 0.8.0 and I ran through a lot of problems and I wish these posts will <strong>help you succeeding in building an application supported by CouchDB without all the hassle I went through</strong>.</p>
<p>Because my main project is <a href="http://www.timmyontime.com/" target="_self">TimmyOnTime</a> (and don&#8217;t forget to check out our new blog <a href="http://behindtheclock.timmyontime.com/" target="_blank">behind the clock</a>), I will work partial-time on this project so it might not go as fast as I (and you) want. But don&#8217;t despair, you will still be ahead of the trend when NRBDMS will go mainstream.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/01/12/be-ahead-of-the-trend-try-couchdb/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cleanest way of executing javascript code on page load</title>
		<link>http://www.javascriptkata.com/2008/12/01/cleanest-way-of-executing-javascript-code-on-page-load/</link>
		<comments>http://www.javascriptkata.com/2008/12/01/cleanest-way-of-executing-javascript-code-on-page-load/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 15:01:08 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=69</guid>
		<description><![CDATA[Everytime that I work on a new project, I'm always unsure about which way to execute javascript on page load.]]></description>
			<content:encoded><![CDATA[<p>Everytime that I work on a new project, I&#8217;m always unsure about which way to execute javascript on page load. These are the different techniques :</p>
<h3>Your old buddy body.onload()</h3>
<p>The safest way to execute javascript when a page loads is to use the body.onLoad method.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;body onload=&quot;doSomething();&quot;&gt;&lt;/body&gt;</div>
</li>
</ol>
</div>
<h3>The document.ready function with jquery</h3>
<p>This technique requires jquery and is similar to the <strong>body.onLoad technique</strong> (see above).</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;body&gt;&lt;script&gt;$(document).ready()&lt;/script&gt;&lt;/body&gt;</div>
</li>
</ol>
</div>
<p>The problem is that I don&#8217;t like that the javascript code is too close to the HTML code. There&#8217;s really something that I don&#8217;t like.</p>
<h3>The #ID.ready</h3>
<p><strong>DO NOT USE!</strong> It doesn&#8217;t work. At first, I tought that this technique worked but in fact, even if the element corresponding to the ID doesn&#8217;t exists, the function is called. To understand it better, I&#8217;ll give you an example. Suppose that I want to load a user list with ajax after the page load and that list correspond to <strong>ul#users</strong>, I&#8217;d write the following code</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;ul#users&quot;</span><span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</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; loadUsersWithAjax<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Unfortunately, it doesn&#8217;t work.</p>
<h3>The document.ready + #ID.each</h3>
<p>After the failure of the #ID.ready technique (see above), I tried a similar thing that is working but ugly looking. Suppose I want to do the same as above, I&#8217;d write this :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</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="br0">&#40;</span><span class="st0">&quot;ul#users&quot;</span><span class="br0">&#41;</span>.<span class="me1">each</span><span class="br0">&#40;</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; &nbsp; &nbsp; loadUsersWithAjax<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>That way, it would execute the loadUsersWithAjax() method for each <strong>ul#users</strong> and because element IDs are unique to a page, the method would be called only once. It looks bad but it&#8217;s the &#8220;prettiest&#8221; way I found of executing javascript code on page load.</p>
<p>Now, I want to know <strong>what would you suggest? Which technique is your?</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/12/01/cleanest-way-of-executing-javascript-code-on-page-load/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>JavascriptKata now ad free</title>
		<link>http://www.javascriptkata.com/2008/11/28/javascriptkata-now-ad-free/</link>
		<comments>http://www.javascriptkata.com/2008/11/28/javascriptkata-now-ad-free/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 17:11:41 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=68</guid>
		<description><![CDATA[[Update 2009-09-20 : I will try to bring back ads. I removed them because I was frustrated, I bring them back as an experience.]
JavascriptKata was created in the blog bubble. In a time when all you had to do was to create a blog, put ads on it and you were rich. It never was [...]]]></description>
			<content:encoded><![CDATA[<p>[Update 2009-09-20 : I will try to bring back ads. I removed them because I was frustrated, I bring them back as an experience.]</p>
<p>JavascriptKata was created in the blog bubble. In a time when <cite>all you had to do was to create a blog, put ads on it and you were rich</cite>. It never was true. The ads on this blog gave me about 100$ in a year so I finally felt discouraged and left it for dead. Nevertheless, this blog as a good share of traffic and more subscribers in the feed than any blog that I&#8217;ve owned/I own/will own. In fact, I call it a blog but it&#8217;s not really a blog. I used wordpress and that&#8217;s the main reason why I&#8217;m calling it a blog.</p>
<p>I tried several things to keep my interest in writing about javascript but they all failed because there was the devil saying &#8220;anyways, you don&#8217;t make any money out of it&#8221;. But there was the angel saying &#8220;maybe you don&#8217;t make money but you like javascript and should continue to write about it&#8221;. As usual, the devil won.</p>
<p>I won&#8217;t try to make another revival of this blog but I&#8217;ll try to write more about little things that happens between me and javascript, about the daily problems that I encounter. I&#8217;ll write a lot about <a href="http://www.jquery.com" target="_blank">jquery</a> too since for me, there can be no javascript without jquery.</p>
<p>Thanks to all of you!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/11/28/javascriptkata-now-ad-free/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Load a page with javascript</title>
		<link>http://www.javascriptkata.com/2008/02/12/load-a-page-with-javascript/</link>
		<comments>http://www.javascriptkata.com/2008/02/12/load-a-page-with-javascript/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 14:31:20 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Ask Dan a javascript question]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/2008/02/12/load-a-page-with-javascript/</guid>
		<description><![CDATA[Someone asked me : "I would like this site to automatically load a certain page (1-31) depending on what day of the month it is (1-31)"]]></description>
			<content:encoded><![CDATA[<p>I receive javascript-related questions in my inbox from time to time and here&#8217;s the last one.</p>
<blockquote><p> I am ok with HTML but awfully new to JavaScript and I know very little at this point. My question is for this web site I&#8217;m building. I need to know the exact JavaScript code and placement of the code in an HTML document for this goal. I would like this site to <strong>automatically load a certain page (1-31) depending on what day of the month it is (1-31)</strong>. I don&#8217;t need to worry about what month or year it is, just the date in the month. For instance when it is the eleventh day in the month, page11.html will automatically load when you click on to the site. I would like it to take the time(Date) from the client side not the server. This way no mater what time zone your in the right HTML page will pop up at midnight.</p></blockquote>
<p>Most people can&#8217;t give the <em>exact javascript code placement of the code in an HTML document</em> because it would always be buggy. They can give hints and/or snippets of code but rarely a complete working piece. Sorry&#8230;</p>
<p>Secondly, relying on javascript to do all this work would be a mistake because <strong>if javascript is disabled, the site will not work</strong>. Most users have javascript enabled but you should always think about web-crawlers (google, yahoo!, etc) that will try to index your page but will hit an empty page.</p>
<p>It&#8217;s always hard to guess all the reasons why someone would want a different page everyday of the month and I don&#8217;t know more about the project than what is written above. Considering this, maybe there are just parts of the page that are changing everyday and you could load them on the server-side thus having a single page loading including other pages depending on the day. The problem is that you would have to ask the timezone of the user and keep it in a cookie.</p>
<p>Just remember, <strong>javascript should not be a requirement to navigate in a site</strong>.</p>
<p>Finally, if I would really want to load a complete new page in javascript, I would do the following.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> dayOfTheMonth = <span class="br0">&#40;</span><span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">getDate</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">location.<span class="me1">href</span>=<span class="st0">&quot;page&quot;</span> + dayOfTheMonth.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span> + <span class="st0">&quot;.html&quot;</span>;</div>
</li>
</ol>
</div>
<p style="margin-bottom: 2em">&nbsp;</p>
<p>This is the way I would do it, <strong>do you have any other suggestions?</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/02/12/load-a-page-with-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to have jQuery and prototype.js in the same project</title>
		<link>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/</link>
		<comments>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 11:02:41 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[librairies]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/</guid>
		<description><![CDATA[I&#8217;m a big fan of jQuery. This librarie is just the best and simplest one around. I really noticed it when I wanted to get rid of jQuery in TimmyOnTime and try to use prototype.js instead, just to be more &#8220;rails-oriented&#8221; (that&#8217;s a pretty lame excuse don&#8217;t you think?)
Why I didn&#8217;t like prototype.js
There was not [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of <a href="http://jquery.com/" target="_blank">jQuery</a>. This librarie is just the <strong>best</strong> and <strong>simplest</strong> one around. I really noticed it when I wanted to get rid of jQuery in <a href="http://timmyontime.com/" target="_blank">TimmyOnTime</a> and try to use <a href="http://www.prototypejs.org/" target="_blank">prototype.js</a> instead, just to be more &#8220;rails-oriented&#8221; (that&#8217;s a pretty lame excuse don&#8217;t you think?)</p>
<h3>Why I didn&#8217;t like prototype.js</h3>
<p>There was not a lot of javascript written for the project so I thought that getting rid of jQuery and using prototype.js would be easy. It turned out to be hell on earth. <strong>Prototype.js made me feel like I was back in the 90s writing C++</strong>. A simple click event turns out to be a awful lot of ugly code with ugly function names.</p>
<p>Example, <strong>if I wanted to show the content of a DIV I clicked</strong> in prototype.js, I would use things like <a href="http://www.prototypejs.org/api/event/observe" target="_blank">Event.observe</a>, <a href="http://www.prototypejs.org/api/function/bindAsEventListener" target="_blank">bindAsEventListener</a>, a mix of native DOM element and prototype.js element, and worst of all, an <a href="http://www.prototypejs.org/api">unintelligible documentation</a>.</p>
<p>jQuery as a complete different way of doing it in a single easy-to-understand line :<br />
<em>$(&#8221;#a_div&#8221;).click(function() { alert($(this).text()); });</em>.<br />
As simple as that!</p>
<h3>The problem</h3>
<p>I wanted to use some interface element in Rails that requires prototype.js so I had to have both librairies. The problem is that there&#8217;s a conflict between them. Prototype.js doesn&#8217;t seem to give a damn about it but jQuery is nicer with you. It offers a <a href="http://docs.jquery.com/Core/jQuery.noConflict" target="_blank">noConflict</a> method.</p>
<p><strong>With Rails</strong>, prototype.js is the default librairie so to override this, you could do</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;%= javascript_include_tag &quot;prototype&quot;, &quot;jquery&quot; %&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;$j = jQuery.noConflict();&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>This way, you will have to use <strong>$j</strong> instead of <strong>$</strong> to call jQuery.<strong>$</strong> would still call prototype.</p>
<p>Another thing you could do is to use <a href="http://ennerchi.com/projects/jrails" target="_blank">jQuery on Rails</a> and don&#8217;t give a damn about scriptaculous. I personally prefer to use librairies that are fully tested instead of using a &#8220;by-pass&#8221; that could break my code. It&#8217;s your choice&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New on Javascript Kata : the echo chamber</title>
		<link>http://www.javascriptkata.com/2008/01/08/new-on-javascript-kata-the-echo-chamber/</link>
		<comments>http://www.javascriptkata.com/2008/01/08/new-on-javascript-kata-the-echo-chamber/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 22:55:55 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/2008/01/08/new-on-javascript-kata-the-echo-chamber/</guid>
		<description><![CDATA[Since the beginning of the new year, I&#8217;m offering to javascripters the echo chamber. What is it? It&#8217;s a category where I will post news about what happens in the javascript world. I will try to avoid ajax related news because Ajaxian already does the job really well.
You can subscribe to the RSS feed with [...]]]></description>
			<content:encoded><![CDATA[<p>Since the beginning of the new year, I&#8217;m offering to javascripters the <a href="http://www.javascriptkata.com/category/echo-chamber/">echo chamber</a>. What is it? It&#8217;s <strong>a category where I will post news about what happens in the javascript world</strong>. I will try to avoid ajax related news because <a href="http://ajaxian.com/" target="_blank">Ajaxian</a> already does the job really well.</p>
<p>You can subscribe to the RSS feed with <a href="http://feeds.feedburner.com/JavascriptKata">all the articles</a> or to the RSS feed<a href="http://feeds.feedburner.com/javascriptkata/no_echo_chamber"> without the articles from the echo chamber</a>.</p>
<p>Some may have noticed that I redesigned the site. The <a href='http://www.javascriptkata.com/wp-content/uploads/2008/01/js_old.jpg' title='Javascript Kata - old design'>old design</a> was long overdue and it was a pleasure for my eyes to have a new one.</p>
<p>I would also like to have your <strong>feedback on what you think of the new echo chamber and design</strong>?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/01/08/new-on-javascript-kata-the-echo-chamber/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
