<?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/"
	>

<channel>
	<title>James Padolsey</title>
	<atom:link href="http://james.padolsey.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Sat, 04 Jul 2009 17:20:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to avoid switch-case syndrome</title>
		<link>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/</link>
		<comments>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 17:20:25 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[TOTW]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=989</guid>
		<description><![CDATA[This is the first post of a hopeful series dubbed &#8220;<strong><em>JavaScript, tip of the week</em></strong>&#8220;; I will try to release a tip in some form every week.

I&#8217;m young and haven&#8217;t been doing this programming thing for very long so&#8230;]]></description>
			<content:encoded><![CDATA[<p>This is the first post of a hopeful series dubbed &#8220;<strong><em>JavaScript, tip of the week</em></strong>&#8220;; I will try to release a tip in some form every week.</p>

<p>I&#8217;m young and haven&#8217;t been doing this programming thing for very long so the fact that I&#8217;ve never had to use a switch-case statement (to my recollection) might not be very impressive; actually you probably see this as a bad thing and you might even be wondering why I haven&#8217;t. I can&#8217;t really explain why but I seem to have an inherent dislike of things like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>something<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
        doX<span style="color: #009900;">&#40;</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>
&nbsp;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span>
        doY<span style="color: #009900;">&#40;</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>
&nbsp;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span>
        doN<span style="color: #009900;">&#40;</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>
&nbsp;
    <span style="color: #006600; font-style: italic;">// And so on...</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>The fictional author of this code obviously doesn&#8217;t know enough about JavaScript to make use of its various other constructs, most of them ten times more suited to this situation than an ugly switch statement. It&#8217;s not its appearance that truly annoys me though; it&#8217;s the knowledge that there are so many easier and more graceful ways of achieving such functionality.</p>

<p>Switch statements are considered as useful when you have a single variable and dependent on its value you want to carry out one of a number of different tasks. Using multiple if-else clauses doesn&#8217;t seem appropriate so the switch statement is often called upon to lend a hand. I&#8217;m sure most of you are familier with it and have seen it more than once before.</p>

<p>Let&#8217;s take the example from above; dependent on the value of <code>something</code> we run <code>doX</code>, <code>doY</code> or <code>doN</code>. In JavaScript, the same logic can be expressed with a simple lookup table in the form of an object:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cases <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> doX<span style="color: #339933;">,</span>
    <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> doY<span style="color: #339933;">,</span>
    <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span> doN
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cases<span style="color: #009900;">&#91;</span>something<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cases<span style="color: #009900;">&#91;</span>something<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>Not only is this cleaner but it&#8217;s also reusable, modifiable and accessible&#8230; All the conditions are saved as part of an object, so if you ever need to recall or change those conditions then it&#8217;s as easy as <code>cases[condition]</code>.</p>

<p>I suspect this covers about 90% of situations where a switch-statement is used; the remaining 10% are probably legitimate uses. Using a lookup table as an alternative to the Switch statement is not new; in fact this is the only way such logic can be expressed in both the Python and Lua languages, both of which have no support for switch statements.</p>

<p>So, my message is the following: do not use switch-case constructs unless absolutely necessary. Why? - Because there are better alternatives; simple as that!</p>

<p>Read more about the &#8220;Switch Statement&#8221; here: <a href="http://en.wikipedia.org/wiki/Switch_statement">en.wikipedia.org/wiki/Switch_statement</a>.</p>

]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/feed/</wfw:commentRss>
		</item>
		<item>
		<title>&#8230; a pointless headline</title>
		<link>http://james.padolsey.com/general/a-pointless-headline/</link>
		<comments>http://james.padolsey.com/general/a-pointless-headline/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 14:02:52 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=984</guid>
		<description><![CDATA[I have no doubt, in some cultures, industries, societies and organisations the individual is assumed guilty until being proven innocent. Regardless of law, a.k.a pen on paper, people as a modern &#8220;species&#8221; seem to deny their peers the very respect&#8230;]]></description>
			<content:encoded><![CDATA[<p>I have no doubt, in some cultures, industries, societies and organisations the individual is assumed guilty until being proven innocent. Regardless of law, a.k.a pen on paper, people as a modern &#8220;species&#8221; seem to deny their peers the very respect which they themselves strive for. You don&#8217;t have to look far; the morning papers will provide suitable confirmation&#8230; we&#8217;re falling apart, and it&#8217;s been happening since the dawn of the Human.</p>

<p>I think very little about topics of this genre because I&#8217;m normally left depressed and far from reality; I rather put thinking time into modern distractions like media, the internet and other trivial pursuits. But sometimes, you&#8217;re forced into a mental corner and it makes you wonder about things far from life, like life itself and the point in you existing.</p>

<p>I won&#8217;t bore you with realisations you&#8217;ve probably made before; I&#8217;m sure my thoughts have been thought a thousand times before and that almost makes this tiny endeavor entirely pointless. I won&#8217;t stop though, because I think this website deserves a bit of character; I rather be more than just a code-monger; I rather be a human in your eyes.</p>

<p>I guess that&#8217;s the thing about the internet; it reduces a persons effort and character to nothing more than a few words on a page. It&#8217;s a reductive medium; like the TV, or radio, or a written publication. As a reader, viewer or listener you&#8217;re never going to be able to fully appreciate the emotions behind the medium. Because of this, everything is constantly belittled!</p>

<p>What you may not realise when using these mediums is how just one sign of gratitude can make an entire endeavor worth the effort&#8230; And I&#8217;m not talking about me or anything related to me; I&#8217;m talking about everything and everyone! Even the most altruistic people on earth would not deny gratitude; in a way, it makes their actions all the more significant. It&#8217;s not the words themselves that are important; it&#8217;s the fact someone has said them!</p>

<p>Other times, the words mean nothing, because we hear them so often. When you read a paper I bet you rarely really think about what you&#8217;re reading; oh, another bomb killing 50 people, another dead celebrity, another abused child dead&#8230; These headlines come easy off my tongue because I hear them so often; they&#8217;ve lost all meaning. Do you feel pain when you hear that 200 people have died, regardless of cause? Does it mean anything to you, or is it yet another headline describing the unfortunate fate of someone you&#8217;ve never met?</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/general/a-pointless-headline/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Retaining a reference, the simple way</title>
		<link>http://james.padolsey.com/javascript/retaining-a-reference-the-simple-way/</link>
		<comments>http://james.padolsey.com/javascript/retaining-a-reference-the-simple-way/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 16:29:29 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[Code Snippets]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[DOM]]></category>

		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=972</guid>
		<description><![CDATA[Something that bugs me, especially when I'm in the depths of a nasty-looking jQuery chain, is the fact that there's no way to reference the last selection, that is, without explicitly assigning it to a variable]]></description>
			<content:encoded><![CDATA[<p>Something that bugs me, especially when I&#8217;m in the depths of a nasty-looking jQuery chain, is the fact that there&#8217;s no way to reference the last selection, that is, without explicitly assigning it to a variable.</p>

<p>Look at the following code:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#something'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">/* NOT POSSIBLE */</span></pre></div></div>




<p>This is what I&#8217;d really love to be able to do (<code>$('#something')</code> is assigned to <code>this</code> in the background). But, unfortunately, there&#8217;s no way to change the value of <code>this</code> (because it&#8217;s immutable); plus jQuery provides us with no way to grapple the previous selection. So, unless I want to mess around with functions as setters there&#8217;s really only two ways of doing what I want:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// #1 (BAD PRACTICE - selecting twice)</span>
$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#something'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#something'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// #2</span>
<span style="color: #003366; font-weight: bold;">var</span> something <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#something'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
something.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span> something.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>So now we&#8217;re left with only one acceptable way of doing this; assigning <code>$('#something')</code> to a variable and then referencing that variable from that point onwards.</p>

<p>I accept that this is the &#8220;best practice&#8221; but, to be honest, I really hate it&#8230; Just looking at it; it seems unnecessary. And I don&#8217;t want to mess around with functions as setters because that just makes the code even more bloated. Instead, I&#8217;d much rather have a property under the jQuery namespace that will always be referencing the last selection. Something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#something'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span> $._this.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">/* $('#something') === $._this */</span></pre></div></div>




<p>And as the code progresses, <code>$._this</code> is re-assigned continually; always reflecting the previous selection.</p>

<p>Maybe, in the future, the jQuery team will choose to implement something like this, but, for now, it&#8217;s possible using the following &#8220;hack&#8221;:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>_jQueryInit<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>selector<span style="color: #339933;">,</span> context<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>jQuery._this <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> _jQueryInit<span style="color: #009900;">&#40;</span>selector<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p><strong>NOTE</strong>: This is not the same as <code>jQuery(something).prevObject</code>.</p>

<p>I know its usefulness may not be immediately apparent to all of you; and I don&#8217;t blame you if you don&#8217;t find this technique appealing, but, before you make your decision, consider that its usage is quite specific and I think you&#8217;ll only really understand its appeal when you&#8217;ve encountered a situation that requires it.</p>
]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/retaining-a-reference-the-simple-way/feed/</wfw:commentRss>
		</item>
		<item>
		<title>&#8220;wordwrap&#8221; for JavaScript</title>
		<link>http://james.padolsey.com/javascript/wordwrap-for-javascript/</link>
		<comments>http://james.padolsey.com/javascript/wordwrap-for-javascript/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 08:17:01 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[Code Snippets]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=968</guid>
		<description><![CDATA[This function emulates PHP's <code>wordwrap</code>. It takes four arguments]]></description>
			<content:encoded><![CDATA[<p>This function emulates PHP&#8217;s <code><a href="http://us3.php.net/manual/en/function.wordwrap.php">wordwrap</a></code>. It takes four arguments:</p>

<ul>
    <li>The string to be wrapped.</li>
    <li>The column width (a number, default: 75)</li>
    <li>The character(s) to be inserted at every break. (default: &#8216;\n&#8217;)</li>
    <li>The cut: a Boolean value (false by default). See <a href="http://us3.php.net/manual/en/function.wordwrap.php">PHP docs</a> for more info.</li>
</ul>

<h2>The code</h2>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> wordwrap<span style="color: #009900;">&#40;</span> str<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> brk<span style="color: #339933;">,</span> cut <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    brk <span style="color: #339933;">=</span> brk <span style="color: #339933;">||</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">;</span>
    width <span style="color: #339933;">=</span> width <span style="color: #339933;">||</span> <span style="color: #CC0000;">75</span><span style="color: #339933;">;</span>
    cut <span style="color: #339933;">=</span> cut <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> str<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> regex <span style="color: #339933;">=</span> <span style="color: #3366CC;">'.{1,'</span> <span style="color: #339933;">+</span>width<span style="color: #339933;">+</span> <span style="color: #3366CC;">'}(<span style="color: #000099; font-weight: bold;">\\</span>s|$)'</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>cut <span style="color: #339933;">?</span> <span style="color: #3366CC;">'|.{'</span> <span style="color: #339933;">+</span>width<span style="color: #339933;">+</span> <span style="color: #3366CC;">'}|.+$'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'|<span style="color: #000099; font-weight: bold;">\\</span>S+?(<span style="color: #000099; font-weight: bold;">\\</span>s|$)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> str.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span> RegExp<span style="color: #009900;">&#40;</span>regex<span style="color: #339933;">,</span> <span style="color: #3366CC;">'g'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span> brk <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>




<h2>Usage</h2>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">wordwrap<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The quick brown fox jumped over the lazy dog.'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>





<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">The quick brown fox &lt;br/&gt;
jumped over the lazy &lt;br/&gt;
dog.</pre></div></div>




<p>I know there are other solutions out there; the ones I saw seemed a bit slow though, not to mention unnecessarily complicated/bloated&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/wordwrap-for-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Monitoring DOM properties</title>
		<link>http://james.padolsey.com/javascript/monitoring-dom-properties/</link>
		<comments>http://james.padolsey.com/javascript/monitoring-dom-properties/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 17:31:01 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[DOM]]></category>

		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=956</guid>
		<description><![CDATA[I recently came across <a href="http://plugins.jquery.com/project/watch">a jQuery plugin</a> that claims to emulate the functionality of <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch"><code>Object.prototype.watch</code></a>, a method inherent in all objects that enables you to watch for a property change and run a function upon that change. Of course, this isn&#8217;t&#8230;]]></description>
			<content:encoded><![CDATA[<p>I recently came across <a href="http://plugins.jquery.com/project/watch">a jQuery plugin</a> that claims to emulate the functionality of <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch"><code>Object.prototype.watch</code></a>, a method inherent in all objects that enables you to watch for a property change and run a function upon that change. Of course, this isn&#8217;t available in all implementations. IE, for example, doesn&#8217;t support this but has a DOM equivalent, <code>onPropertyChange</code>. Both methods are useful but their slightly different functionality makes it impossible to use either of them to create a unified solution. Again we&#8217;re left to resort to the lowest common denominator, <code>setInterval</code>.</p>

<p>The jQuery plugin I mentioned attempts to use <code>watch()</code> if it&#8217;s available, otherwise it uses <code>setInterval</code>, repeatadly iterating through a stack of objects checking for changes. Even this attempt at a unified solution is futile because the functionality behind <code>watch()</code> is fundamentally unachievable with <em>cross-browser</em> JavaScript. According to <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch">this MDC documentation</a>, the <code>watch</code> method not only acts as a type of event registrar for property-changes but also has the capability to define the property&#8217;s value (following the attempted change). In other words, the psuedo-event from <code>watch()</code> will fire before the property is actually changed, not after - the return value from the handler becomes the actual value of that property. Therefore, any attempts at emulation (for example, with <code>setInterval</code>) will not succeed since we&#8217;re only able to fire our psuedo-event <em>after</em> the change.</p>

<p>The following example shows how you would use <code>watch()</code> - at no time does the property actually change to &#8216;456&#8242;, it&#8217;s constantly &#8216;123&#8242;:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> prop<span style="color: #339933;">:</span> <span style="color: #CC0000;">123</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
obj.<span style="color: #660066;">watch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'prop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>propertyName<span style="color: #339933;">,</span> oldValue<span style="color: #339933;">,</span> newValue<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> oldValue<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
obj.<span style="color: #660066;">prop</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">456</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">prop</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 123</span></pre></div></div>




<p>Additionally, <code>watch()</code> does not recognise internal property changes (within the DOM). If you setup a watch on the <code>value</code> property of an <code>input</code> element, even when you type into that field, your watch-handler will not execute. I&#8217;m not sure if this is intentional but it makes this technique entirely useless for monitoring DOM properties.</p>

<p>So, it appears that the only way to monitor DOM properties consistently across all browsers is to only use <code>setInterval</code>. The following plugins, <code>watch</code> and <code>unwatch</code> work quite well:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">watch</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> id<span style="color: #339933;">,</span> fn <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> oldVal <span style="color: #339933;">=</span> self<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>
            <span style="color: #3366CC;">'watch_timer'</span><span style="color: #339933;">,</span>
            setInterval<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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> oldVal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    fn.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> id<span style="color: #339933;">,</span> oldVal<span style="color: #339933;">,</span> self<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    oldVal <span style="color: #339933;">=</span> self<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> self<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">unwatch</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> id <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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>
        clearInterval<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'watch_timer'</span><span style="color: #009900;">&#41;</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;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Plus, I finally found use for jQuery.data()! ;)</span></pre></div></div>




<p>These can be used on any type of object, an example:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">watch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>propName<span style="color: #339933;">,</span> oldVal<span style="color: #339933;">,</span> newVal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Value has been changed to '</span> <span style="color: #339933;">+</span> newVal<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;
<span style="color: #006600; font-style: italic;">// or...</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> prop<span style="color: #339933;">:</span> <span style="color: #CC0000;">123</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">watch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'prop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>propName<span style="color: #339933;">,</span> oldVal<span style="color: #339933;">,</span> newVal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Prop has been changed to '</span> <span style="color: #339933;">+</span> newVal<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></div></div>




<p>I went ahead and created a new special event for jQuery using the plugin:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">valuechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'valuechange'</span><span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
jQuery.<span style="color: #660066;">event</span>.<span style="color: #660066;">special</span>.<span style="color: #660066;">valuechange</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    setup<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>
&nbsp;
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">watch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</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>
            jQuery.<span style="color: #660066;">event</span>.<span style="color: #660066;">handle</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span><span style="color: #3366CC;">'valuechange'</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    teardown<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>
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unwatch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Usage:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'valuechange'</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>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Value changed... New value: '</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><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>This event is useful when you want to be notified every single time the value of an input element changes. No native DOM event will do this.</p>

<p>If there&#8217;s one thiing you should take away from all this then it&#8217;s this: if you are going to try making a browser-specific feature available across all browsers you should make sure that you match the functionality precisely, otherwise it&#8217;s worthless. For example, an <code>Array.prototype.forEach</code> method that passes the index as the first parameter instead of the second is useless. If you can&#8217;t match the functionality then simply create your own abstraction.</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/monitoring-dom-properties/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Microsoft, you make me sick!</title>
		<link>http://james.padolsey.com/general/microsoft-you-make-me-sick/</link>
		<comments>http://james.padolsey.com/general/microsoft-you-make-me-sick/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 19:58:08 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[IE]]></category>

		<category><![CDATA[Microsoft]]></category>

		<category><![CDATA[Rant]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=943</guid>
		<description><![CDATA[I&#8217;m not normally one to protest so distastefully but Microsoft&#8217;s last move has really pissed me off. Apparently, their new devil-child, IE8, beats Mozilla Firefox <em>and</em> Google Chrome in areas such as &#8220;Reliability&#8221;, &#8220;Compatibility&#8221; and &#8220;Developer Tools&#8221;; at least, according to&#8230;]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not normally one to protest so distastefully but Microsoft&#8217;s last move has really pissed me off. Apparently, their new devil-child, IE8, beats Mozilla Firefox <em>and</em> Google Chrome in areas such as &#8220;Reliability&#8221;, &#8220;Compatibility&#8221; and &#8220;Developer Tools&#8221;; at least, according to <a href="http://www.microsoft.com/windows/internet-explorer/get-the-facts/browser-comparison.aspx">this chart</a> (<a href="http://james.padolsey.com/wp-content/uploads/chart.png">IMG</a>)!</p>

<p>Apparently IE8 considers itself more secure and easier to use than other browsers too!</p>

<p>I can&#8217;t even begin to express my absolute opposition to every speck of misinformation on that chart. How dare Microsoft mislead consumers to such a degree - I know they put financial superiority and their monopoly above all other human values but this doesn&#8217;t excuse their flagrant disregard of their customers&#8217; intelligence.</p>

<p>Consumerism is only good for one thing in my opinion - creating arrogant, possibly corrupt and more annoyingly, ignorant individuals. The people running Microsoft and the various departments involved must either be unquestionable morons or just so manipulated by the culture of consumerism, personified solely by that retched corporation, that they are beyond help!</p>

<p>In the past I&#8217;ve been quite calm and civilian in my dealings with the atrocity known by some only as &#8220;IE&#8221;, but now I feel a new spark of anger has been born; Microsoft has forever lost my respect and confidence in the prospect of change.</p>

<p>I know, I know! Marketing and advertising, it&#8217;s all about lies, spin and manipulation&#8230; But, that doesn&#8217;t mean it&#8217;s right, and yes, there is room in our capitalist cultures for honesty.</p>

<p>I want to have a long and hard chat with the heads of the IE team; I&#8217;d love to hear their twisted take on reality - maybe one of them could shed some light on the unacceptable marketing drivel that&#8217;s been circling the net, which, by the way, not only includes the chart I mentioned, but <a href="http://www.microsoft.com/australia/ie8/competition/">check this out</a>! Yes, not only are Microsoft capable of releasing lies in the form of propaganda but they&#8217;ve also developed a system that will insult the browser you&#8217;ve chosen to use unless, of course, you&#8217;re using Internet Explorer, but then why would you?</p>

<p>The marketing team responsible for the latest scam should publicly apologize and then be permanently banned from every touching (or speaking about) anything remotely related to technology again!</p>

<p>It&#8217;s fair enough when Microsoft slam their own previous products but attacking Google Chrome and more shockingly, Mozilla Firefox, is way below the belt!</p>
]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/general/microsoft-you-make-me-sick/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Zoomer component for sale!</title>
		<link>http://james.padolsey.com/javascript/zoomer-component-for-sale/</link>
		<comments>http://james.padolsey.com/javascript/zoomer-component-for-sale/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 09:23:49 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[Cool Stuff]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[News]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[Zoomer]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=934</guid>
		<description><![CDATA[<a href="http://themeforest.net?ref=JimmyP">ThemeForest</a>, one of Envato&#8217;s marketplaces, has just launched a &#8220;<a href="http://themeforest.net/category/javascript?ref=JimmyP">JavaScript</a>&#8221; category and within it my new JavaScript component, &#8220;<a href="http://themeforest.net/item/zoomer/46445?ref=JimmyP">Zoomer</a>&#8220;. As the description states, &#8220;&#8216;Zoomer&#8217; enables your users to closely inspect images by hovering their cursor over them. It’s been built&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://themeforest.net?ref=JimmyP">ThemeForest</a>, one of Envato&#8217;s marketplaces, has just launched a &#8220;<a href="http://themeforest.net/category/javascript?ref=JimmyP">JavaScript</a>&#8221; category and within it my new JavaScript component, &#8220;<a href="http://themeforest.net/item/zoomer/46445?ref=JimmyP">Zoomer</a>&#8220;. As the description states, &#8220;&#8216;Zoomer&#8217; enables your users to closely inspect images by hovering their cursor over them. It’s been built as a robust, unobtrusive and highly customizable jQuery plugin.&#8221; Here&#8217;s a brief overview of its key features:</p>

<ul>
    <li>A fully customizable zoomer for use with any image</li>
    <li>Works in all modern browsers (including IE6 )</li>
    <li>This is a progressive enhancement; images will still be viewable when JavaScript is not available.</li>
    <li>The Zoomer itself is highly customizable.</li>
    <li>The Zoomer requires NO images – the shadows/gradients are dynamically generated by JavaScript (using VML /Canvas technologies)</li>
    <li>In-depth documentation including <strong>two videos</strong> and an FAQ !</li>
</ul>

<p class="video"><a href="http://themeforest.net/item/zoomer/full_screen_preview/46445?ref=JimmyP"><strong>See the demo</strong></a></p>

<h2>It&#8217;s customizable!</h2>

<p>Almost every aspect of the zoomer is customizable, here&#8217;s a preview of the jQuery options:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">zoomer</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    zoomerClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'_zoomer'</span><span style="color: #339933;">,</span>
    zoomSrc<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
    height<span style="color: #339933;">:</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span>
    mousewheelZoom<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    defaultZoom<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
    maxZoom<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
    minZoom<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
    feedback<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>zoom<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onOver<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: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onOut<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: #009900;">&#125;</span><span style="color: #339933;">,</span>
    zoomerOverlay<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        className<span style="color: #339933;">:</span> <span style="color: #3366CC;">'_zoomer-overlay'</span><span style="color: #339933;">,</span>
        shadowWidth<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
        shadowOpacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span>
        <span style="color: #009966; font-style: italic;">/* Must be in &quot;rgb(r,g,b)&quot; format */</span>
        shadowColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'rgb(0,0,0)'</span><span style="color: #339933;">,</span>
        radialOpacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.4</span><span style="color: #339933;">,</span>
        <span style="color: #009966; font-style: italic;">/* Must be in &quot;rgb(r,g,b)&quot; format */</span>
        radialColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'rgb(0,0,0)'</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>




<p>Even the shadow beneath the Zoomer is customizable - this is possible thanks to canvas/VML technologies.</p>

<p class="video">
    <a href="http://themeforest.net/item/zoomer/46445?ref=JimmyP"><strong>Buy Zoomer (currently $5)!</strong></a>
</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/zoomer-component-for-sale/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Metadata within HTML comments</title>
		<link>http://james.padolsey.com/javascript/metadata-within-html-comments/</link>
		<comments>http://james.padolsey.com/javascript/metadata-within-html-comments/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 09:29:50 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[Code Snippets]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Data]]></category>

		<category><![CDATA[Github]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=928</guid>
		<description><![CDATA[Recently, I discussed the pitfalls of using custom HTML attributes and "expando" DOM properties to store per-element data. Thanks to the comments on that post I learnt that sometimes doing so is hard to avoid. Some people resort to]]></description>
			<content:encoded><![CDATA[<p>Recently, <a href="http://james.padolsey.com/javascript/its-just-an-api/">I discussed the pitfalls</a> of using custom HTML attributes and &#8220;expando&#8221; DOM properties to store per-element data. Thanks to the comments on that post I now understand that doing so is sometimes hard to avoid. Some people resort to using jQuery&#8217;s &#8220;<a href="http://plugins.jquery.com/project/metadata">metadata</a>&#8221; plugin which can massively simplify the process of adding metadata (i.e. data about data). I&#8217;ve been meaning to post a technique I&#8217;ve been using recently; keeping data in HTML comments and then extracting it and tying it to the closest (parent) element.</p>

<p>With this technique you can do the following:</p>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;foo&quot;</span>&gt;</span>
    <span style="color: #808080; font-style: italic;">&lt;!-- { someData: 123, aString: &quot;bar&quot; } --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span> ... regular content ... <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>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- Regular comments can still be used --&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--{specialID:1234}--&gt;</span>... comment-data is tied to the parent element...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></div></div>




<p>Using the &#8220;<a href="http://github.com/jamespadolsey/commentData/tree/master">commentData</a>&#8221; function you can extract that information and use it within your JavaScript, e.g.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> fooData <span style="color: #339933;">=</span> commentData<span style="color: #009900;">&#40;</span> foo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; {someData: 123, aString: &quot;bar&quot;}</span>
fooData.<span style="color: #660066;">someData</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &lt;= 123</span></pre></div></div>




<p class="video"><a href="http://github.com/jamespadolsey/commentData/tree/master">Download &#8220;commentData&#8221; from Github</a></p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/metadata-within-html-comments/feed/</wfw:commentRss>
		</item>
		<item>
		<title>It&#8217;s just an API</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/</link>
		<comments>http://james.padolsey.com/javascript/its-just-an-api/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 11:53:32 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[HTML/XHTML]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[DOM]]></category>

		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=916</guid>
		<description><![CDATA[Thousands of developers interact with this API on a daily basis yet most forget it is <em>just</em>&#8230; an API! Of course, I&#8217;m talking about the notorious &#8220;<a href="http://en.wikipedia.org/wiki/Document_Object_Model">Document Object Model</a>&#8221; (a.k.a the DOM). With the rise of &#8220;Rich Internet Applications&#8221; interaction&#8230;]]></description>
			<content:encoded><![CDATA[<p>Thousands of developers interact with this API on a daily basis yet most forget it is <em>just</em>&#8230; an API! Of course, I&#8217;m talking about the notorious &#8220;<a href="http://en.wikipedia.org/wiki/Document_Object_Model">Document Object Model</a>&#8221; (a.k.a the DOM). With the rise of &#8220;Rich Internet Applications&#8221; interaction with the DOM has sky-rocketed and as a result it&#8217;s being heavily abused!</p>

<p>Have you ever needed to do this? -</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// E.g. 1</span>
element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><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;">this</span>.<span style="color: #660066;">rel</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">' clicked'</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">clientX</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ,'</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">clientY</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// E.g. 2</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseover</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: #003366; font-weight: bold;">var</span> color <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beforeColor'</span><span style="color: #339933;">,</span> color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    changeColor<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</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></pre></div></div>




<p>If you have then (in my humble opinion) you&#8217;re abusing the DOM!</p>

<p>The first example is obviously bad practice; using HTML attributes to track changes unrelated to the HTML content is a crime of sorts and should never be done! The second example probably doesn&#8217;t seem as bad to most developers. jQuery&#8217;s &#8220;data&#8221; method allows you to store expando-like data about an element without obtrusively defining it as a direct property. This technique of storing element data is far better than using the HTML <code>rel</code> attribute. Or is it?</p>

<p>I was an advocate of this particular technique but I&#8217;ve come to realise that it is, in most situations, no better than using any random HTML attribute. If we put the unobtrusive-vs-obtrusive argument aside, both techniques are effectively the same. Plus, jQuery&#8217;s &#8220;data&#8221; technique is not <em>really</em> unobtrusive - jQuery still uses an expando property to tie the element to a particular property in a JavaScript object; I&#8217;m not saying there&#8217;s anything wrong with this, I&#8217;m just pointing out the common misconception that using jQuery&#8217;s <code>data</code> method is somehow cleaner than using a random or made-up HTML attribute (/DOM property).</p>

<p>The main reason I am now so sceptical about its usage is because, over time, I&#8217;ve seen a lot of cases that don&#8217;t really require it at all and could quite easily be achieved with a well-thought-out use of JavaScript closures. Obviously it&#8217;s not all bad; there are a few valid uses - for example, jQuery currently uses its own &#8220;data&#8221; method to tie events to elements/objects; a central part of the its event dispatch system.</p>

<h2>Magical closures</h2>

<p>The nature of JavaScript closures allows us to use regular variables to store progress or states and then access those variables from within nested functions. I&#8217;ve put together a short example; this is an outline of a drag-and-drop script:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Library neutral/agnostic</span>
&nbsp;
draggableElements.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> down <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
        y <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    elem.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousedown'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        down <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        x <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span> <span style="color: #339933;">-</span> getOffset<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">left</span><span style="color: #339933;">;</span>
        y <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageY</span> <span style="color: #339933;">-</span> getOffset<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    document.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><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>down<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            elem.<span style="color: #660066;">applyCSS</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                left<span style="color: #339933;">:</span> e.<span style="color: #660066;">pageX</span> <span style="color: #339933;">-</span> x<span style="color: #339933;">,</span>
                top<span style="color: #339933;">:</span> e.<span style="color: #660066;">pageY</span> <span style="color: #339933;">-</span> y
            <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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    document.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseup'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        down <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><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>Every &#8220;draggable&#8221; element is given its very own closure (through <code>forEach</code>) within which we can define numerous variables accessible in each of the nested &#8220;handler&#8221; functions (&#8217;mousemove&#8217;, &#8216;mousedown&#8217;, etc.). No expando properties are required, <em>nada</em>!</p>

<p>I haven&#8217;t got much more to say about this; the above code pretty much sums up my point!</p>

<p>I&#8217;m interested in valid uses of jQuery&#8217;s <code>data()</code> method (other than those used in the core); someone please enlighten me!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/its-just-an-api/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Best practices &amp; JSLint</title>
		<link>http://james.padolsey.com/javascript/best-practices-jslint/</link>
		<comments>http://james.padolsey.com/javascript/best-practices-jslint/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 17:22:13 +0000</pubDate>
		<dc:creator>James</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Crockford]]></category>

		<category><![CDATA[JSLint]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=902</guid>
		<description><![CDATA[&#8220;<a href="http://jslint.com/">JSLint</a>&#8221; is a JavaScript &#8220;code quality tool&#8221;. If you&#8217;ve never heard of it then think of it as the <a href="http://validator.w3.org/">W3C HTML validator</a>, but for JavaScript. <a href="http://www.crockford.com/">Doug Crockford</a>, the genius that created JSLint, frequently claims that JavaScript is &#8220;The World&#8217;s Most&#8230;]]></description>
			<content:encoded><![CDATA[<p>&#8220;<a href="http://jslint.com/">JSLint</a>&#8221; is a JavaScript &#8220;code quality tool&#8221;. If you&#8217;ve never heard of it then think of it as the <a href="http://validator.w3.org/">W3C HTML validator</a>, but for JavaScript. <a href="http://www.crockford.com/">Doug Crockford</a>, the genius that created JSLint, frequently claims that JavaScript is &#8220;The World&#8217;s Most Misunderstood Programming Language&#8221;; if you want to know where he&#8217;s coming from with this statement then you&#8217;ll have to purchase his book, &#8220;<a href="http://www.amazon.com/exec/obidos/ASIN/0596517742/wrrrldwideweb">JavaScript: The Good Parts</a>&#8221; or, for a brief overview, watch the <a href="http://www.youtube.com/watch?v=hQVTIJBZook">Google Tech Talk</a>.</p>

<p>In some ways JavaScript could be considered just as lax as HTML when it comes to what is acceptable and what isn&#8217;t. For example, most browsers will let you get away with HTML that looks like this:</p>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span></span>
<span style="color: #009900;"><span style="color: #000066;">id</span> <span style="color: #66cc66;">=</span>list&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>One
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Two
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Three</pre></div></div>




<p>And, similarly, most, if not all, browsers will let you get away with JavaScript that looks like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> muahaha<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    myarray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    myarray<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name1'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;056&quot;</span>
    something <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span> myarray<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name1'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>something<span style="color: #339933;">==</span><span style="color: #CC0000;">56</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>myarray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>myarray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>




<p>How many things can you count in the above code that are either bad practice or just incorrect usage of the language? If we go by JSLint then there are a total of 16 problems with it! Plus there are a couple that aren&#8217;t noticed by JSLint (using an array as an object &#038; accessing <code>myarray.length</code> on every iteration). That&#8217;s 18 altogether! Yet, all browsers will run this code quite happily without throwing a single error.</p>

<p>Technically, it&#8217;s all legal JavaScript (minus the extra semi-colon at the end), so I&#8217;d expect all browsers to munch it up without complaining.</p>

<p>So, if it works<sup>1</sup>, then why should you take that extra step to ensure best practices; what&#8217;s the point and who will benefit?</p>
<span id="more-902"></span>

<p>For starters, there&#8217;s you! When you need to maintain or add to this code you&#8217;ll find yourself spending many more minutes doing so than if the original code has been written with best practices in mind. Although, honestly, <em>you</em> are the last person that <em>you</em> should be worrying about. What on earth is another developer going to make of this?</p>

<p>Good practices exist for a reason; not following them generally leads to bad things. Don&#8217;t use <a href="http://jslint.com/">JSLint</a> because you <em>have</em> to, use it because you <em>want</em> to!</p>

<p><sup>1</sup> - <small>The function won&#8217;t work as expected (<code>parseInt('056') !== 56</code>) but it will &#8220;work&#8221;; as in, it won&#8217;t throw any errors. Actually, just completely ignore the <code>muahaha</code> function; it doesn&#8217;t do anything; it&#8217;s just an example of awful code.</small></p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/best-practices-jslint/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.748 seconds -->
<!-- Cached page generated by WP-Super-Cache on 2009-07-05 02:46:38 -->
