<?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>Jonathon Bolster &#187; javascript</title>
	<atom:link href="http://www.bolsterweb.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bolsterweb.com</link>
	<description>Programming, drawing, random, life.</description>
	<lastBuildDate>Mon, 03 Oct 2011 17:30:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How clean is your window (object)?</title>
		<link>http://www.bolsterweb.com/2011/10/how-clean-is-your-window-object/</link>
		<comments>http://www.bolsterweb.com/2011/10/how-clean-is-your-window-object/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 17:30:32 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=316</guid>
		<description><![CDATA[I recently wanted to see how polluted the global namespace of our web application was. By that, I mean I wanted to see how many unnecessary objects we were assigning to the window object. To do this, I wanted to compare against a clean window object. A quick look on Google and I found this [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wanted to see how polluted the global namespace of our web application was. By that, I mean I wanted to see how many unnecessary objects we were assigning to the window object.</p>
<p>To do this, I wanted to compare against a clean window object. A quick look on Google and I found this article: <a href="http://darcyclarke.me/development/getting-a-clean-document-or-window-object-in-javascript/" title="Getting a clean document or window Object in JavaScript" onclick="pageTracker._trackPageview('/outgoing/darcyclarke.me/development/getting-a-clean-document-or-window-object-in-javascript/?referer=');">Getting a clean document or window Object in JavaScript</a>. The basic idea is that you create a new iframe element, and you can use the contentWindow property of that to give you a nice clean window object. As the author states, you might want to do this to prevent the remapping of native methods but I just want it for a quick (i.e. hacky) way to see what objects have being put into my own &#8216;clean&#8217; window.</p>
<p>What I do is create the iframe to get my clean window. Set up an object to copy across the objects from the main window object, then remove any that appear in the clean window.</p>
<p>Here it is in glorious, unminified code:</p>
<pre class="brush: jscript; title: ; notranslate">
(function(window){
	var keys = {},
	cleanWindow,
	iframe = document.createElement(&quot;iframe&quot;);

	iframe.style.display =&quot;none&quot;;
	iframe = document.body.appendChild(iframe);

	cleanWindow = iframe.contentWindow;
	document.body.removeChild(iframe);

	for(var keyName in window){
		keys[keyName] = window[keyName];
	}

	for(var keyName in cleanWindow) {
		delete keys[keyName]
	}

	return keys;
}(window))
</pre>
<p>And here&#8217;s a version that I&#8217;ve minified down:</p>
<pre class="brush: jscript; title: ; notranslate">
(function($){var k={},w,d=document,b=d.body,i=d.createElement(&quot;iframe&quot;);i.style.display=&quot;none&quot;;i=b.appendChild(i);w=i.contentWindow;b.removeChild(i);for(var s in $){k[s]=$[s]}for(var s in w){delete k[s]}return k}(window))
</pre>
<p>This is written as <a href="http://stackoverflow.com/questions/592396/what-is-the-purpose-of-a-self-executing-function-in-javascript" title="What is the purpose of a self executing function in javascript? on StackOverflow" onclick="pageTracker._trackPageview('/outgoing/stackoverflow.com/questions/592396/what-is-the-purpose-of-a-self-executing-function-in-javascript?referer=');">self executing JavaScript</a> and will return an object containing the objects that we worked out were added to the window.</p>
<p>Note: This is a very quick and dirty piece of code. Mostly it was an experiment for myself, to use the &#8216;clean window&#8217; object. It won&#8217;t tell you if a native window property was changed or anything, it only compares the keys. Just remember that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2011/10/how-clean-is-your-window-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looking at the animations on Google&#8217;s Jim Henson doodle</title>
		<link>http://www.bolsterweb.com/2011/09/looking-at-the-animations-on-googles-jim-henson-doodle/</link>
		<comments>http://www.bolsterweb.com/2011/09/looking-at-the-animations-on-googles-jim-henson-doodle/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 10:56:27 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Random Musings]]></category>
		<category><![CDATA[googledoodle]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jsfiddle]]></category>
		<category><![CDATA[sprite]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=297</guid>
		<description><![CDATA[On opening Google today, I&#8217;m shown a couple of muppets I can control (well, I can open their mouths and spin their heads around). This is to celebrate Jim Henson&#8217;s 75th birthday, but I&#8217;m interested in what makes it tick since there are a few little animations in there and I want to see what [...]]]></description>
			<content:encoded><![CDATA[<p>On opening Google today, I&#8217;m shown a couple of muppets I can control (well, I can open their mouths and spin their heads around). This is to celebrate Jim Henson&#8217;s 75th birthday, but I&#8217;m interested in what makes it tick since there are a few little animations in there and I want to see what Google does.</p>
<p><img src="http://www.bolsterweb.com/wp-content/uploads/2011/09/google-doodle-24-sep-2011.jpg" alt="Google Doodle for Jim Henson&#039;s 75th Birthday" title="Google Doodle for Jim Henson&#039;s 75th Birthday" width="371" height="183" class="size-full wp-image-307 aligncenter" /></p>
<p>Looking at the images that are loaded, they seem to be PNG sprite images. So then I went on a little (short lived) mission on how to recreate the animations myself. Honestly, there are muppet animations you can play with soon.</p>
<p>There is a handy little jQuery plugin called <a href="http://spritely.net/" title="Spritely" onclick="pageTracker._trackPageview('/outgoing/spritely.net/?referer=');">Spritely</a>. This will take a jQuery selector object and manipulate the background image position to generate the animation. Nicely, all you have to do is supply the number of frames and it will do the rest for you.</p>
<p>So I open up a new <a href="http://jsfiddle.net/jonathon/5GSah/" title="jsFiddle" onclick="pageTracker._trackPageview('/outgoing/jsfiddle.net/jonathon/5GSah/?referer=');">jsFiddle</a>, fill in my HTML and CSS with the containers and background images I want and then put in this little beauty:</p>
<pre class="brush: jscript; title: ; notranslate">
$('div').click(function() {
    var $this = $(this);
    $this.sprite({
        fps: 9,
        no_of_frames: $this.data(&quot;num&quot;),
        on_last_frame: function(obj) {
            obj.destroy();
        }
    });
});
</pre>
<p>Now, what I&#8217;ve done here is make it so that every div object will generate a sprite animation on click. This probably isn&#8217;t the prettiest way to do it, but I have *only* just discovered Spritely so I&#8217;ll look at the docs and improve. If you want to let me know in the comments, that&#8217;d be appreciated.</p>
<p>(Also, for the eagle eyed of you, I&#8217;m using a data attribute to store the number of frames in the animation. This was painstakingly done by opening the PNG files and counting the muppet heads. I bet I will find Spritely would do it for me after reviewing the documentation).</p>
<p>You&#8217;ve waited long enough. Go to the jsFiddle to play with the <a href="http://jsfiddle.net/jonathon/5GSah/" title="Muppet animations for Google Doodle on jsFiddle" onclick="pageTracker._trackPageview('/outgoing/jsfiddle.net/jonathon/5GSah/?referer=');">Muppet animations</a>.</p>
<p>Alternatively, here are links to the sprite images I&#8217;ve seen. Haven&#8217;t found anything for the blue guy on the left though! If you have, let me know and I&#8217;ll add it.</p>
<ul>
<li><a href="http://www.google.co.uk/logos/2011/henson11-hp-2oa.png" onclick="pageTracker._trackPageview('/outgoing/www.google.co.uk/logos/2011/henson11-hp-2oa.png?referer=');">http://www.google.co.uk/logos/2011/henson11-hp-2oa.png</a></li>
<li><a href="http://www.google.co.uk/logos/2011/henson11-hp-3oa.png" onclick="pageTracker._trackPageview('/outgoing/www.google.co.uk/logos/2011/henson11-hp-3oa.png?referer=');">http://www.google.co.uk/logos/2011/henson11-hp-3oa.png</a></li>
<li><a href="http://www.google.co.uk/logos/2011/henson11-hp-4ga.png" onclick="pageTracker._trackPageview('/outgoing/www.google.co.uk/logos/2011/henson11-hp-4ga.png?referer=');">http://www.google.co.uk/logos/2011/henson11-hp-4ga.png</a></li>
<li><a href="http://www.google.co.uk/logos/2011/henson11-hp-6ea-lr.png" onclick="pageTracker._trackPageview('/outgoing/www.google.co.uk/logos/2011/henson11-hp-6ea-lr.png?referer=');">http://www.google.co.uk/logos/2011/henson11-hp-6ea-lr.png</a></li>
<li><a href="http://www.google.co.uk/logos/2011/henson11-hp-6ea-er.png" onclick="pageTracker._trackPageview('/outgoing/www.google.co.uk/logos/2011/henson11-hp-6ea-er.png?referer=');">http://www.google.co.uk/logos/2011/henson11-hp-6ea-er.png</a></li>
<li><a href="http://www.google.co.uk/logos/2011/henson11-hp-6ea.png" onclick="pageTracker._trackPageview('/outgoing/www.google.co.uk/logos/2011/henson11-hp-6ea.png?referer=');">http://www.google.co.uk/logos/2011/henson11-hp-6ea.png</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2011/09/looking-at-the-animations-on-googles-jim-henson-doodle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript &#8211; e.preventDefault() is NOT return false</title>
		<link>http://www.bolsterweb.com/2011/01/javascript-e-preventdefault-is-not-return-false/</link>
		<comments>http://www.bolsterweb.com/2011/01/javascript-e-preventdefault-is-not-return-false/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 11:57:18 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=194</guid>
		<description><![CDATA[I&#8217;ve seen a lot of people use return false in place of e.preventDefault. This is usually when dealing with setting the click event on a hyperlink. I can only think that this comes from the original JavaScript snippets of running code on a click by setting onclick="alert('hi'); return false". This is wrong Well &#8211; it [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen a lot of people use <code>return false</code> in place of <code>e.preventDefault</code>. This is usually when dealing with setting the click event on a hyperlink. I can only think that this comes from the original JavaScript snippets of running code on a click by setting <code>onclick="alert('hi'); return false"</code>.</p>
<p><strong>This is wrong</strong></p>
<p>Well &#8211; it might be wrong. It all depends on your application. As long as you know the differences in them, you should be fine:</p>
<ul>
<li><strong>e.preventDefault</strong>: this (aptly) prevents the default event from happening. In the case of a hyperlink that you want to assign JavaScript to, this will stop the link from going to the location of the link</li>
<li><strong>e.stopPropagation</strong>: this stops the event from bubbling up. If you click on something that had a click event then using this will stop the event bubbling up and any click event that the parent containers might have.</li>
<li><strong>return false</strong>: this is the same as calling both of the methods above.</li>
</ul>
<p>I&#8217;ve done two examples for this:</p>
<ol>
<li><a href="http://jsfiddle.net/jonathon/XTY5c/" onclick="pageTracker._trackPageview('/outgoing/jsfiddle.net/jonathon/XTY5c/?referer=');">Showing the difference in e.preventDefault() and return false</a>: This example works on the click event and shows that preventDefault and return false are actually different.
</li>
<li><a href="http://jsfiddle.net/jonathon/JLMaR/" onclick="pageTracker._trackPageview('/outgoing/jsfiddle.net/jonathon/JLMaR/?referer=');">Example of e.stopPropagation</a>: This example shows a practical example of when you&#8217;d want to stop an event bubbling up. It uses event handlers on the mouseover event to show this.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2011/01/javascript-e-preventdefault-is-not-return-false/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Preventing accidental double clicks on links using jQuery</title>
		<link>http://www.bolsterweb.com/2010/12/preventing-accidental-double-clicks-on-links-using-jquery/</link>
		<comments>http://www.bolsterweb.com/2010/12/preventing-accidental-double-clicks-on-links-using-jquery/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 18:31:18 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[stackoverflow]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=190</guid>
		<description><![CDATA[I recently answered this question on StackOverflow asking how to prevent someone from clicking a link multiple times. This is the solution I came up with: You can use jQuery&#8217;s data storage methods to assign arbitrary bits of data to specific DOM elements. In this case, when the user clicks a link I assign a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently answered <a href="http://stackoverflow.com/questions/4410282/how-can-i-prevent-button-mashing-when-using-anchors-instead-of-buttons/" onclick="pageTracker._trackPageview('/outgoing/stackoverflow.com/questions/4410282/how-can-i-prevent-button-mashing-when-using-anchors-instead-of-buttons/?referer=');">this question on StackOverflow</a> asking how to prevent someone from clicking a link multiple times.</p>
<p>This is the solution I came up with:</p>
<pre class="brush: jscript; title: ; notranslate">$(&quot;a&quot;).click(function(e) {
    e.preventDefault();

    if (!$(this).data('isClicked')) {
        var link = $(this);

        // Your code on successful click

        // Set the isClicked value and set a timer to reset in 3s
        link.data('isClicked', true);
        setTimeout(function() {
            link.removeData('isClicked')
        }, 3000);
    } else {
        // Anything you want to say 'Bad user!'
    }
});</pre>
<p>You can use <a href="http://api.jquery.com/category/miscellaneous/data-storage/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/category/miscellaneous/data-storage/?referer=');">jQuery&#8217;s data storage</a> methods to assign arbitrary bits of data to specific DOM elements. In this case, when the user clicks a link I assign a property to the element as well as setting a timer to remove it 3 seconds later. This lets me know if the specific element has already been clicked by just calling the <code>data</code> method.</p>
<p>It&#8217;s not a bullet proof solution since it&#8217;s incredibly easy to get around if you&#8217;re determined enough. However, it is a simple example of how you can use those methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/12/preventing-accidental-double-clicks-on-links-using-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Shortcut for JavaScript timestamp</title>
		<link>http://www.bolsterweb.com/2010/11/shortcut-for-javascript-timestamp/</link>
		<comments>http://www.bolsterweb.com/2010/11/shortcut-for-javascript-timestamp/#comments</comments>
		<pubDate>Tue, 30 Nov 2010 11:28:25 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=179</guid>
		<description><![CDATA[JavaScript&#8217;s Date object provides a function for getting the milliseconds from the UNIX epoch &#8211; getTime(): However, there is a handy shortcut that gets the same thing: It makes me wonder if the +new shortcut can work for any other JavaScript object but I haven&#8217;t had a chance to look yet. Update: Apparently it&#8217;s just [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript&#8217;s Date object provides a function for getting the milliseconds from the UNIX epoch &#8211; <a href="http://www.w3schools.com/jsref/jsref_getTime.asp" onclick="pageTracker._trackPageview('/outgoing/www.w3schools.com/jsref/jsref_getTime.asp?referer=');">getTime()</a>:</p>
<pre class="brush: jscript; title: ; notranslate">
var timeStamp = new Date().getTime();
</pre>
<p>However, there is a handy shortcut that gets the same thing:</p>
<pre class="brush: jscript; title: ; notranslate">
var timeStamp = +new Date();
</pre>
<p>It makes me wonder if the <code>+new</code> shortcut can work for any other JavaScript object but I haven&#8217;t had a chance to look yet.</p>
<p><strong>Update</strong>: Apparently it&#8217;s just the unary operator (<a href="http://xkr.us/articles/javascript/unary-add/" onclick="pageTracker._trackPageview('/outgoing/xkr.us/articles/javascript/unary-add/?referer=');">http://xkr.us/articles/javascript/unary-add/</a>). From the article:</p>
<blockquote><p>The unary + operator, when used on types other than string, will internally attempt to call valueOf() or toString() (in that order) and then attempt to convert the result to a number. Thusly, the unary + operator can successfully convert many of the native JS types with certain restrictions.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/11/shortcut-for-javascript-timestamp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fon Bookmarklet</title>
		<link>http://www.bolsterweb.com/2008/09/fon-bookmarklet/</link>
		<comments>http://www.bolsterweb.com/2008/09/fon-bookmarklet/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 21:33:00 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jobolster.wordpress.com/2008/09/15/fon-bookmarklet/</guid>
		<description><![CDATA[I&#8217;m about to go on a short holiday around Europe. I&#8217;ve my Fon router plugged in, ready to get the benefits of free internet on my iPod Touch whilst away. Typed a quick &#8216;fon bookmarklet&#8217; search into google and only found a youtube video of some chinese person showing his working (but not actually giving [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m about to go on a short holiday around Europe. I&#8217;ve my Fon router plugged in, ready to get the benefits of free internet on my iPod Touch whilst away. Typed a quick &#8216;fon bookmarklet&#8217; search into google and only found a youtube video of some chinese person showing his working (but not actually giving the code)</p>
<p>So here&#8217;s mine. It&#8217;s simple, but it works:<br />
<code>javascript: document.getElementById('login_email').value='EMAIL_ADDRESS'; document.getElementById('login_password').value='PASSWORD'; document.getElementById('login_email').parentElement.submit()</code></p>
<p>And here&#8217;s it in link form for clicking and dragging to the bookmarks toolbar:<br />
<a href="javascript:document.getElementById('login_email').value='EMAIL_ADDRESS';document.getElementById('login_password').value='PASSWORD';document.getElementById('login_email').parentElement.submit()">Fon Login</a></p>
<p>My own link has obfuscation on the password just so that it&#8217;s not blatantly obvious in the link (maybe I&#8217;ll do some obfuscation code eventually), but changing those details would work for the &#8216;Fon Login&#8217; page that loads up when connecting to a FON_AP wifi point. It works in Safari, at least. The &#8216;parentElement&#8217; is null in Firefox but since this is for an iPod Touch, I don&#8217;t really mind. If anyone knows why it&#8217;s null then just let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2008/09/fon-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

