<?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>dev.enekoalonso.com &#187; javascript</title>
	<atom:link href="http://dev.enekoalonso.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.enekoalonso.com</link>
	<description>having fun with code</description>
	<lastBuildDate>Wed, 12 Oct 2011 21:40:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Command line scripting with Node.js</title>
		<link>http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/</link>
		<comments>http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 21:40:17 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=730</guid>
		<description><![CDATA[I often write custom deploy scripts for the projects I work on. In most cases, I choose python. You can do pretty much everything with shell and system commands, but python dictionaries and lists make it very easy to manipulate data, lists of files, text, etc. Networking is also very easy in python (http, ftp, [...]]]></description>
			<content:encoded><![CDATA[<p>I often write custom deploy scripts for the projects I work on. In most cases, I choose python. You can do pretty much everything with shell and system commands, but python dictionaries and lists make it very easy to manipulate data, lists of files, text, etc. Networking is also very easy in python (http, ftp, etc).</p>
<p>To run a Javascript file from the console you can type &#8220;node myscript.js&#8221;. Or you can set the application that will run the file like you do with bash or python:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">#<span class="sy0">!</span> <span class="re0">/usr/bin/</span>env node</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;Hello&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">setTimeout<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; console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;world!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>, <span class="nu0">2000</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Now you can change the permissions of the script to make it executable:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw2">chmod</span> <span class="nu0">755</span> myscript.js</div>
</li>
</ol>
</div>
<p>And run the script by typing its name, like you do with shell or python scripts:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ .<span class="sy0">/</span>myscript.js param1 param2 <span class="st0">&quot;param 3&quot;</span></div>
</li>
<li class="li1">
<div class="de1">Hello</div>
</li>
<li class="li1">
<div class="de1">world<span class="sy0">!</span></div>
</li>
</ol>
</div>
<p>Nice, uh? Now go and write some cool command line scripts in Javascript :)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>October 5, 2010 -- <a href="http://dev.enekoalonso.com/2010/10/05/javascript-console-in-the-terminal-command-line/" title="Javascript console in the terminal (command line)">Javascript console in the terminal (command line)</a> (1)</li><li>August 6, 2011 -- <a href="http://dev.enekoalonso.com/2011/08/06/python-script-remove-empty-folders/" title="Python script: remove empty folders">Python script: remove empty folders</a> (1)</li><li>June 14, 2011 -- <a href="http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/" title="Titanium Studio and Titanium Mobile 1.7">Titanium Studio and Titanium Mobile 1.7</a> (0)</li><li>May 18, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/" title="location.hash is dead (on html5 browsers)">location.hash is dead (on html5 browsers)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Titanium Studio and Titanium Mobile 1.7</title>
		<link>http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/</link>
		<comments>http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 20:52:24 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[appcelerator]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[studio]]></category>
		<category><![CDATA[titanium]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=706</guid>
		<description><![CDATA[Titanium Studio: will this change anything? It is a big step, and offers something I really missed while working with Titanium in the past: integrated debugging. I think I will rather stay with XCode and code native Objective-C. Link: Introducing Titanium Studio and Titanium Mobile 1.7. Related Posts:August 18, 2010 -- Titanium Developer: love and hate (Part [...]]]></description>
			<content:encoded><![CDATA[<p>Titanium Studio: will this change anything? It is a big step, and offers something I really missed while working with Titanium in the past: integrated debugging. I think I will rather stay with XCode and code native Objective-C.</p>
<p>Link: <a href="http://developer.appcelerator.com/blog/2011/06/introducing-titanium-studio.html">Introducing Titanium Studio and Titanium Mobile 1.7</a>.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/titanium-developer-love-and-hate-part-ii/" title="Titanium Developer: love and hate (Part II)">Titanium Developer: love and hate (Part II)</a> (0)</li><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li><li>July 30, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/" title="Titanium Developer: love and hate">Titanium Developer: love and hate</a> (3)</li><li>August 3, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/03/iphone-sdk-old-versions/" title="List of iPhone SDK &#038; XCode packages">List of iPhone SDK &#038; XCode packages</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>location.hash is dead (on html5 browsers)</title>
		<link>http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/</link>
		<comments>http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/#comments</comments>
		<pubDate>Wed, 18 May 2011 21:49:51 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[docs]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[location]]></category>
		<category><![CDATA[manipulate]]></category>
		<category><![CDATA[mdc]]></category>
		<category><![CDATA[pjax]]></category>
		<category><![CDATA[pushState]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=696</guid>
		<description><![CDATA[Pretty cool technique to actually change the url of the page (something until now impossible to do in Javascript for security reasons): http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate Also good to read, the browser history MDC documentation: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history And finally, pjax, a new script that combines the use oj Ajax calls and this technique to manipulate the url to achieve [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty cool technique to actually change the url of the page (something until now impossible to do in Javascript for security reasons):<br />
<a href="http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate">http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate</a></p>
<p>Also good to read, the browser history MDC documentation:<br />
<a href="https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history">https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history</a></p>
<p>And finally, pjax, a new script that combines the use oj Ajax calls and this technique to manipulate the url to achieve a beautiful ajax navigation experience, with permalinks and without the need of using location.hash.<br />
<a href="http://pjax.heroku.com/">http://pjax.heroku.com/</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 29, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/29/modifying-the-url-hash-without-affecting-the-browser-history/" title="Modifying the URL Hash without affecting the browser history">Modifying the URL Hash without affecting the browser history</a> (0)</li><li>May 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/12/angry-birds-as-a-web-app/" title="Angry Birds as a Web App">Angry Birds as a Web App</a> (0)</li><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li><li>August 27, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/27/link-whirlpool-js/" title="Link: Whirlpool JS">Link: Whirlpool JS</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Angry Birds as a Web App</title>
		<link>http://dev.enekoalonso.com/2011/05/12/angry-birds-as-a-web-app/</link>
		<comments>http://dev.enekoalonso.com/2011/05/12/angry-birds-as-a-web-app/#comments</comments>
		<pubDate>Thu, 12 May 2011 17:40:58 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[angry]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[birds]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[webapp]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=693</guid>
		<description><![CDATA[Really, really cool, Rovio has released Angry Birds (and all the game assets) as a web app, Javascript and Canvas based. I can&#8217;t wait to see clones of Angry Birds with custom assets, maps and sounds. via Angry Birds as a Web App. Related Posts:July 30, 2010 -- Titanium Developer: love and hate (3)May 3, 2010 [...]]]></description>
			<content:encoded><![CDATA[<p>Really, really cool, Rovio has released Angry Birds (and all the game assets) as a web app, Javascript and Canvas based. I can&#8217;t wait to see clones of Angry Birds with custom assets, maps and sounds.</p>
<p>via <a href="http://googlesystem.blogspot.com/2011/05/angry-birds-as-web-app.html">Angry Birds as a Web App</a>.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>July 30, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/" title="Titanium Developer: love and hate">Titanium Developer: love and hate</a> (3)</li><li>May 3, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/03/playing-with-websockets/" title="Playing with WebSockets">Playing with WebSockets</a> (4)</li><li>May 18, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/" title="location.hash is dead (on html5 browsers)">location.hash is dead (on html5 browsers)</a> (0)</li><li>May 6, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/" title="Finding duplicate ID&#8217;s on an HTML page">Finding duplicate ID&#8217;s on an HTML page</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2011/05/12/angry-birds-as-a-web-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding duplicate ID&#8217;s on an HTML page</title>
		<link>http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/</link>
		<comments>http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/#comments</comments>
		<pubDate>Fri, 06 May 2011 17:49:34 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[ids]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[page]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=687</guid>
		<description><![CDATA[Looks like sometimes we forgot element ID&#8217;s are meant to be unique on a HTML page. Here is a little bit of code I just wrote to find duplicate ID&#8217;s on a page (run the code on your browser&#8217;s javascript console): var idList = &#123;&#125;; var nodes = document.getElementsByClassName&#40;&#39;&#39;&#41;; for &#40;var i in nodes&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Looks like sometimes we forgot element ID&#8217;s are meant to be unique on a HTML page. Here is a little bit of code I just wrote to find duplicate ID&#8217;s on a page (run the code on your browser&#8217;s javascript console):</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> idList = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> nodes = document.<span class="me1">getElementsByClassName</span><span class="br0">&#40;</span><span class="st0">&#39;&#39;</span><span class="br0">&#41;</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> nodes<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>isNaN<span class="br0">&#40;</span>i<span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> nodes<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">id</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; idList<span class="br0">&#91;</span>nodes<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">id</span><span class="br0">&#93;</span> = idList<span class="br0">&#91;</span>nodes<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">id</span><span class="br0">&#93;</span>? idList<span class="br0">&#91;</span>nodes<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">id</span><span class="br0">&#93;</span><span class="nu0">+1</span>:<span class="nu0">1</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">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> id <span class="kw1">in</span> idList<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>idList<span class="br0">&#91;</span>id<span class="br0">&#93;</span> <span class="sy0">&gt;</span> <span class="nu0">1</span><span class="br0">&#41;</span> console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;Duplicate id: #&quot;</span> + id<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Have fun!</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>May 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/12/angry-birds-as-a-web-app/" title="Angry Birds as a Web App">Angry Birds as a Web App</a> (0)</li><li>May 14, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/" title="Interesting JSON vulnerability (old stuff)">Interesting JSON vulnerability (old stuff)</a> (0)</li><li>April 14, 2010 -- <a href="http://dev.enekoalonso.com/2010/04/14/getting-unique-timestamps-in-javascript/" title="Getting unique timestamps in Javascript">Getting unique timestamps in Javascript</a> (0)</li><li>March 24, 2010 -- <a href="http://dev.enekoalonso.com/2010/03/24/mooml-1-1-no-more-eval-say-hi-to-mooml-template-and-mooml-templates/" title="Mooml 1.1 &#8211; No more eval(), say Hi to Mooml.Template and Mooml.Templates">Mooml 1.1 &#8211; No more eval(), say Hi to Mooml.Template and Mooml.Templates</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Staying busy</title>
		<link>http://dev.enekoalonso.com/2011/03/14/staying-busy/</link>
		<comments>http://dev.enekoalonso.com/2011/03/14/staying-busy/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 15:43:47 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[agora]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[level]]></category>
		<category><![CDATA[studios]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=666</guid>
		<description><![CDATA[Wow, I just realized my last post here was in October 2010. It&#8217;s been a while. Turns out, October was when we launched the last project I was working on at LEVEL Studios. I am still working on that project, but doing back end stuff in C#.Net. Besides a couple of days working on another [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, I just realized my last post here was in October 2010. It&#8217;s been a while. Turns out, October was when we launched the last project I was working on at LEVEL Studios. I am still working on that project, but doing back end stuff in C#.Net. Besides a couple of days working on another project for Agora Games, I have not touched Javascript at all. Maybe that&#8217;s why I had nothing to write here.</p>
<p>Either way, I have many things and thoughts to share, but not a lot of free time to do it. I&#8217;ll try to keep posting about all the stuff I am working on now.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>May 6, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/06/tech-talks-at-level-studios/" title="Tech talks at LEVEL Studios">Tech talks at LEVEL Studios</a> (0)</li><li>October 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/" title="Command line scripting with Node.js">Command line scripting with Node.js</a> (0)</li><li>June 14, 2011 -- <a href="http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/" title="Titanium Studio and Titanium Mobile 1.7">Titanium Studio and Titanium Mobile 1.7</a> (0)</li><li>May 18, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/" title="location.hash is dead (on html5 browsers)">location.hash is dead (on html5 browsers)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2011/03/14/staying-busy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript console in the terminal (command line)</title>
		<link>http://dev.enekoalonso.com/2010/10/05/javascript-console-in-the-terminal-command-line/</link>
		<comments>http://dev.enekoalonso.com/2010/10/05/javascript-console-in-the-terminal-command-line/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 04:15:19 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[exe]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=672</guid>
		<description><![CDATA[I&#8217;ve been looking for a command line Javascript console for a while. I have even thought of creating my own one, some hybrid between my fake Linux terminal and JSFiddle.net. Lately, I&#8217;ve been working with node.js lately on a little project just for fun. Never before I had done server side Javascript and it looks [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been looking for a command line Javascript console for a while. I have even thought of creating my own one, some hybrid between <a href="http://enekoalonso.com">my fake Linux terminal</a> and <a href="http://www.jsfiddle.net">JSFiddle.net</a>. Lately, I&#8217;ve been working with <a href="http://dev.enekoalonso.com/tag/node-js/">node.js</a> lately on a little project just for fun. Never before I had done server side Javascript and it looks like it has serious potential. But still, I had no console other than Firebug or, my favorite, the one included in Webkit.</p>
<p>So I am very happy after coming across this article which shows <a href="http://webreflection.blogspot.com/2010/10/javascriptcore-via-terminal.html">how to use the JavaScriptCore.Framework included in Mac OS X on the command line</a>. Doing some Google searches, looks like people have been talking about this <a href="http://www.phpied.com/javascript-shell-scripting/">for a long time</a>. Even more! Here is <a href="http://www.phpied.com/make-your-javascript-a-windows-exe/">how to compile javascript to .exe in Windows</a>. Didn&#8217;t even thought that could be possible, being Javascript such a dynamic language.</p>
<p>One interesting thing about the jsc Javascript console is that you can load() javascript files from disk. Thus, one can get the <a href="http://mootools.net/core">Mootools core</a> (Core + Function + Array + Number + String + Hash + Class + Class.Extras + JSON) and load it to start creating awesome classes, etc.</p>
<p>This is going to be a lot of fun :)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>October 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/" title="Command line scripting with Node.js">Command line scripting with Node.js</a> (0)</li><li>July 13, 2011 -- <a href="http://dev.enekoalonso.com/2011/07/13/clone-your-osxterminal-window/" title="Clone your OSX terminal window">Clone your OSX terminal window</a> (1)</li><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li><li>July 30, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/" title="Titanium Developer: love and hate">Titanium Developer: love and hate</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/10/05/javascript-console-in-the-terminal-command-line/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Date from ISO 8601 string</title>
		<link>http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/</link>
		<comments>http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 16:17:34 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[explorer]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[iso]]></category>
		<category><![CDATA[iso8601]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[parts]]></category>
		<category><![CDATA[standard]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=662</guid>
		<description><![CDATA[Javascript is full of surprises and browser compatibility is even more exciting. For some reason, I thought all browsers or Javascript engines had support for creating dates from ISO 8601 strings. Like this: var myDate = new Date&#40;&#34;2010-09-21T02:57:00Z&#34;&#41;; console.log&#40;myDate&#41;; // Mon Sep 20 2010 19:57:00 GMT-0700 (Pacific Daylight Time) Turns out that does not work [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript is full of surprises and browser compatibility is even more exciting. For some reason, I thought all browsers or Javascript engines had support for creating dates from ISO 8601 strings. Like this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> myDate = <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="st0">&quot;2010-09-21T02:57:00Z&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span>myDate<span class="br0">&#41;</span>; <span class="co1">// Mon Sep 20 2010 19:57:00 GMT-0700 (Pacific Daylight Time)</span></div>
</li>
</ol>
</div>
<p>Turns out that does not work on Internet Explorer (tested on IE8). It didn&#8217;t work in Titanium Developer either, when I was working on an iPhone/Android app a couple of weeks ago. Here is the function I used to load dates from ISO 8601 strings:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> dateFromISO8601<span class="br0">&#40;</span>isostr<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">var</span> parts = isostr.<span class="me1">match</span><span class="br0">&#40;</span><span class="re0">/\d+/g</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">return</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span>parts<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, parts<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &#8211; <span class="nu0">1</span>, parts<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>, parts<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>, parts<span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span>, parts<span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> myDate = dateFromISO8601<span class="br0">&#40;</span><span class="st0">&quot;2010-09-21T02:57:00Z&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span>myDate<span class="br0">&#41;</span>; <span class="co1">// Mon Sep 20 2010 19:57:00 GMT-0700 (Pacific Daylight Time)</span></div>
</li>
</ol>
</div>
<p>That should work fine everywhere :)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>June 14, 2011 -- <a href="http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/" title="Titanium Studio and Titanium Mobile 1.7">Titanium Studio and Titanium Mobile 1.7</a> (0)</li><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li><li>July 30, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/" title="Titanium Developer: love and hate">Titanium Developer: love and hate</a> (3)</li><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/titanium-developer-love-and-hate-part-ii/" title="Titanium Developer: love and hate (Part II)">Titanium Developer: love and hate (Part II)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MooTools Core for Server and Mobile Development</title>
		<link>http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/</link>
		<comments>http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 17:50:30 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[appcelerator]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[phone]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=655</guid>
		<description><![CDATA[One of the best things of MooTools is that it is very modular, from the core itself. This means you don&#8217;t need to include all core elements in your projects if you don&#8217;t want or don&#8217;t need some modules. For example, you may not want to include Array or Function, if you don&#8217;t need any [...]]]></description>
			<content:encoded><![CDATA[<p>One of the best things of MooTools is that it is very modular, from the core itself. This means you don&#8217;t need to include all core elements in your projects if you don&#8217;t want or don&#8217;t need some modules. For example, you may not want to include Array or Function, if you don&#8217;t need any enhancements of such objects.</p>
<p>In the case of server side or mobile applications (<a href="http://www.appcelerator.com/">Titanium Developer</a> and other Javascript based mobile frameworks), there is no browser involved which means there is no DOM. Thus, we can removed any kind of module that depend or are designed to work with this elements: Browser, Element, Selectors, Cookie&#8230; all those modules have no value for server side or mobile applications.</p>
<p>I found this page which summarizes the modules you should need for server or mobile development:</p>
<blockquote><p>
<strong><a href="http://mootools.net/core">MooTools Core</a></strong><br />
Core: core<br />
Native: Array, Function, Number, String, Hash<br />
Class: Class, Class.Extras</p>
<p><strong><a href="http://mootools.net/more">MooTools More</a></strong><br />
Core: More<br />
Class: Class.Refactor, Class.Binds, Class.Occludes
</p></blockquote>
<p><a href="http://onecrayon.com/spice/docs/mootools-server/">http://onecrayon.com/spice/docs/mootools-server/</a></p>
<p>Personally, I don&#8217;t find the MooTools More modules very useful, so I&#8217;m not including them in my projects. Maybe Binds would be a good addition, but it is not critical.</p>
<p>Having MooTools in a Javascript application makes it easier to develop and brings you all the power of Object Oriented programming with classes, inheritance and more.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>July 30, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/" title="Titanium Developer: love and hate">Titanium Developer: love and hate</a> (3)</li><li>June 14, 2011 -- <a href="http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/" title="Titanium Studio and Titanium Mobile 1.7">Titanium Studio and Titanium Mobile 1.7</a> (0)</li><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/titanium-developer-love-and-hate-part-ii/" title="Titanium Developer: love and hate (Part II)">Titanium Developer: love and hate (Part II)</a> (0)</li><li>September 21, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/" title="Date from ISO 8601 string">Date from ISO 8601 string</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Link: Whirlpool JS</title>
		<link>http://dev.enekoalonso.com/2010/08/27/link-whirlpool-js/</link>
		<comments>http://dev.enekoalonso.com/2010/08/27/link-whirlpool-js/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 20:08:25 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[compressed]]></category>
		<category><![CDATA[crypto]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[encode]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[minified]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[whirlpool]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=651</guid>
		<description><![CDATA[Whirlpool is a very strong hashing algorithm (one way encryption) that encodes any text into a 128 character string (popular md5 generates a 32 byte string). Looking for the JavaScript version, I had to search on my email inbox for an email I received back in October 2009 to found the link where to download [...]]]></description>
			<content:encoded><![CDATA[<p>Whirlpool is a very strong hashing algorithm (one way encryption) that encodes any text into a 128 character string (popular md5 generates a 32 byte string). </p>
<p>Looking for the JavaScript version, I had to search on my email inbox for an email I received back in October 2009 to found the link where to download the Whirlpool Hash Javascript Algorithm. It&#8217;s kind of surprising that searching in Google for &#8220;whirlpool js&#8221;, &#8220;whirlpool javascript&#8221; or &#8220;whirlpool hash javascript algorithm&#8221; does not return the link.</p>
<p>For the record, here is the url:<br />
<a href="http://www.sunsean.com/Whirlpool.html">Whirlpool Hash Javascript Algorithm</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>May 18, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/" title="location.hash is dead (on html5 browsers)">location.hash is dead (on html5 browsers)</a> (0)</li><li>May 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/12/angry-birds-as-a-web-app/" title="Angry Birds as a Web App">Angry Birds as a Web App</a> (0)</li><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/titanium-developer-love-and-hate-part-ii/" title="Titanium Developer: love and hate (Part II)">Titanium Developer: love and hate (Part II)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/08/27/link-whirlpool-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Little tricks: repeating strings in Javascript &amp; Python</title>
		<link>http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/</link>
		<comments>http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 19:17:34 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[repeat]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=641</guid>
		<description><![CDATA[How many times have you found yourself printing strings like &#8220;&#8212;&#8212;&#8212;&#8212;-&#8221; or &#8220;===========&#8221;? I do that a lot when I have to do console.logs or if I&#8217;m working on a console application or script. Adding dividers to the output makes it more readable. Python has a very peculiar syntax for repeating strings, which consists in [...]]]></description>
			<content:encoded><![CDATA[<p>How many times have you found yourself printing strings like &#8220;&#8212;&#8212;&#8212;&#8212;-&#8221; or &#8220;===========&#8221;? I do that a lot when I have to do console.logs or if I&#8217;m working on a console application or script. Adding dividers to the output makes it more readable.</p>
<p>Python has a very peculiar syntax for repeating strings, which consists in just multiplying the string by a number, like this:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="st0">&quot;123&quot;</span><span class="sy0">*</span><span class="nu0">4</span> // returns <span class="st0">&quot;123123123123&quot;</span></div>
</li>
</ol>
</div>
<p>We can achieve the same in Javascript using Array.join() like this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">Array<span class="br0">&#40;</span><span class="nu0">4</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;123&quot;</span><span class="br0">&#41;</span>; <span class="co1">// returns &quot;123123123123&quot;</span></div>
</li>
<li class="li1">
<div class="de1">Array<span class="br0">&#40;</span><span class="nu0">30</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;-&quot;</span><span class="br0">&#41;</span>; <span class="co1">// returns &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot;</span></div>
</li>
<li class="li1">
<div class="de1">Array<span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&lt;span&gt;test&lt;/span&gt;&quot;</span><span class="br0">&#41;</span>; <span class="co1">// returns &quot;&lt;span&gt;test&lt;/span&gt;&lt;span&gt;test&lt;/span&gt;&quot;</span></div>
</li>
</ol>
</div>
<p>Very useful for logs or to generate repeated html strings, etc.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>July 1, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/" title="Little tricks: editing strings by index in Javascript and Python">Little tricks: editing strings by index in Javascript and Python</a> (1)</li><li>August 12, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/12/little-tricks-concatenating-arrays-in-javascript/" title="Little tricks: concatenating arrays in Javascript">Little tricks: concatenating arrays in Javascript</a> (4)</li><li>July 20, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/" title="Little tricks: string padding in Javascript">Little tricks: string padding in Javascript</a> (3)</li><li>May 22, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/" title="More WebSockets, now with Python!">More WebSockets, now with Python!</a> (9)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Little tricks: concatenating arrays in Javascript</title>
		<link>http://dev.enekoalonso.com/2010/08/12/little-tricks-concatenating-arrays-in-javascript/</link>
		<comments>http://dev.enekoalonso.com/2010/08/12/little-tricks-concatenating-arrays-in-javascript/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 21:44:30 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[append]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[native]]></category>
		<category><![CDATA[push]]></category>
		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=632</guid>
		<description><![CDATA[It happens that when working with Javascript frameworks like MooTools for a long time, it&#8217;s easy to forget how to do things in plain/native Javascript. For example, concatenating arrays is an easy task thanks to the Function object method apply: var array1 = &#91;1,2,3,4&#93;; var array2 = &#91;5,6,7&#93;; array1.push.apply&#40;array1, array2&#41;; // array1 = [1, 2, [...]]]></description>
			<content:encoded><![CDATA[<p>It happens that when working with Javascript frameworks like MooTools for a long time, it&#8217;s easy to forget how to do things in plain/native Javascript. For example, concatenating arrays is an easy task thanks to the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function object</a> method <b>apply</b>:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> array1 = <span class="br0">&#91;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> array2 = <span class="br0">&#91;</span><span class="nu0">5</span>,<span class="nu0">6</span>,<span class="nu0">7</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">array1.<span class="me1">push</span>.<span class="me1">apply</span><span class="br0">&#40;</span>array1, array2<span class="br0">&#41;</span>; <span class="co1">// array1 = [1, 2, 3, 4, 5, 6, 7]</span></div>
</li>
<li class="li1">
<div class="de1">array1.<span class="me1">push</span>.<span class="me1">apply</span><span class="br0">&#40;</span>array1, <span class="br0">&#91;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>; <span class="co1">// array1 = [1, 2, 3, 4, 5, 6, 7, 1, 2, 3]</span></div>
</li>
</ol>
</div>
<p>Note that this method <b>concatenates two arrays</b>, and does not check for duplicate items.</p>
<p>In MooTools, the equivalent to this method would be <a href="http://mootools.net/docs/core/Native/Array#Array:extend">Array.extend()</a>:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> array1 = <span class="br0">&#91;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> array2 = <span class="br0">&#91;</span><span class="nu0">5</span>,<span class="nu0">6</span>,<span class="nu0">7</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">array1.<span class="me1">extend</span><span class="br0">&#40;</span>array2<span class="br0">&#41;</span>; <span class="co1">// array1 = [1, 2, 3, 4, 5, 6, 7]</span></div>
</li>
<li class="li1">
<div class="de1">array1.<span class="me1">extend</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>; <span class="co1">// array1 = [1, 2, 3, 4, 5, 6, 7, 1, 2, 3]</span></div>
</li>
</ol>
</div>
<p>Again, Array.extend() concatenates two arrays without checking for duplicates.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/" title="Little tricks: repeating strings in Javascript &#038; Python">Little tricks: repeating strings in Javascript &#038; Python</a> (1)</li><li>May 14, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/" title="Interesting JSON vulnerability (old stuff)">Interesting JSON vulnerability (old stuff)</a> (0)</li><li>October 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/" title="Command line scripting with Node.js">Command line scripting with Node.js</a> (0)</li><li>June 14, 2011 -- <a href="http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/" title="Titanium Studio and Titanium Mobile 1.7">Titanium Studio and Titanium Mobile 1.7</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/08/12/little-tricks-concatenating-arrays-in-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Titanium Developer: love and hate</title>
		<link>http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/</link>
		<comments>http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 04:57:16 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[appcelerator]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[pc]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=615</guid>
		<description><![CDATA[I&#8217;ve been using Titanium Developer for a while now, since I installed it to do some mobile app research one or two months ago. But up until now I barely used it again other than to maintain a desktop app I created for a game, which thanks to Titanium runs in both Mac and PC. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <a href="http://www.appcelerator.com/products/titanium-mobile-application-development/">Titanium Developer</a> for a while now, since I installed it to do some mobile app research one or two months ago. But up until now I barely used it again other than to maintain a desktop app I created for a game, which thanks to Titanium runs in both Mac and PC. Thus, up until this week, I hadn&#8217;t really get in depth with it.</p>
<h3>The good things</h3>
<p>Nice, easy and fast. Creating apps is piece of cake, loading JSON feeds from Internet, using local storage, placing buttons and labels on the screen&#8230; it&#8217;s all easy and it works on both iPhone and Android right away.</p>
<h3>The bad things</h3>
<p>Titanium Developer looks like an unfinished app. It shouldn&#8217;t be version 1.X, not even 0.X. Looks like an alpha, buggy and very easy to get screwed up, specially if you share your projects between Windows and Mac. This time it launches the emulator, this time it does not. This time your project does not load properly. Next time, you try to create a new project and it fails. And the errors are totally useless. Not very helpful.</p>
<h3>Conclusion</h3>
<p>The idea is great, awesome. The code is clean and using Javascript for development makes it easy for web developers to get easily involved into mobile app development. If only the <a href="http://twitter.com/appcelerator">Appcelerator</a> team could improve their app, we would save a lot of headaches.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/titanium-developer-love-and-hate-part-ii/" title="Titanium Developer: love and hate (Part II)">Titanium Developer: love and hate (Part II)</a> (0)</li><li>June 14, 2011 -- <a href="http://dev.enekoalonso.com/2011/06/14/titanium-studio-and-titanium-mobile-1-7/" title="Titanium Studio and Titanium Mobile 1.7">Titanium Studio and Titanium Mobile 1.7</a> (0)</li><li>August 3, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/03/iphone-sdk-old-versions/" title="List of iPhone SDK &#038; XCode packages">List of iPhone SDK &#038; XCode packages</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/07/30/titanium-developer-love-and-hate/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Little tricks: string padding in Javascript</title>
		<link>http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/</link>
		<comments>http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 22:35:45 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[pad]]></category>
		<category><![CDATA[padding]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[zeropad]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=611</guid>
		<description><![CDATA[I just found this little trick to zero pad numbers in Javascript. It is also applicable to padding with any character, not just zeros. var n = 123 String&#40;&#34;00000&#34; + n&#41;.slice&#40;-5&#41;; // returns 00123 &#40;&#34;00000&#34; + n&#41;.slice&#40;-5&#41;; // returns 00123 &#40;&#34; &#160; &#160; &#34; + n&#41;.slice&#40;-5&#41;; // returns &#34; &#160;123&#34; (with two spaces) Found here. [...]]]></description>
			<content:encoded><![CDATA[<p>I just found this little trick to zero pad numbers in Javascript. It is also applicable to padding with any character, not just zeros.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> n = <span class="nu0">123</span></div>
</li>
<li class="li1">
<div class="de1">String<span class="br0">&#40;</span><span class="st0">&quot;00000&quot;</span> + n<span class="br0">&#41;</span>.<span class="me1">slice</span><span class="br0">&#40;</span><span class="nu0">-5</span><span class="br0">&#41;</span>; <span class="co1">// returns 00123</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="st0">&quot;00000&quot;</span> + n<span class="br0">&#41;</span>.<span class="me1">slice</span><span class="br0">&#40;</span><span class="nu0">-5</span><span class="br0">&#41;</span>; <span class="co1">// returns 00123</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="st0">&quot; &nbsp; &nbsp; &quot;</span> + n<span class="br0">&#41;</span>.<span class="me1">slice</span><span class="br0">&#40;</span><span class="nu0">-5</span><span class="br0">&#41;</span>; <span class="co1">// returns &quot; &nbsp;123&quot; (with two spaces)</span></div>
</li>
</ol>
</div>
<p>Found <a href="http://ricochen.wordpress.com/2010/04/24/javascript-zero-padding-numbers/">here</a>.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/" title="Little tricks: repeating strings in Javascript &#038; Python">Little tricks: repeating strings in Javascript &#038; Python</a> (1)</li><li>July 2, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/02/equivalent-of-parseint-in-freemarker/" title="Equivalent of parseInt() in Freemarker">Equivalent of parseInt() in Freemarker</a> (0)</li><li>July 1, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/" title="Little tricks: editing strings by index in Javascript and Python">Little tricks: editing strings by index in Javascript and Python</a> (1)</li><li>September 2, 2009 -- <a href="http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/" title="Creating HTML blocks with Mootools">Creating HTML blocks with Mootools</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Little tricks: editing strings by index in Javascript and Python</title>
		<link>http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/</link>
		<comments>http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 16:57:04 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[edit]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[readonly]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=591</guid>
		<description><![CDATA[Editing strings by index maybe something that we don&#8217;t do all the time. But it&#8217;s one of these things that, coming from languages like C, one would assume is as trivial as assigning the value of an indexed position. Something like this: var a = &#34;hello world&#34; a&#91;0&#93; = &#34;H&#34; console.log&#40;a&#41; // outputs &#34;hello world&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Editing strings by index maybe something that we don&#8217;t do all the time. But it&#8217;s one of these things that, coming from languages like C, one would assume is as trivial as assigning the value of an indexed position. Something like this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> a = <span class="st0">&quot;hello world&quot;</span></div>
</li>
<li class="li1">
<div class="de1">a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = <span class="st0">&quot;H&quot;</span></div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span> <span class="co1">// outputs &quot;hello world&quot;</span></div>
</li>
</ol>
</div>
<p>Go ahead and paste that code in your favorite javascript console. You would expect the output to be &#8220;Hello there&#8221;, but it is not.<br />
Same goes with Python, check this code:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a = <span class="st0">&quot;hello world&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = <span class="st0">&quot;H&quot;</span></div>
</li>
<li class="li1">
<div class="de1">Traceback <span class="br0">&#40;</span>most recent call last<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; File <span class="st0">&quot;&lt;stdin&gt;&quot;</span>, line <span class="nu0">1</span>, <span class="kw1">in</span> <span class="sy0">&lt;</span>module<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">TypeError</span>: <span class="st0">&#39;str&#39;</span> <span class="kw2">object</span> does <span class="kw1">not</span> support item assignment</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = <span class="st0">&#39;H&#39;</span></div>
</li>
<li class="li1">
<div class="de1">Traceback <span class="br0">&#40;</span>most recent call last<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; File <span class="st0">&quot;&lt;stdin&gt;&quot;</span>, line <span class="nu0">1</span>, <span class="kw1">in</span> <span class="sy0">&lt;</span>module<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">TypeError</span>: <span class="st0">&#39;str&#39;</span> <span class="kw2">object</span> does <span class="kw1">not</span> support item assignment</div>
</li>
</ol>
</div>
<p>This is because strings are read only and cannot be modified at all. Only replaced with new strings.</p>
<h2>The solution: convert to array and back to string</h2>
<p>There may be other solutions out there, I&#8217;m sure, but this is the solution I use: Both in Python and Javascript, a string can be converted into an array or list of characters (actually, list of strings). Then we can modify values by index and finally join the output into a new string.</p>
<p>Javascript:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> a = <span class="st0">&quot;hello world&quot;</span></div>
</li>
<li class="li1">
<div class="de1">a = a.<span class="me1">split</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">a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = <span class="st0">&quot;H&quot;</span></div>
</li>
<li class="li1">
<div class="de1">a = a.<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">console.<span class="me1">log</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span> <span class="co1">// outputs &quot;Hello world&quot;</span></div>
</li>
</ol>
</div>
<p>Python:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a = <span class="st0">&quot;hello world&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a = <span class="kw2">list</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = <span class="st0">&quot;H&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a = <span class="st0">&quot;&quot;</span>.<span class="me1">join</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> a</div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&#39;Hello world&#39;</span></div>
</li>
</ol>
</div>
<p>It&#8217;s interesting to see how the syntax for joining arrays into strings is totally the opposite in Javascript and Python. In Javascript, <a href="https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Array_Methods">join()</a> is a function of the Array object while in Python <a href="http://docs.python.org/py3k/library/stdtypes.html?highlight=join#str.join">join()</a> is a function of the string class.</p>
<p>In the other hand, <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/split">split()</a> is a method of the String object in Javascript. There is a <a href="http://docs.python.org/py3k/library/stdtypes.html?highlight=split#str.split">split()</a> function on the string class in Python, but it cannot be used to split strings without a specific separator. The <a href="http://docs.python.org/py3k/library/functions.html?highlight=list#list">list()</a> function/constructor does the trick.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/" title="Little tricks: repeating strings in Javascript &#038; Python">Little tricks: repeating strings in Javascript &#038; Python</a> (1)</li><li>July 20, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/" title="Little tricks: string padding in Javascript">Little tricks: string padding in Javascript</a> (3)</li><li>May 22, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/" title="More WebSockets, now with Python!">More WebSockets, now with Python!</a> (9)</li><li>September 2, 2009 -- <a href="http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/" title="Creating HTML blocks with Mootools">Creating HTML blocks with Mootools</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More WebSockets, now with Python!</title>
		<link>http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/</link>
		<comments>http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/#comments</comments>
		<pubDate>Sun, 23 May 2010 06:41:27 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[websocket]]></category>
		<category><![CDATA[websockets]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=576</guid>
		<description><![CDATA[A couple of weeks ago, Tim and I worked on a little game/demo using WebSockets and C# (I haven&#8217;t been able to put it online since I do not have a Windows server). It was a lot of fun and we were able to see the potential of WebSockets and how much internet can will [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, <a href="http://twitter.com/SkullDuggerT">Tim</a> and I worked on a <a href="http://dev.enekoalonso.com/2010/05/03/playing-with-websockets/">little game/demo using WebSockets and C#</a> (I haven&#8217;t been able to put it online since I do not have a Windows server). It was a lot of fun and we were able to see the potential of WebSockets and how much internet <del datetime="2010-05-23T06:09:54+00:00">can</del> will change in the next few years because of them.</p>
<p>Today I spent a couple of hours researching and implementing a WebSocket solution that I could run in my linux servers, and I found this very nice tutorial for creating a Python WebSockets server: <a href="http://yz.mit.edu/wp/web-sockets-tutorial-with-simple-python-server/">http://yz.mit.edu/wp/web-sockets-tutorial-with-simple-python-server/</a></p>
<p>So I took that code and modified to create a very, very basic chat, that broadcasts received messages to all connected clients. You can try it here (as long as the python server is still up): <a href="http://enekoalonso.com/research/html5/websockets/python/">http://enekoalonso.com/research/html5/websockets/python/</a></p>
<p>Here is the client code:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">var</span> form = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&#39;form&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">var</span> msg = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&#39;msg&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">var</span> output = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&#39;serveroutput&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">var</span> s = <span class="kw2">new</span> WebSocket<span class="br0">&#40;</span><span class="st0">&quot;ws://&quot;</span>+window.<span class="me1">location</span>.<span class="me1">hostname</span>+<span class="st0">&quot;:9876/&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;s.<span class="me1">onopen</span> = <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;opened&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; out<span class="br0">&#40;</span><span class="st0">&#39;Connected.&#39;</span><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">&nbsp;s.<span class="me1">onclose</span> = <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;closed&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; out<span class="br0">&#40;</span><span class="st0">&#39;Connection closed.&#39;</span><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">&nbsp;s.<span class="me1">onmessage</span> = <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;got: &quot;</span> + e.<span class="me1">data</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; out<span class="br0">&#40;</span>e.<span class="me1">data</span><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">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;form.<span class="me1">onsubmit</span> = <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; e.<span class="me1">preventDefault</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; msg.<span class="me1">value</span> = <span class="st0">&#39;&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; window.<span class="me1">scrollTop</span> = window.<span class="me1">scrollHeight</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="kw2">function</span> sendMsg<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; s.<span class="me1">send</span><span class="br0">&#40;</span>msg.<span class="me1">value</span><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">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">function</span> out<span class="br0">&#40;</span>text<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> el = document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#39;p&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; el.<span class="me1">innerHTML</span> = text;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; output.<span class="me1">appendChild</span><span class="br0">&#40;</span>el<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">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;msg.<span class="kw3">focus</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And the server code:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="co1">#!/usr/bin/env python</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">import</span> <span class="kw3">socket</span>, <span class="kw3">threading</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1"># httphost = &quot;localhost:8888&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1"># sockethost = &quot;localhost:9876&quot;</span></div>
</li>
<li class="li1">
<div class="de1">httphost = <span class="st0">&quot;enekoalonso.com&quot;</span></div>
</li>
<li class="li1">
<div class="de1">sockethost = <span class="st0">&quot;enekoalonso.com:9876&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> handle<span class="br0">&#40;</span>s, addr<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; handsake = <span class="st0">&quot;HTTP/1.1 101 Web Socket Protocol Handshake<span class="es0">\r</span><span class="es0">\n</span>Upgrade: WebSocket<span class="es0">\r</span><span class="es0">\n</span>Connection: Upgrade<span class="es0">\r</span><span class="es0">\n</span>WebSocket-Origin: http://%s<span class="es0">\r</span><span class="es0">\n</span>WebSocket-Location: ws://%s/<span class="es0">\r</span><span class="es0">\n</span>WebSocket-Protocol: sample<span class="es0">\r</span><span class="es0">\n</span><span class="es0">\r</span><span class="es0">\n</span>&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>httphost, sockethost<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; s.<span class="me1">send</span><span class="br0">&#40;</span>handsake<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; data = s.<span class="me1">recv</span><span class="br0">&#40;</span><span class="nu0">1024</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; lock = <span class="kw3">threading</span>.<span class="me1">Lock</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">while</span> <span class="nu0">1</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; data = s.<span class="me1">recv</span><span class="br0">&#40;</span><span class="nu0">1024</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw1">not</span> data: <span class="kw1">break</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#39;Data from&#39;</span>, addr, <span class="st0">&#39;:&#39;</span>, data</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; lock.<span class="me1">acquire</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#91;</span>conn.<span class="me1">send</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span> <span class="kw1">for</span> conn <span class="kw1">in</span> clients<span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; lock.<span class="me1">release</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">print</span> <span class="st0">&#39;Client closed:&#39;</span>, addr</div>
</li>
<li class="li1">
<div class="de1">&nbsp; lock.<span class="me1">acquire</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; clients.<span class="me1">remove</span><span class="br0">&#40;</span>s<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; lock.<span class="me1">release</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; s.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> start_server<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp;s = <span class="kw3">socket</span>.<span class="kw3">socket</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;s.<span class="me1">setsockopt</span><span class="br0">&#40;</span><span class="kw3">socket</span>.<span class="me1">SOL_SOCKET</span>, <span class="kw3">socket</span>.<span class="me1">SO_REUSEADDR</span>, <span class="nu0">1</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;s.<span class="me1">bind</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="st0">&#39;&#39;</span>, <span class="nu0">9876</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;s.<span class="me1">listen</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">while</span> <span class="nu0">1</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;conn, addr = s.<span class="me1">accept</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">print</span> <span class="st0">&#39;Connected by&#39;</span>, addr</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;clients.<span class="me1">append</span><span class="br0">&#40;</span>conn<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw3">threading</span>.<span class="me1">Thread</span><span class="br0">&#40;</span>target = handle, args = <span class="br0">&#40;</span>conn,addr<span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">clients = <span class="br0">&#91;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">start_server<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Check the live demo (be aware WebSockets are only supported by Google Chrome right now): <a href="http://enekoalonso.com/research/html5/websockets/python/">http://enekoalonso.com/research/html5/websockets/python/</a><br />
Open a few tabs and send some text. You should see it appearing in all of them. It will also work if you open the page in multiple browser windows or even if you open the page in different computers.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>May 3, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/03/playing-with-websockets/" title="Playing with WebSockets">Playing with WebSockets</a> (4)</li><li>May 6, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/06/tech-talks-at-level-studios/" title="Tech talks at LEVEL Studios">Tech talks at LEVEL Studios</a> (0)</li><li>February 19, 2010 -- <a href="http://dev.enekoalonso.com/2010/02/19/having-fun-with-mooml-and-twitter/" title="Having fun with Mooml &#038; Twitter">Having fun with Mooml &#038; Twitter</a> (0)</li><li>May 18, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/18/location-hash-is-dead-on-html5-browsers/" title="location.hash is dead (on html5 browsers)">location.hash is dead (on html5 browsers)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Performance matters</title>
		<link>http://dev.enekoalonso.com/2010/05/16/performance-matters/</link>
		<comments>http://dev.enekoalonso.com/2010/05/16/performance-matters/#comments</comments>
		<pubDate>Sun, 16 May 2010 18:36:24 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[checking]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[parameter]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[runtime]]></category>
		<category><![CDATA[value]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=543</guid>
		<description><![CDATA[I just read this on an email from a Google&#8217;s Closure developer: Adding runtime checks for invalid usage is something we have policy against. It adds to the code size as well as to the runtime cost. I totally agree. Developers should be responsible for their usage of APIs or third party libraries, while these [...]]]></description>
			<content:encoded><![CDATA[<p>I just read this on an email <a href="http://groups.google.com/group/closure-library-discuss/msg/15fae0a2aee76721">from a Google&#8217;s Closure developer</a>:</p>
<blockquote><p>Adding runtime checks for invalid usage is something we have policy against. It adds to the code size as well as to the runtime cost.</p></blockquote>
<p>I totally agree. Developers should be responsible for their usage of APIs or third party libraries, while these should focus on working as efficient as possible given the correct parameters. Being Javascript a non-strongly-typed language, checking the parameters passed to every function are right at runtime would be a huge overkill.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>March 15, 2010 -- <a href="http://dev.enekoalonso.com/2010/03/15/more-updates-to-mooml-coming-soon/" title="More updates to Mooml coming soon">More updates to Mooml coming soon</a> (0)</li><li>January 28, 2010 -- <a href="http://dev.enekoalonso.com/2010/01/28/dojo-y-yo/" title="Dojo y yo">Dojo y yo</a> (0)</li><li>May 6, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/06/tech-talks-at-level-studios/" title="Tech talks at LEVEL Studios">Tech talks at LEVEL Studios</a> (0)</li><li>January 28, 2010 -- <a href="http://dev.enekoalonso.com/2010/01/28/things-to-do/" title="Things to do">Things to do</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/05/16/performance-matters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Interesting JSON vulnerability (old stuff)</title>
		<link>http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/</link>
		<comments>http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/#comments</comments>
		<pubDate>Fri, 14 May 2010 21:00:56 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[issue]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[vulnerability]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=532</guid>
		<description><![CDATA[Somebody at work passed this article about an interesting vulnerability in web APIs that use JSON. The exploit combines Cross Site Request Forgery (CSRF) with a JSON Array hack allowing an evil site to grab sensitive user data from an unsuspecting user. The hack involves redefining the Array constructor, which is totally legal in Javascript. http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx [...]]]></description>
			<content:encoded><![CDATA[<p>Somebody at work passed this article about an interesting vulnerability in web APIs that use JSON.</p>
<blockquote><p>The exploit combines <a title="Cross Site Request Forgery" href="http://www.codinghorror.com/blog/archives/001175.html">Cross Site Request Forgery</a> (CSRF) with a JSON Array hack allowing an evil site to grab sensitive user data from an unsuspecting user. The hack involves redefining the Array constructor, which is totally legal in Javascript.</p></blockquote>
<p><a href="http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx">http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx</a></p>
<p>I&#8217;ve found a while ago that you can put pretty much any URL on a script tag and the browser will download the content right away, whether it&#8217;s javascript or not, as long as the type attribute in the tag is set to &#8216;text/javascript&#8217;.  But although browsers download the URL content and place it inside the script tag, you can&#8217;t get to the content from Javascript (using innerHTML, for instance). Or can you? Apparently, Firebug can, at least on the HTML tab. I&#8217;m not sure if Firebug has direct access to the DOM, other than through Javascript, but I hope that is the case.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>May 6, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/" title="Finding duplicate ID&#8217;s on an HTML page">Finding duplicate ID&#8217;s on an HTML page</a> (0)</li><li>August 12, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/12/little-tricks-concatenating-arrays-in-javascript/" title="Little tricks: concatenating arrays in Javascript">Little tricks: concatenating arrays in Javascript</a> (4)</li><li>February 19, 2010 -- <a href="http://dev.enekoalonso.com/2010/02/19/having-fun-with-mooml-and-twitter/" title="Having fun with Mooml &#038; Twitter">Having fun with Mooml &#038; Twitter</a> (0)</li><li>October 12, 2011 -- <a href="http://dev.enekoalonso.com/2011/10/12/command-line-scripting-with-node-js/" title="Command line scripting with Node.js">Command line scripting with Node.js</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mooml 1.2.3 &#8211; Bye, bye, with()</title>
		<link>http://dev.enekoalonso.com/2010/04/28/mooml-1-2-3-bye-bye-with/</link>
		<comments>http://dev.enekoalonso.com/2010/04/28/mooml-1-2-3-bye-bye-with/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 04:26:54 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mooml]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[version]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=517</guid>
		<description><![CDATA[Mooml 1.1 was the first version of Mooml that didn&#8217;t rely on eval(), a javascript function that causes lot&#8217;s of problems when not used properly, but that also prevent minimizers and compilers like Closure to generate valid code. The new Mooml 1.2.3 release also gets rid of with(), which will no longer be used to [...]]]></description>
			<content:encoded><![CDATA[<p>Mooml 1.1 was the first version of Mooml that didn&#8217;t rely on eval(), a javascript function that causes lot&#8217;s of problems when not used properly, but that also prevent minimizers and compilers like Closure to generate valid code.</p>
<p>The new Mooml 1.2.3 release also gets rid of with(), which will no longer be used to render Mooml templates. Instead, templates are prepared when they get registered, so all template tag functions (div(), span(), p()) get replaced by calls to the Mooml engine. Although this means there will be a performance hit when registering templates, it also means templates will render faster. As a plus, template parameter names will no longer conflict with template tag function names. Thus, you can now have a template with a parameter named &#8216;div&#8217; and the template wont fail rendering.</p>
<p>Download: http://mootools.net/forge/p/mooml<br />
Source: http://github.com/eneko/mooml</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>March 15, 2010 -- <a href="http://dev.enekoalonso.com/2010/03/15/more-updates-to-mooml-coming-soon/" title="More updates to Mooml coming soon">More updates to Mooml coming soon</a> (0)</li><li>March 24, 2010 -- <a href="http://dev.enekoalonso.com/2010/03/24/mooml-1-1-no-more-eval-say-hi-to-mooml-template-and-mooml-templates/" title="Mooml 1.1 &#8211; No more eval(), say Hi to Mooml.Template and Mooml.Templates">Mooml 1.1 &#8211; No more eval(), say Hi to Mooml.Template and Mooml.Templates</a> (0)</li><li>February 19, 2010 -- <a href="http://dev.enekoalonso.com/2010/02/19/having-fun-with-mooml-and-twitter/" title="Having fun with Mooml &#038; Twitter">Having fun with Mooml &#038; Twitter</a> (0)</li><li>February 1, 2010 -- <a href="http://dev.enekoalonso.com/2010/02/01/mooml-globalize-mooml-template-functions-going-global/" title="Mooml.globalize(): Mooml template functions going global">Mooml.globalize(): Mooml template functions going global</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/04/28/mooml-1-2-3-bye-bye-with/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting unique timestamps in Javascript</title>
		<link>http://dev.enekoalonso.com/2010/04/14/getting-unique-timestamps-in-javascript/</link>
		<comments>http://dev.enekoalonso.com/2010/04/14/getting-unique-timestamps-in-javascript/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 18:16:18 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[gettime]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[uid]]></category>
		<category><![CDATA[unique]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=510</guid>
		<description><![CDATA[Sometimes, when you want to generate unique id&#8217;s or similar, it is very common to use the current date as unix timestamp (new Date().getTime() or $time() in Mootools). The problem is that if we get the current timestamp multiple times too fast, it may happen that we get exactly the same value. Actually, this will [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when you want to generate unique id&#8217;s or similar, it is very common to use the current date as unix timestamp (new Date().getTime() or $time() in Mootools). The problem is that if we get the current timestamp multiple times too fast, it may happen that we get exactly the same value. Actually, this will happen very easily, as this tests shows:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> times = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span>times.<span class="me1">length</span> <span class="sy0">&lt;</span> <span class="nu0">20</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; times.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span>times<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// All timestamps returned would be pretty much the same</span></div>
</li>
</ol>
</div>
<p>To prevent this, we can use this little function which ensures we get a unique timestamp every time we call it:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// Mootools</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> getUniqueTime<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> time = $time<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>time == $time<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> $time<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>
<li class="li1">
<div class="de1"><span class="co1">// Javascript generic</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> getUniqueTime<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> time = <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>time == <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</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; <span class="kw1">return</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</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>Test it in Firebug:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> getUniqueTime<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> time = <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>time == <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</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; <span class="kw1">return</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</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>
<li class="li1">
<div class="de1"><span class="kw2">var</span> times = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span>times.<span class="me1">length</span> <span class="sy0">&lt;</span> <span class="nu0">20</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; times.<span class="me1">push</span><span class="br0">&#40;</span>getUniqueTime<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span>times<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Now all timestamps are unique</span></div>
</li>
</ol>
</div>
<p>Nice, uh?</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>May 6, 2011 -- <a href="http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/" title="Finding duplicate ID&#8217;s on an HTML page">Finding duplicate ID&#8217;s on an HTML page</a> (0)</li><li>October 5, 2010 -- <a href="http://dev.enekoalonso.com/2010/10/05/javascript-console-in-the-terminal-command-line/" title="Javascript console in the terminal (command line)">Javascript console in the terminal (command line)</a> (1)</li><li>September 21, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/" title="Date from ISO 8601 string">Date from ISO 8601 string</a> (1)</li><li>September 17, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/17/mootools-core-for-server-and-mobile-development/" title="MooTools Core for Server and Mobile Development">MooTools Core for Server and Mobile Development</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/04/14/getting-unique-timestamps-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

