<?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>code-diesel &#187; javascript</title>
	<atom:link href="http://www.codediesel.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Using the new Page visibility API in your apps</title>
		<link>http://www.codediesel.com/javascript/using-the-new-page-visibility-api/</link>
		<comments>http://www.codediesel.com/javascript/using-the-new-page-visibility-api/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 07:01:43 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2986</guid>
		<description><![CDATA[One of the features lacking in the current browser api is that of determining whether the web page is currently visible to the user or is hidden (either in another tab or window). The new Page Visibility API allows you to do just that &#8211; determine whether your web page is visible to the user, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features lacking in the current browser api is that of determining whether the web page is currently visible to the user or is hidden (either in another tab or window). </p>
<p>The new <a href="http://www.w3.org/TR/2011/WD-page-visibility-20110602/" rel="nofollow"  title="page visibility api" target="_blank">Page Visibility API</a> allows you to do just that &#8211; determine whether your web page is visible to the user, is hidden in a background tab or window, or is prerendering. It allows the developer to use the page visibility state in JavaScript logic to make the user experience more friendly; for example, by stopping video, animation or slideshow playback whenever the user switches to another browser tab or window, and resuming whenever the user switches back. Also if your page is doing some ajax processing periodically, which consumes precious system resources, we can pause it when the page is not in focus. Other use can be in analytics, checking how long the page had been in actual user focus, rather then as a hidden tab or window.</p>
<p>Check the below demo page to see how this works. The demo was tested in Safari, Opera 11.10, Chrome and Firefox.<br />
<span id="more-2986"></span><br />
<a href="http://www.codediesel.com/demos/visibility/"class="view_demo" target="_blank" ></a></p>
<p>Notice how the video pauses whenever the page is hidden. Also check the title of the page as it switches state.</p>
<p>The api is quite simple in its design &#8211; whenever a web page visibility changes, the &#8216;visibilitychange&#8217; event is fired. This event can then be registered using the addEventListener method.</p>
<p>Currently the Page Visibility API supports three visibility states:<br />
   &#8216;visible&#8217; : user has opened the page and is working within it.<br />
   &#8216;hidden&#8217; : user has switched to another tab or minimized browser window.<br />
   &#8216;prerender&#8217; : browser is just prerendering a page.</p>
<p>However the problem with the current api implementation is that different browsers have implemented the api specification a little differntly, which can make working with them a little tedious. So one option is to use a wrapper which hides the various browser differences. <a href="https://github.com/evilmartians/visibility.js#readme" rel="nofollow"  title="visibility.js" target="_blank">Visibility.js</a> is one such wrapper which eases the usage of the api by hiding vendor-specific property prefixes and adding some high-level functions.</p>
<p>Below is the complete source for the above demo page. I&#8217;d not go into further details, as the respective pages have all the detailed documentation.</p>

<div class="wp_codebox"><table><tr id="p29862"><td class="code" id="p2986code2"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>meta charset<span style="color: #339933;">=</span><span style="color: #3366CC;">'UTF-8'</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/visibility.fallback.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/visibility.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;swfobject.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Visibility.<span style="color: #660066;">js</span> test<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Page Visibility API <span style="color: #339933;">&lt;</span>strong id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;support&quot;</span><span style="color: #339933;">&gt;&lt;/</span>strong<span style="color: #339933;">&gt;</span>.<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Switch browser tabs or window.<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;ytapiplayer&quot;</span><span style="color: #339933;">&gt;</span>
You need Flash player <span style="color: #CC0000;">8</span><span style="color: #339933;">+</span> and JavaScript enabled to view <span style="color: #000066; font-weight: bold;">this</span> video.
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> onYouTubePlayerReady<span style="color: #009900;">&#40;</span>playerId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ytplayer <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myytplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ytplayer<span style="color: #009900;">&#41;</span> ytplayer.<span style="color: #660066;">playVideo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> pause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ytplayer<span style="color: #009900;">&#41;</span> ytplayer.<span style="color: #660066;">pauseVideo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> params <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> allowScriptAccess<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;always&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> atts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;myytplayer&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
swfobject.<span style="color: #660066;">embedSWF</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.youtube.com/v/ylLzyHk54Z0?&quot;</span> <span style="color: #339933;">+</span>
                   <span style="color: #3366CC;">&quot;enablejsapi=1&amp;playerapiid=ytplayer&amp;version=3&quot;</span><span style="color: #339933;">,</span>
                   <span style="color: #3366CC;">&quot;ytapiplayer&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;425&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;356&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;8&quot;</span><span style="color: #339933;">,</span> 
                   <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> atts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009966; font-style: italic;">/* Make sure page visibility api is supported */</span>                   
<span style="color: #003366; font-weight: bold;">var</span> support <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'support'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Visibility.<span style="color: #660066;">isSupported</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    support.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'is supported'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    support.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'isn’t supported'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
document.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> Visibility.<span style="color: #660066;">state</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/* Pause/Play video when the page changes state */</span>
Visibility.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* Also change the page title on state change */</span>
    document.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> state<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>state <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;visible&quot;</span><span style="color: #009900;">&#41;</span>
        play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        pause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/using-the-new-page-visibility-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Generating clean URLs with javascript</title>
		<link>http://www.codediesel.com/javascript/generating-clean-urls-with-javascript/</link>
		<comments>http://www.codediesel.com/javascript/generating-clean-urls-with-javascript/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 08:43:55 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2983</guid>
		<description><![CDATA[In a recent project I needed to generate clean search urls on a form submit. There are basically two ways to do that. One is to post the search variables to the same page, and generate a clean url using PHP and then redirect it to the new url. The other way is to generate [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I needed to generate clean search urls on a form submit. There are basically two ways to do that. One is to post the search variables to the same page, and generate a clean url using PHP and then redirect it to the new url. The other way is to generate clean urls using JavaScript and immediately direct the page to the new url. This saves some processing on the server and one redirection, and also allows us to generate a url without a page refresh. I decided to go with the JavaScript solution.<br />
<span id="more-2983"></span><br />
A rough idea of the first option is given below.</p>

<div class="wp_codebox"><table><tr id="p29839"><td class="code" id="p2983code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'search'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$clean_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
   <span style="color: #009933; font-style: italic;">// code to create a clean url. </span>
   <span style="color: #009933; font-style: italic;">// After that $clean_url will contain the new redirect url </span>
   <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: <span style="color: #006699; font-weight: bold;">$clean_url</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>form <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myForm&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;get&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;search&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;search&quot;</span> maxlength<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Search&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The problem with the first option is that you cannot generate a clean url without refreshing the page. This can be problematic if your application requires you to generate clean urls without going back to the server (which entails a page refresh).</p>
<p>So for example if on form submission you have some ugly urls like the following.</p>

<div class="wp_codebox"><table><tr id="p298310"><td class="code" id="p2983code10"><pre class="html4strict" style="font-family:monospace;">http://mysite.com/search.php?keyword=blue+boxed+buttons</pre></td></tr></table></div>

<p>You can generate clean urls like the following on the client and then send it to the server.</p>

<div class="wp_codebox"><table><tr id="p298311"><td class="code" id="p2983code11"><pre class="html4strict" style="font-family:monospace;">http://mysite.com/search/blue-boxed-buttons</pre></td></tr></table></div>

<p>The basic search form is shown below.</p>

<div class="wp_codebox"><table><tr id="p298312"><td class="code" id="p2983code12"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;get&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">maxlength</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p>Now the following code will handle the clean url generation on the above form submission. It will intercept the form submission, generate a clean url, and direct the page to the new url, all without a page refresh.</p>

<div class="wp_codebox"><table><tr id="p298313"><td class="code" id="p2983code13"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Friendly URL rewrite */</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* Remove unwanted characters, only accept alphanumeric and space */</span>
    <span style="color: #003366; font-weight: bold;">var</span> keyword <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#search'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^A-Za-z0-9 ]/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* Replace multi spaces with a single space */</span>
    keyword <span style="color: #339933;">=</span> keyword.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s{2,}/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* Replace space with a '-' symbol */</span>
    keyword <span style="color: #339933;">=</span> keyword.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> cleanUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://mysite.com/search/'</span> <span style="color: #339933;">+</span> keyword.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    window.<span style="color: #660066;">location</span> <span style="color: #339933;">=</span> cleanUrl<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// Prevent default button behaviour</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Below is the Apache htaccess rule to go along with the above redirection. Our main search page is called &#8216;search.php&#8217;.</p>

<div class="wp_codebox"><table><tr id="p298314"><td class="code" id="p2983code14"><pre class="text" style="font-family:monospace;">RewriteEngine on
RewriteBase /
RewriteRule ^search/([A-Za-z0-9-]+)/?$ search.php?keyword=$1 [NC,L]</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/generating-clean-urls-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a infinite scroll page using jQuery</title>
		<link>http://www.codediesel.com/javascript/create-a-infinite-scroll-page-using-jquery/</link>
		<comments>http://www.codediesel.com/javascript/create-a-infinite-scroll-page-using-jquery/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 09:54:56 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2972</guid>
		<description><![CDATA[Waypoints is a small jQuery plugin that makes it easy to execute a function whenever you scroll to a particular element &#8211; a &#8216;waypoint&#8217;. This can be used to highlight when a element comes into view, load additional html or throw a popup. In this post we will use it to create a infinite scroll [...]]]></description>
			<content:encoded><![CDATA[<p>Waypoints is a small jQuery plugin that makes it easy to execute a function whenever you scroll to a particular element &#8211; a &#8216;waypoint&#8217;. This can be used to highlight when a element comes into view, load additional html or throw a popup. In this post we will use it to create a infinite scroll page.</p>
<p>An infinite scroll page can be useful when you do not want to add pagination, but would rather load the content on the same page when the user requests.<br />
<span id="more-2972"></span><br />
<a href="http://imakewebthings.github.com/jquery-waypoints/" rel="nofollow"  title="waypoints" target="_blank">Waypoints</a> requires jQuery, so you need to add that to your page before initializing waypoints.</p>

<div class="wp_codebox"><table><tr id="p297219"><td class="code" id="p2972code19"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery-1.4.4.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;waypoints.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><a href="http://www.codediesel.com/demos/waypoints/"class="view_demo" target="_blank" ></a></p>
<p>The following is how you register an element as a waypoint. When the user scrolls past that element it triggers a <em>waypoint.reached</em> event, which you can then use to run your custom code.</p>
<p>For example if you have a footer element with the following structure, you can attach a function to be executed when that element comes in view.</p>

<div class="wp_codebox"><table><tr id="p297220"><td class="code" id="p2972code20"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;footer&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span>  
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Footer text<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>footer&gt;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p297221"><td class="code" id="p2972code21"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		offset<span style="color: #339933;">:</span> <span style="color: #3366CC;">'100%'</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009966; font-style: italic;">/* Execute the following when the footer comes in view */</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'footer'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> direction<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You have scrolled to the end of the page.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the above example we only displayed a alert dialog, but what we instead want is additional data to be appended to the existing content, giving us our infinite page. The following is a example code to create a infinite page by loading additional data from a php server page.</p>

<div class="wp_codebox"><table><tr id="p297222"><td class="code" id="p2972code22"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$footer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'footer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		offset<span style="color: #339933;">:</span> <span style="color: #3366CC;">'100%'</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/* When the 'footer' element comes in view, 
           the following code is executed */</span>
	$footer.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> direction<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$footer.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'remove'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">/* We get additional data from a php page and 
                   append the same to the end of the page. */</span>
		$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'more-data.php'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#main'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$footer.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span>opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can find additional options at the waypoints page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/create-a-infinite-scroll-page-using-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Reactive web content</title>
		<link>http://www.codediesel.com/javascript/building-reactive-web-content/</link>
		<comments>http://www.codediesel.com/javascript/building-reactive-web-content/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 10:28:16 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2929</guid>
		<description><![CDATA[Reading is an active process. Weather you are reading a web page, a book or any other media, the information tends to generate ideas in the mind of the reader. An active reader asks questions, considers alternatives, questions assumptions and analyses the argument of the author. An active reader doesn&#8217;t passively store up information, but [...]]]></description>
			<content:encoded><![CDATA[<p>Reading is an active process. Weather you are reading a web page, a book or any other media, the information tends to generate ideas in the mind of the reader.  An active reader asks questions, considers alternatives, questions assumptions and analyses the argument of the author. An active reader doesn&#8217;t passively store up information, but uses the author&#8217;s arguments to create a framework to further up his ideas and understanding.<br />
<span id="more-2929"></span><br />
As far as the content of the written page was concerned reading was a static thing; until the arrival of the electronic media. Take the following paragraph as an example.</p>
<blockquote><p>
When you eat 7 cookies, and a cookie contains on average 50 calories, you will consume around 350 calories. </p>
<p>So if on average you require 300 calories to survive daily, you are eating an excess of 50 calories.
</p></blockquote>
<p>This is a static text and before the web this was all you could get. But with interactivity easily achievable with JavaScript and other tools we could easily convert the above text content to a &#8216;Reactive Document&#8217;.</p>
<p>A Reactive Document allows the reader to play with the author&#8217;s assumptions and analysis, and see the consequences of any changes. Now take the above paragraph and convert it to a reactive format as given below. You can change the values of the underlined text by dragging them left or right.</p>
<p><iframe src="http://www.codediesel.com/demos/tangle/" scrolling="no" frameborder="0" width="100%" height="120px"><br />
</iframe></p>
<p>Notice how the consequences of your adjustments are reflected in the above paragraph. The reader can explore alternative scenarios, understand the results of his actions, and come to a generally deeper understanding of the content.</p>
<h4>Implementation</h4>
<p>The Reactive Document was implemented using the <a href="http://worrydream.com/Tangle/guide.html" rel="nofollow"  title="Tangle" target="_blank">Tangle</a> library by <a href="http://worrydream.com/" rel="nofollow"  title="Bret Victor" target="_blank">Bret Victor</a>. The idea is somewhat along the lines of Wolfram&#8217;s <a href="http://www.wolfram.com/cdf/" rel="nofollow"  title="Wolfram CDF" target="_blank">Computable Document Format (CDF)</a>. </p>
<p>Although technically it is a simple MVC implementation in JavaScript; conceptually it lets you add a interactive dimension to your web pages in a quick and easy manner.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/building-reactive-web-content/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Transform websites to mobile sites quickly</title>
		<link>http://www.codediesel.com/javascript/transform-websites-to-mobile-sites-quickly/</link>
		<comments>http://www.codediesel.com/javascript/transform-websites-to-mobile-sites-quickly/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 04:31:03 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2887</guid>
		<description><![CDATA[Mobilize.js allows web developers create mobile sites out of existing websites with little effort. It supports the following features: Automatically detect mobile browsers. Easy to integrate – no server side changes needed. Open source – no expensive software or licenses needed. Familiar HTML5, Javascript and JQuery Mobile tools used. Supports iPhone, Android, Blackberry, Opera Mini [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mobilizejs.com/" rel="nofollow" ><img src="http://www.codediesel.com/wp-content/uploads/2011/08/mobilejs.gif" alt="" title="mobilejs" width="75" height="121" class="alignleft size-full wp-image-2886" /></a><a href="http://mobilizejs.com/" rel="nofollow" target="_blank" >Mobilize.js</a> allows web developers create mobile sites out of existing websites with little effort. It supports the following features:</p>
<p>Automatically detect mobile browsers.<br />
Easy to integrate – no server side changes needed.<br />
Open source – no expensive software or licenses needed.<br />
Familiar HTML5, Javascript and JQuery Mobile tools used.<br />
Supports iPhone, Android, Blackberry, Opera Mini and other browsers and devices.</p>
<p>If you have a WordPress site, plugins are available for the same. To see exactly how mobile.js works, check <a href="http://cdn.mobilizejs.com/docs/trunk/manual/introduction.html#id3" rel="nofollow" target="_blank" >this section</a> of the documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/transform-websites-to-mobile-sites-quickly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Infinite Wall of images using Mootools</title>
		<link>http://www.codediesel.com/javascript/infinite-wall-of-images-using-mootools/</link>
		<comments>http://www.codediesel.com/javascript/infinite-wall-of-images-using-mootools/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 12:33:12 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[graphics]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2852</guid>
		<description><![CDATA[JavaScript Photo galleries are dime-a-dozen, with a large number of variations in size and features. But this particular one developed using Mootools is a breath of fresh air. Most galleries are spatially bounded, i.e. they have a limit how how much you can scroll in any direction. The Wall &#8211; is a Photo Gallery which [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/thumbnailExamples1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/thumbnailExamples1.jpg" alt="" title="the wall" width="155" height="131" class="alignleft size-full wp-image-2855" /></a>JavaScript Photo galleries are dime-a-dozen, with a large number of variations in size and features.  But this particular one developed using Mootools is a breath of fresh air. Most galleries are spatially bounded, i.e. they have a limit how how much you can scroll in any direction.<br />
<span id="more-2852"></span><br />
The  Wall &#8211; is a Photo Gallery which can cover your entire screen with images.  It provides a spatially infinite plane to display images, video or other media. Imagine a infinite grid, in the cells of which you can put images. You can scroll the photos in any direction &#8211; vertical, horizontal, diagonal and reverse too. You can insert any number of images in the grid, which are repeated over the entire plane to give a illusion of an infinite wall.</p>
<p>Also, it’s not just for images, it also can hold videos and text and it can be used as an endless grid or also as the popular Coda Slider.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall1.jpg" alt="" title="the_wall" width="554" height="331" class="aligncenter size-full wp-image-2857" /></a></p>
<p>The plugin also provides many interesting variations on the basic theme as shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall_options1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall_options1.jpg" alt="the wall options" title="the_wall_options" width="550" height="318" class="aligncenter size-full wp-image-2859" /></a></p>
<p>Demos : <a href="http://wall.plasm.it/examples/" rel="nofollow" target="_blank" >http://wall.plasm.it/examples/</a><br />
URL : <a href="http://wall.plasm.it/" rel="nofollow" target="_blank" >http://wall.plasm.it/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/infinite-wall-of-images-using-mootools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharing data across windows using localStorage</title>
		<link>http://www.codediesel.com/javascript/sharing-messages-and-data-across-windows-using-localstorage/</link>
		<comments>http://www.codediesel.com/javascript/sharing-messages-and-data-across-windows-using-localstorage/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 07:28:39 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2832</guid>
		<description><![CDATA[One of a common requirement in some web applications is that of sending messages and data across windows (of the same origin). Doing this is not easy and requires the help of cookies and a bit of JavaScript. Now with HTML5 and its localStorage feature we can accomplish this quite easily. Storage Events Whenever a [...]]]></description>
			<content:encoded><![CDATA[<p>One of a common requirement in some web applications is that of sending messages and data across windows (of the same origin). Doing this is not easy and requires the help of cookies and a bit of JavaScript. Now with HTML5 and its localStorage feature we can accomplish this quite easily.<br />
<span id="more-2832"></span></p>
<h4>Storage Events</h4>
<p>Whenever a <em>localStorage</em> object is changed using the <em>setItem()</em>, <em>removeItem()</em>, or <em>clear()</em> functions, the <em>storage</em> event is fired (across multiple windows of the same origin) by the browser, we can trap this event and use it to share messages or data across windows. Check the following demo to see how this works in a simple case.</p>
<p><a href="http://www.codediesel.com/demos/localstorage/index.php"class="view_demo"  target="_blank"></a></p>
<p>The above example works by using the storage event of the Window object. So whenever the localStorage object is modified, the <em>storage</em> event is fired and we use it to update the window content.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/06/localstorage.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/06/localstorage.gif" alt="" title="localstorage" width="437" height="361" class="aligncenter size-full wp-image-2834" /></a></p>

<div class="wp_codebox"><table><tr id="p283226"><td class="code" id="p2832code26"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;storage&quot;</span><span style="color: #339933;">,</span> displayStorageEvent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Below is the skeleton code for the demo example.</p>

<div class="wp_codebox"><table><tr id="p283227"><td class="code" id="p2832code27"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Input element</span>
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Element to display the updated data</span>
<span style="color: #003366; font-weight: bold;">var</span> output <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'event-data'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> displayStorageEvent<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">key</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'storage-event'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        output.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> e.<span style="color: #660066;">newValue</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> updateStorageEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'storage-event'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;storage&quot;</span><span style="color: #339933;">,</span> displayStorageEvent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;keyup&quot;</span><span style="color: #339933;">,</span> updateStorageEvent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Sharing messages</h4>
<p>Although in the above example we are sharing data across windows, we can use the same mechanism to share messages. In the above example we are using the &#8216;storage-event&#8217; key to store the data we type in the input box. Instead we can pass a simple message. The syntax remains the same, only the semantics changes.</p>

<div class="wp_codebox"><table><tr id="p283228"><td class="code" id="p2832code28"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Store the 'checkPassword' message in the 'app.message.auth' key */</span>
localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app.message.auth'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;checkPassword&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/* Store the 'dataSave' message in the 'app.message.status' key */</span>
localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app.message.status'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;dataSave&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Security concerns</h4>
<p>As the communication can only happen between windows with the same origin, there is little risk of data leak between sites. Still you should refrain from storing sensitive information in the storage.</p>
<h4>Possible applications</h4>
<p>Now that we have seen a simple proof of concept of &#8216;cross window messaging and data sharing&#8217;, what are the potential use cases of the same. An idea without a concrete and useful application is useless. Although many people will have good ideas for the above concept, I presently lack one. So if you can think of some nice use cases for this, please voice your ideas in the comments section.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/sharing-messages-and-data-across-windows-using-localstorage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding HTML5 Geolocation to your web applications</title>
		<link>http://www.codediesel.com/javascript/adding-html5-geolocation-to-your-applications/</link>
		<comments>http://www.codediesel.com/javascript/adding-html5-geolocation-to-your-applications/#comments</comments>
		<pubDate>Fri, 13 May 2011 06:09:21 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2815</guid>
		<description><![CDATA[One of the interesting and useful addition to the HTML5 specification is the Geolocation API. The Geolocation API allows users to share their location with web applications so that they can enjoy the benefits of various location-aware services. Geolocation enables you as a developer or website owner to figure out where a particular user is [...]]]></description>
			<content:encoded><![CDATA[<p>One of the interesting and useful addition to the HTML5 specification is the Geolocation API. The Geolocation API allows users to share their location with web applications so that they can enjoy the benefits of various location-aware services.</p>
<p>Geolocation enables you as a developer or website owner to figure out where a particular user is located on the planet. This can be helpful in various web applications; for example in social networking, where you can find out where your various friends are currently located or in advertising where you can  display targeted ads based on the users location.<br />
<span id="more-2815"></span></p>
<h4>Getting your users location</h4>
<p>In the past, IP based geolocation was the only way to get a users possible location. Services and databases such as  <a href="http://www.maxmind.com/app/geoip_country" rel="nofollow" target="_blank" >MaxMind</a> or <a href="http://www.ip2location.com/" rel="nofollow" target="_blank" >ip2location</a> where used to locate the position of the user, based on the users IP address.</p>
<p>Now that Geolocation is available as a built-in API service in HTML5, it makes easier for developers to quickly integrate location based services into their applications.</p>
<p><a href="http://www.codediesel.com/demos/geolocation"class="view_demo" target="_blank" ></a></p>
<h4>Detecting Geolocation support</h4>
<p>Before you write any Geolocation code you need to check if it is supported on your browser. The simplest way to detect if Geolocation is supported is to use the global &#8216;navigator&#8217; object.</p>

<div class="wp_codebox"><table><tr id="p281537"><td class="code" id="p2815code37"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">geolocation</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Geolocation supported. Do something here.</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>Reading users location</h4>
<p>Once we have checked that Geolocation is supported we can use the following to get the users location data.</p>

<div class="wp_codebox"><table><tr id="p281538"><td class="code" id="p2815code38"><pre class="javascript" style="font-family:monospace;">navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span>success_handler<span style="color: #339933;">,</span> error_handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will execute the callback function &#8216;success_handler&#8217; if the request is successful or the &#8216;error_handler&#8217; callback if some error occurs. The location data is available as a &#8216;position&#8217; object in the &#8216;success_handler&#8217;.</p>

<div class="wp_codebox"><table><tr id="p281539"><td class="code" id="p2815code39"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> success_handler<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        latitude <span style="color: #339933;">=</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
        longitude <span style="color: #339933;">=</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
        accuracy <span style="color: #339933;">=</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">accuracy</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Below is a list of all properties available:</p>
<table class="posts_table">
<colgroup>
<col width="36%" />
<col width="38%" />
<col width="26%" />
</colgroup>
<thead valign="bottom">
<tr>
<th class="head"><strong>Property</strong></th>
<th class="head"><strong>Data Type</strong></th>
<th class="head"><strong>Unit</strong></th>
</tr>
</thead>
<tbody valign="top">
<tr>
<td>coords.latitude</td>
<td>double</td>
<td>decimal degrees</td>
</tr>
<tr>
<td>coords.longitude</td>
<td>double</td>
<td>decimal degrees</td>
</tr>
<tr>
<td>coords.altitude</td>
<td>meters</td>
<td>decimal degrees</td>
</tr>
<tr>
<td>coords.accuracy</td>
<td>double</td>
<td>meters</td>
</tr>
<tr>
<td>coords.altitudeAccuracy</td>
<td>double or null</td>
<td>meters</td>
</tr>
<tr>
<td>coords.heading</td>
<td>double or null</td>
<td>degrees</td>
</tr>
<tr>
<td>coords.speed</td>
<td>double or null</td>
<td>meters/second</td>
</tr>
<tr>
<td>timestamp</td>
<td>DOMTimeStamp</td>
<td>Date() object</td>
</tr>
</tbody>
</table>
<h4>User privacy</h4>
<p>When you opened the demo web page given above you may have received a notification bar like the following; this bar requests your permission to share your location with the browser.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/05/geo-location.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/05/geo-location.gif" alt="" title="geo-location" width="603" height="25" class="aligncenter size-full wp-image-2817" /></a></p>
<p>The HTML5 Geolocation specification mandates that before any location data can be retrieved the browser first request permission from the user to share its location. Only when the user allows does the browser retrieves the users location. </p>
<blockquote><p>
<strong>Note:</strong> If opted to &#8216;always share&#8217; or &#8216;always deny&#8217; in Firefox and later change your mind, you can easily revert permissions by going back to the website and selecting &#8216;Tools&#8217; menu->&#8217;Page Info&#8217; in Firefox. Then change the setting for &#8216;Share Location&#8217; on the Permissions tab.
</p></blockquote>
<h4>How does Geolocation work</h4>
<p>Once you give permission to the browser to share your location, the browser gathers information about nearby access points and also your computer’s IP address which the browser then sends to the default geolocation service provider &#8211; Google Location Services in case of Firefox, to get an estimate of your location. This location estimate is then sent to the requesting website.</p>
<h4>Location accuracy</h4>
<p>You may have noticed above that the &#8216;position&#8217; object returns a <strong>accuracy</strong> variable. <strong>accuracy</strong> represents a value in meters which specifies how close the latitude and longitude values are to the actual location. You can increase the location accuracy by passing a option to the <strong>getCurrentPosition</strong> function. Bear in mind that this is only a hint that you give to the browser; whether it returns a high accuracy result depends on the location service your browser uses.</p>

<div class="wp_codebox"><table><tr id="p281540"><td class="code" id="p2815code40"><pre class="javascript" style="font-family:monospace;">navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span>
                                    success_handler<span style="color: #339933;">,</span> 
                                    error_handler<span style="color: #339933;">,</span> 
                                    <span style="color: #009900;">&#123;</span>enableHighAccuracy<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Saving location to Cookies</h4>
<p>Once we get the location data from the user it is better to store it for the entire session so we can use it on other pages. Storing it in cookies is one quick option. For this example I&#8217;ve used the jQuery <a href="http://plugins.jquery.com/project/Cookie" rel="nofollow" target="_blank" >cookie plugin</a>, which provides a simple and clean way to save and retrieve cookies.</p>
<p>We can store the cookies in the &#8216;success_handler&#8217; and then later check the same before issuing another location request.</p>

<div class="wp_codebox"><table><tr id="p281541"><td class="code" id="p2815code41"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> success_handler<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009966; font-style: italic;">/* Get the location data */</span>
        latitude <span style="color: #339933;">=</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
        longitude <span style="color: #339933;">=</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
        accuracy <span style="color: #339933;">=</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">accuracy</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posLat&quot;</span><span style="color: #339933;">,</span> latitude<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posLon&quot;</span><span style="color: #339933;">,</span> longitude<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posAccuracy&quot;</span><span style="color: #339933;">,</span> accuracy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we have nicely saved the data, all the other site pages can now access the location data by reading them from the cookies.</p>

<div class="wp_codebox"><table><tr id="p281542"><td class="code" id="p2815code42"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Check if location data is available in the cookies */</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posLat&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    latitude  <span style="color: #339933;">=</span> $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posLat&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    longitude <span style="color: #339933;">=</span> $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posLon&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    accuracy  <span style="color: #339933;">=</span> $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posAccuracy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// do something here</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>Continuous Position Updates</h4>
<p>In the above example the location was read once and stored for future use. But what if your application is on a mobile device and you are on the move. The Geolocation API provides another function <strong>navigator.geolocation.watchPosition</strong> that automatically retrieves the users current location if it ever changes. The function is similar to &#8216;getCurrentPosition&#8217;, the only difference is that it regularly polls the location and returns the new location data.</p>

<div class="wp_codebox"><table><tr id="p281543"><td class="code" id="p2815code43"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> watch_id <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">watchPosition</span><span style="color: #009900;">&#40;</span>
                                        success_handler<span style="color: #339933;">,</span> 
                                        error_handler
                                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The function return a unique numeric id which you can later use to stop the polling.</p>

<div class="wp_codebox"><table><tr id="p281544"><td class="code" id="p2815code44"><pre class="javascript" style="font-family:monospace;">navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">clearWatch</span><span style="color: #009900;">&#40;</span>watch_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/adding-html5-geolocation-to-your-applications/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Drawing Trees in Canvas</title>
		<link>http://www.codediesel.com/javascript/drawing-trees-in-canvas/</link>
		<comments>http://www.codediesel.com/javascript/drawing-trees-in-canvas/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 12:27:57 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2788</guid>
		<description><![CDATA[Lately I’ve been experimenting with the new HTML5 Canvas element, and the best way to learn some new technology is to create something in it. For some time I&#8217;ve been dabbling in Processing and thought of porting some algorithms there in Canvas. Rather than jumping into animation it was better to play around with static [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2011/01/tree1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/01/tree1.gif" alt="" title="tree1" width="230" height="216" class="alignleft size-full wp-image-2790" /></a>Lately I’ve been experimenting with the new HTML5 Canvas element, and the best way to learn some new technology is to create something in it. For some time I&#8217;ve been  dabbling in <a href="http://processing.org/" rel="nofollow" target="_blank" >Processing</a> and thought of porting some algorithms there in Canvas.<br />
Rather than jumping into animation it was better to play around with static structures. I&#8217;ve a soft spot for visualizations so I decided to port some algorithms for creating recursive tree structures.<br />
<span id="more-2788"></span></p>
<h4>Download</h4>
<p>Before to go further you can view the demo and also download the code. The demos will only run in browses that support the Canvas element.</p>
<p><a href="http://www.codediesel.com/demos/canvas/tree/tree1.html"target="_blank" >VIEW DEMO</a></p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/tree" rel="nofollow" >Download Source</a><br />
<span>Downloads : 616  / File size : 6.8 kB</span>
</div>
<h4>Drawing a tree</h4>
<p>The simplest way to draw a tree is with the following code. This will render the tree with the default options.</p>

<div class="wp_codebox"><table><tr id="p278847"><td class="code" id="p2788code47"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Trees <span style="color: #000066; font-weight: bold;">in</span> Canvas<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">'algoTree.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>canvas id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;canvas&quot;</span><span style="color: #339933;">&gt;&lt;/</span>canvas<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;application/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> height <span style="color: #339933;">=</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> width <span style="color: #339933;">=</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
canvas.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> height<span style="color: #339933;">;</span>
canvas.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> width<span style="color: #339933;">;</span>
ctx <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
tree.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span>height<span style="color: #339933;">,</span>width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Below are a few generated samples.<br />
<a href="http://www.codediesel.com/wp-content/uploads/2011/01/tree2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/01/tree2.gif" alt="" title="tree2" width="501" height="501" class="aligncenter size-full wp-image-2791" /></a></p>
<h4>Usage</h4>
<p>The &#8216;algoTree.js&#8217; exposes a single method, tree.draw, that accepts six parameters as given below.</p>

<div class="wp_codebox"><table><tr id="p278848"><td class="code" id="p2788code48"><pre class="javascript" style="font-family:monospace;">tree.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> spread<span style="color: #339933;">,</span> show leaves<span style="color: #339933;">,</span> leaves type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8216;ctx&#8217; is the canvas context.<br />
&#8216;height/width&#8217; is the canvas height and width.<br />
&#8216;spread&#8217; takes a number between 0.3 &#8211; 0.9 and adjusts the size of the tree.<br />
&#8216;show leaves&#8217; takes a boolean value and displays the leaves if set to <strong>true</strong>.<br />
&#8216;leaves type&#8217; takes one of the following four parameters-<br />
           tree.SMALL_LEAVES<br />
           tree.MEDIUM_LEAVES<br />
           tree.BIG_LEAVES<br />
           tree.THIN_LEAVES</p>
<p>By default the trees are drawn with leaves but you can draw bare skeletal tree by passing the boolean value &#8216;false&#8217; as the fourth parameter in the tree.draw method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/drawing-trees-in-canvas/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Introducing HTML5 Web Workers</title>
		<link>http://www.codediesel.com/javascript/introducing-html5-web-workers/</link>
		<comments>http://www.codediesel.com/javascript/introducing-html5-web-workers/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 05:44:22 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2780</guid>
		<description><![CDATA[One of the interesting and useful additions to the HTML5 standards is the support for Web Workers. Web Workers will help you design more responsive web applications then otherwise possible in the past. What are Web Workers Web Workers are basically a API specification that lets you create background JavaScript threads to process CPU intensive [...]]]></description>
			<content:encoded><![CDATA[<p>One of the interesting and useful additions to the HTML5 standards is the support for Web Workers. Web Workers will help you design more responsive web applications then otherwise possible in the past.</p>
<h4>What are Web Workers</h4>
<p>Web Workers are basically a API specification that lets you create background JavaScript threads to process CPU intensive tasks. Normally in browsers a single thread is created to handle all the JavaScript code. So whatever JavaScript code is run in the browser is executed in that single thread; whether you are doing some calculation or updating page elements. The downside of this is that some CPU intensive piece of JavaScript can render the page unresponsive or slow it to a crawl. </p>
<p>Web Workers alleviate this problem by letting you create multiple JavaScript threads that will run independent of each other, this will prevent one CPU intensive piece of JavaScript from interfering with the UI code.<br />
<span id="more-2780"></span></p>
<h4>Checking Web Worker support</h4>
<p>Before you create any Web Worker related code you will need to find if your browser supports HTML5 Web Workers. Failure to do this will send your Web Workers to slog in Never Never Land. The following code will allow you to check if your browser supports Web Workers.</p>

<div class="wp_codebox"><table><tr id="p278057"><td class="code" id="p2780code57"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Check if Web Workers are supported */</span>
<span style="color: #003366; font-weight: bold;">function</span> getWebWorkerSupport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Worker<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>Creating a simple Worker</h4>
<p>Once we have checked that Workers are supported in the browser we are ready to put them to task. In this example we will create a simple Web Worker that will &#8216;Add&#8217; or &#8216;Multiply&#8217; two numbers. The example is deliberately kept simple to keep the discussion focused on Worker details; in reality you would not use Workers for such simple things, but for more elaborate and CPU intensive tasks. Below is a small piece of JavaScript code that does the actual arithmetic calculation. This our Worker. It is stored in the &#8216;arithmetic.js&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p278058"><td class="code" id="p2780code58"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* arithmetic.js */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> addNumbers<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x <span style="color: #339933;">+</span> y<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> mulNumbers<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">*</span>y<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/* 
   Add a event listener to the worker, this will
   be called when the worker receives a message
   from the main page.
*/</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> event.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">op</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'mult'</span><span style="color: #339933;">:</span>
        postMessage<span style="color: #009900;">&#40;</span>mulNumbers<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'add'</span><span style="color: #339933;">:</span>
        postMessage<span style="color: #009900;">&#40;</span>addNumbers<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
        postMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Wrong operation specified&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now that we have or Worker file ready we need to call it from our main file.<br />
The following line will create a new Worker thread and run the code stored in &#8216;arithmetic.js&#8217;.</p>

<div class="wp_codebox"><table><tr id="p278059"><td class="code" id="p2780code59"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Create a new worker */</span>
    arithmeticWorker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Worker<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;arithmetic.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once we have created Workers we need to send and receive messages from them. For that we need to add a event handler to the main calling code.</p>

<div class="wp_codebox"><table><tr id="p278060"><td class="code" id="p2780code60"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* 
   Add a event listener to the worker, this will
   be called whenever the worker posts any message.
*/</span>
arithmeticWorker.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;output&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> event.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now whenever a Worker posts a message the &#8216;onmessage&#8217; event is fired and the code within it is executed. The data passed by the Worker is stored in the event.data container. We have seen how to receive a message from the Worker but how to we post one. Posting a message to the Worker is very simple.</p>

<div class="wp_codebox"><table><tr id="p278061"><td class="code" id="p2780code61"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Message sent by the main page to the Worker. */</span>
arithmeticWorker.<span style="color: #660066;">postMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Main says...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The message can be a simple variable or a JSON object. The same event management applies to the Worker file. Whenever the main page posts a message to the Worker the Worker can receive the message in a &#8216;onmessage&#8217; event handler, at the same time the Worker can post a message to the main page with the &#8216;postMessage&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p278062"><td class="code" id="p2780code62"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Message sent by a Worker to the main page. */</span>
postMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Worker says...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We have already seen the Worker code. Below is the code for the main file.</p>

<div class="wp_codebox"><table><tr id="p278063"><td class="code" id="p2780code63"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;x&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;2&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;y&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;3&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;output&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;multButton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Multiply&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;addButton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Add&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/* Check if Web Workers are supported */</span>
<span style="color: #003366; font-weight: bold;">function</span> getWebWorkerSupport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Worker<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>getWebWorkerSupport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>message<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* Create a new worker */</span>
    arithmeticWorker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Worker<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;arithmetic.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/* 
       Add a event listener to the worker, this will
       be called when the worker posts a message.
    */</span>
    arithmeticWorker.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;output&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> event.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* Register events for buttons */</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;multButton&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009966; font-style: italic;">/* Get the values to do operation on */</span>
        x <span style="color: #339933;">=</span> parseFloat<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;x&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        y <span style="color: #339933;">=</span> parseFloat<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;y&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        message <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">'op'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'mult'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'x'</span>  <span style="color: #339933;">:</span> x<span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'y'</span>  <span style="color: #339933;">:</span> y
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        arithmeticWorker.<span style="color: #660066;">postMessage</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;addButton&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009966; font-style: italic;">/* Get the values to do operation on */</span>
        x <span style="color: #339933;">=</span> parseFloat<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;x&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        y <span style="color: #339933;">=</span> parseFloat<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;y&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        message <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">'op'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'add'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'x'</span>  <span style="color: #339933;">:</span> x<span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'y'</span>  <span style="color: #339933;">:</span> y
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        arithmeticWorker.<span style="color: #660066;">postMessage</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h4>Terminating Web Workers</h4>
<p>Once the main page starts a Worker thread, the thread doesn&#8217;t terminate by itself. The calling page has to explicitly ask the Worker to terminate. This may become necessary because creating each new Worker consumes precious browser resources, which you will need to reclaim once the Workers task is no longer required.</p>

<div class="wp_codebox"><table><tr id="p278064"><td class="code" id="p2780code64"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Terminate the Worker */</span>
arithmeticWorker.<span style="color: #660066;">terminate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Workers and DOM accessibility</h4>
<p>Due to the threading nature of Workers they do not have access to the following JavaScript objects.</p>
<p>    * The DOM (it&#8217;s not thread-safe)<br />
    * The window object<br />
    * The document object<br />
    * The parent object</p>
<p><a href="http://www.codediesel.com/demos/workers/"target="_blank" >VIEW DEMO</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/introducing-html5-web-workers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

