<?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>ReadyState4 &#187; JavaScript</title>
	<atom:link href="http://readystate4.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://readystate4.com</link>
	<description>JavaScript, Web Development, Ruby, and Technology.</description>
	<lastBuildDate>Fri, 04 Jun 2010 19:42:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Great article for learning JavaScript for existing programmers</title>
		<link>http://readystate4.com/2010/06/04/great-article-for-learning-javascript-for-existing-programmers/</link>
		<comments>http://readystate4.com/2010/06/04/great-article-for-learning-javascript-for-existing-programmers/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 19:36:39 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://readystate4.com/?p=67</guid>
		<description><![CDATA[I&#8217;ve been teaching JavaScript to a person with no programming experience and one with lots of programming experience in other languages. Both situations can be tough as one has to learn certain programing concepts (as well as how the web generally works) and the other must unlearn some that don&#8217;t apply here (like getting out [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been teaching JavaScript to a person with no programming experience and one with lots of programming experience in other languages. Both situations can be tough as one has to learn certain programing concepts (as well as how the web generally works) and the other must unlearn some that don&#8217;t apply here (like getting out of the strongly-typed world and appreciating the benefits of duck-typing).</p>
<p>Explaining various concepts in JS such as prototypes, closures, and even &#8220;literal&#8221; shorthand is interesting. I find the questions from both ends  great—sometimes even challenging to explain which actually strengthens me as a JS developer. Still some concepts are hard to explain and the JavaScript Rhino book is too large and boring to dish out as homework. Then I stumbled across this page by Simon Willison which was written as a re-introduction to JavaScript but also makes a perfect condensed overview of what JS is for existing programmers: <a href="https://developer.mozilla.org/en/A_re-introduction_to_JavaScript">https://developer.mozilla.org/en/A_re-introduction_to_JavaScript</a>. It even goes over a little history of the language and how to avoid nasty memory leaks in IE (circular references). Saves us a lot of time and explaining so that we can continue with more complex topics.</p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2010/06/04/great-article-for-learning-javascript-for-existing-programmers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript Memoization</title>
		<link>http://readystate4.com/2009/02/25/javascript-memoization/</link>
		<comments>http://readystate4.com/2009/02/25/javascript-memoization/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 17:18:30 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://readystate4.com/2009/02/25/javascript-memoization/</guid>
		<description><![CDATA[I was taking a look at my friend Takashi&#8217;s JavaScript tweener which isn&#8217;t released yet and noticed he setup his code in a particular way for a few methods that get called dozens of times a second. He explained to me that it was for optimization through memoization. I ran a test case, and indeed [...]]]></description>
			<content:encoded><![CDATA[<p>I was taking a look at my friend Takashi&#8217;s JavaScript tweener which isn&#8217;t released yet and noticed he setup his code in a particular way for a few methods that get called dozens of times a second. He explained to me that it was for optimization through <a href="http://en.wikipedia.org/wiki/Memoization" title="Wikipedia entry">memoization</a>. I ran a test case, and indeed it&#8217;s much faster. Check it out:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">a <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">deep</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">deep</span>.<span style="color: #660066;">variable</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    here<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> plain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">deep</span>.<span style="color: #660066;">variable</span>.<span style="color: #660066;">here</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> memoized<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> z <span style="color: #339933;">=</span> a.<span style="color: #660066;">deep</span>.<span style="color: #660066;">variable</span>.<span style="color: #660066;">here</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</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>
        document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>z<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;">&#125;</span>
&nbsp;
&nbsp;
now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> max <span style="color: #339933;">=</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> max<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        plain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> now<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> max <span style="color: #339933;">=</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> max<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        memoized<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> now<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// plain:  759</span>
<span style="color: #006600; font-style: italic;">// memoized: 36</span></pre></div></div>

<p>There&#8217;s a more complex example on the subject posted a month ago on <a href="http://ajaxian.com/archives/speed-up-your-javascript-with-memoization">Ajaxian</a> and an easier to understand one on Oliver Steele&#8217;s <a href="http://osteele.com/archives/2006/04/javascript-memoization">blog</a> written back in 2006.</p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2009/02/25/javascript-memoization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The difference between != and =!</title>
		<link>http://readystate4.com/2008/12/31/the-difference-between-and/</link>
		<comments>http://readystate4.com/2008/12/31/the-difference-between-and/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 17:38:10 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/12/31/the-difference-between-and/</guid>
		<description><![CDATA[I was helping a friend troubleshoot some of her code today and found the culprit to be a typo in one of her conditional statements:

// var USERID = 12345;
if &#40;USERID =! null&#41;&#123;
     // code
&#125;

Her expression was always resolving to true because she had inverted her != (&#8220;not-null&#8221;) check which was actually [...]]]></description>
			<content:encoded><![CDATA[<p>I was helping a friend troubleshoot some of her code today and found the culprit to be a typo in one of her conditional statements:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// var USERID = 12345;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>USERID <span style="color: #339933;">=!</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Her expression was always resolving to <code>true</code> because she had inverted her <code>!=</code> (&#8220;not-null&#8221;) check which was actually assigning her variable to the return of a statement evaluation. Her mistake would be clearer to understand formatted as such:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// var USERID = 12345;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>USERID <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Since the inverse of <code>false</code>, <code>null</code>, and <code>NaN</code> is <code>true</code> her conditional statement (also an assignment) would always return <code>true</code>. </p>
<p>Which lead me to thinking, the not operator is a great way to write succinct code. In the right context, using the not operator in the right side of an assignment isn&#8217;t a bad idea.</p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/12/31/the-difference-between-and/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improve your jQuery &#8211; 25 excellent tips</title>
		<link>http://readystate4.com/2008/12/16/improve-your-jquery-25-excellent-tips/</link>
		<comments>http://readystate4.com/2008/12/16/improve-your-jquery-25-excellent-tips/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 01:23:04 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/12/16/improve-your-jquery-25-excellent-tips/</guid>
		<description><![CDATA[
Here&#8217;s a nice write-up by Jon Hobbs-Smith at tvidesign.com detailing 25 tips on using jQuery as well as regular JavaScript advice. Most of these tips probably aren&#8217;t anything new for the advanced jQuery user (Jon considers himself intermediate, I think that&#8217;s modest), however I love lists like this because there&#8217;s a treasure trove worth of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx"><img src="http://readystate4.com/images/blog/tvidesign_banner.jpg" style="padding-left: 0px;" /></a><br />
Here&#8217;s a nice write-up by Jon Hobbs-Smith at tvidesign.com detailing <a href="http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx">25 tips on using jQuery</a> as well as regular JavaScript advice. Most of these tips probably aren&#8217;t anything new for the advanced jQuery user (Jon considers himself intermediate, I think that&#8217;s modest), however I love lists like this because there&#8217;s a treasure trove worth of experience being shared. Altruism at it&#8217;s best!</p>
<p>I plan on writing more entries on my own tips and tricks with JavaScript and jQuery as well as my methods of debugging in various browsers, compression techniques, etc., but in the meantime, I&#8217;ll just comment on Jon&#8217;s list: </p>
<p><strong>2. Use a [jQuery] cheat sheet</strong></p>
<p>I think <a href="http://visualjquery.com/">Visual jQuery</a> is all you need. And now that there&#8217;s live search and clickable examples it&#8217;s even better.</p>
<p><strong>3. Combine all your scripts and minify them</strong></p>
<p>Dead Edward&#8217;s packer is awesome for minifying, but before I do that, I like to run my code through <a href="http://www.jslint.com/">jSlint</a> to make sure it&#8217;s all valid. I ignore line-break errors as I don&#8217;t agree in many cases that it&#8217;s an issue.</p>
<p>Additionally, when I work on large JS codebases with a lot of other people who may not code as strictly as I do, I like to use <a href="http://www.crockford.com/javascript/jsmin.html">JSmin</a> on the minimal setting (cut-and-paste version <a href="http://fmarcia.info/jsmin/test.html">here</a>). The compression benefit is only slightly less and it preserves single linefeeds preventing JS errors where people forgot to end their statements with a semicolon. It also makes the code easier to read and debug in a production enviornment—great for the times you want the benefits of minification without the side effect of obfuscation.</p>
<p><strong>5. Keep selection operations to a minimum by caching</strong></p>
<p>I just wanted to comment in Ron&#8217;s example where he used:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myList <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.myList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>First, if I ever reference a jQuery collection in my scripts, I like to append the dollar sign character to the beginning of the variable name to signify that it is, in fact, a jQuery collection:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> $myList <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.myList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s a lot more helpful when you access that variable further down the road and don&#8217;t have to question whether it&#8217;s a jQuery wrapped set, an actual HTML element, or something else.</p>
<p>Secondly, whether you&#8217;re using a JS lib or not, I would never search for an HTML collection by class name alone; What the JavaScript engine is doing in the background is looping through every single element on the page which is very intensive. It would be better to find the closest parent container first or search by specific tag name(s):</p>
<p>Good:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> $myList <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.myList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//added benefit of knowing the type of element you'll</span>
<span style="color: #006600; font-style: italic;">// receive</span></pre></div></div>

<p>Better:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> $myList <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#closest_parent_id .myList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// searching the children of a parent</span>
<span style="color: #006600; font-style: italic;">// container instead of all the elements on the page</span></pre></div></div>

<p>Best:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> $myList <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#closest_parent_id ul.myList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// combination of both above</span></pre></div></div>

<p><strong>19. Use noconflict to rename the jquery object when using other frameworks</strong><br />
<strong>22. How to check if an element exists[...]</strong></p>
<p>As I&#8217;ve probably mentioned before, I always like to use <code>noConflict()</code> in all my projects. I move jQuery to the $j namespace and save <code>$</code> for a shorthand reference to <code>document.getElementById</code> &#8211; I think it&#8217;s what most js developers expect and <code>if($('myDiv))</code> is a lot simpler to write out than <code>if($('#myDiv).length !== 0)</code>  or even <code>if($('#myDiv).length)</code>.</p>
<p>Thanks for the link <a href="http://www.insecureweb.com/">Bryan</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/12/16/improve-your-jquery-25-excellent-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String.toCharCode</title>
		<link>http://readystate4.com/2008/10/27/stringtocharcode/</link>
		<comments>http://readystate4.com/2008/10/27/stringtocharcode/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 15:02:19 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/10/27/stringtocharcode/</guid>
		<description><![CDATA[I noticed that JavaScript has a String.fromCharCode for decoding a sequence of numbers to Unicode values but no String.toCharCode for doing the reverse. So here&#8217;s my rendition:

String.prototype.toCharCode = function&#40;&#41;&#123;
    var r = '', string = this.split&#40;''&#41;;
    for &#40;var i in string&#41;&#123;
        r [...]]]></description>
			<content:encoded><![CDATA[<p>I noticed that JavaScript has a <a href="https://developer.mozilla.org/En/Sandbox/JS/String.fromCharCode" title="Mozilla Definition">String.fromCharCode</a> for decoding a sequence of numbers to Unicode values but no String.toCharCode for doing the reverse. So here&#8217;s my rendition:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toCharCode</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span> string <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> string<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        r <span style="color: #339933;">+=</span> String.<span style="color: #660066;">charCodeAt</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">return</span> r.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>r.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #3366CC;">'bob'</span>.<span style="color: #660066;">toCharCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// returns &quot;98,111,98&quot;</span></pre></div></div>

<p class="update" style="padding: 5px; background-color: lightyellow;">
<strong>Update:</strong> version from my good friend <a href="http://blog.nydd.org/about/">Takashi</a> and link to his blog on <a href="http://blog.nydd.org/2008/10/28/process-string/">why it&#8217;s more efficient</a>:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toCharCode</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> str.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> work <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</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> len<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        work<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> String.<span style="color: #660066;">charCodeAt</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> work.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #3366CC;">'bob'</span>.<span style="color: #660066;">toCharCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// returns &quot;98,111,98&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/10/27/stringtocharcode/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Blogging live from jQuery Camp 2008 (@MIT in Boston)</title>
		<link>http://readystate4.com/2008/09/28/blogging-live-from-jquery-camp-2008-mit-in-boston/</link>
		<comments>http://readystate4.com/2008/09/28/blogging-live-from-jquery-camp-2008-mit-in-boston/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 19:02:50 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/09/28/blogging-live-from-jquery-camp-2008-mit-in-boston/</guid>
		<description><![CDATA[ So far it&#8217;s been great! Good food, good talks. Here&#8217;s the full topic list. (scroll down to the middle of the page)
Took some vids until my camera died, more to come, once I convert &#8216;em and upload &#8216;em. Here&#8217;s the most interesting things I learned so far:
jQuery:

jQuery does do DOM caching! It listens for [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.readystate4.com/images/blog/jquery.gif" style="float: right; padding: 0 0 5px 5px;" /> So far it&#8217;s been great! Good food, good talks. <a href="http://jquery.com/blog/2008/08/31/jquery-conference-2008-agenda/">Here&#8217;s the full topic list.</a> (scroll down to the middle of the page)</p>
<p>Took some <a href="http://www.youtube.com/watch?v=V-MHHtw6kPY">vids</a> until my camera died, more to come, once I convert &#8216;em and upload &#8216;em. Here&#8217;s the most interesting things I learned so far:</p>
<p>jQuery:</p>
<ul>
<li>jQuery <i>does</i> do DOM caching! It listens for DOM-modified events to clear it&#8217;s internal cache.</li>
<li>That random jquery[<i>random numbers</i>] variable that gets added to the window object is to uniquely identify the jQ instantiation (so you can do things like run multiple instances of jQuery on the same page).</li>
<li>I knew about <a href="http://docs.jquery.com/Traversing/end">.end()</a> but <a href="http://docs.jquery.com/Traversing/andSelf">.andSelf()</a>? Cool!</li>
<li>Nokia and Microsoft join the jQuery camp. Nokia will have jQuery built into their phones (they have some browser-like app environment) $M will have it in their VS/ASP.NET and all future versions of Visual Studio will come with it going foward. <a href="http://jquery.com/blog/2008/09/28/jquery-microsoft-nokia/">link</a> <a href="http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx">link</a>.</li>
<li>jQuery is planning to be the first library that doesn&#8217;t use any browser sniffing. They are going to use a &#8220;quirk check&#8221; method of creating some elements that aren&#8217;t added to the page that check for things like if white space gets removed when created an element that starts with a space (IE bug) and others. This way they don&#8217;t have to be constantly updating for new browsers.
<li>John&#8217;s working on a cool new DOM selector API called Sizzle that&#8217;s supposed to be a lot faster. It works by search for the last element in your query up. For example <code>$('div p');</code> would look for all p elements on the page that has a parent div element.</li>
<li>You can use create a namespace when binding events. For example <code>$('#foo').bind('click.bar', func);</code> so it&#8217;s easy to unbind later, and you can encapsulate your events for your own plugins to prevent conflicts.  You can also create your own custom events like <code>$('#foo').bind('apple', func);</code>  and trigger them using <code>$('#foo').trigger('apple');</code> .</li>
<li><code>$('#element').data();</code> lets you bind data to DOM elements (see Data Cache under Internals section <a href="http://remysharp.com/visual-jquery/">here</a>.) Since you can&#8217;t really bind an object to a DOM node jQuery assigns it a random key behind the scenes which it references. This is really handly for storing all kinds of data.
</ul>
<p>jQuery UI:<br />
- Uses the data method above A LOT.<br />
- Lots of enhancements, most interestingly, with drag-and-drop which detects floated elements and allows you to have draggable lists within draggable lists.<br />
- jQuery UI team also working on some experimental stuff for Webkit only (iPhone / Safari / Chrome).<br />
- Theming jQuery UI with <a href="http://ui.jquery.com/themeroller">ThemeRoller</a></p>
<p>Other:<br />
- I&#8217;ve been using this really old but handy site as a jQ reference: <a href="http://visualjquery.com/1.1.2.html">http://visualjquery.com/1.1.2.html</a> made by Yehuda Katz (also a speaker). Asking around on the conference IRC chan I found there&#8217;s a much newer / improved one at <a href="http://remysharp.com/visual-jquery/">http://remysharp.com/visual-jquery/</a> by Remy Sharp. They&#8217;re going to try to update the prior site with the latter.<br />
- Canvas tag is pretty basic but sweet. John Resig gave a whole presentation on it since the using jQuery with Firefox plugins couldn&#8217;t make it.</p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/09/28/blogging-live-from-jquery-camp-2008-mit-in-boston/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a synchronous DWR call</title>
		<link>http://readystate4.com/2008/09/18/making-a-synchronous-dwr-call/</link>
		<comments>http://readystate4.com/2008/09/18/making-a-synchronous-dwr-call/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 18:26:21 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[DWR]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/09/18/making-a-synchronous-dwr-call/</guid>
		<description><![CDATA[It took me about half an hour of Googling to find this, and I couldn&#8217;t readily find this in the DWR documentation. Hopefully this may save someone some time:
To make a DWR call synchronous (meaning browser-blocking) you must pass an object as one of the arguments of your DWR call with a property async set [...]]]></description>
			<content:encoded><![CDATA[<p>It took me about half an hour of Googling to find this, and I couldn&#8217;t readily find this in the <a href="http://directwebremoting.org/dwr/documentation">DWR documentation</a>. Hopefully this may save someone some time:</p>
<p>To make a DWR call synchronous (meaning browser-blocking) you must pass an object as one of the arguments of your DWR call with a property <code>async</code> set to <code>false</code>.</p>
<p>I needed this feature in project where a collection object sends out a call for more data if it didn&#8217;t have it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
_getData<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>offset<span style="color: #339933;">,</span> limit<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> r<span style="color: #339933;">;</span>
    DWRcomponent.<span style="color: #660066;">getComments</span><span style="color: #009900;">&#40;</span>offset<span style="color: #339933;">,</span> limit<span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> 
        async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
        callback<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            r <span style="color: #339933;">=</span> toJSON<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> s <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</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>		
    <span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
...</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/09/18/making-a-synchronous-dwr-call/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JavaScript argument unpacking: converting an array into a list of arguments</title>
		<link>http://readystate4.com/2008/08/17/javascript-argument-unpacking-converting-an-array-into-a-list-of-arguments/</link>
		<comments>http://readystate4.com/2008/08/17/javascript-argument-unpacking-converting-an-array-into-a-list-of-arguments/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 23:43:30 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/08/17/javascript-argument-unpacking-converting-an-array-into-a-list-of-arguments/</guid>
		<description><![CDATA[Ruby has a cool operator by the name of splat(*) that lets you unpack an array as arguments on the fly. See this code below for an example:

&#160;
# Define the item array
item1 = &#91;'Jack', '39', 'Panda'&#93;
&#160;
# Define a regular function that takes three arguments
def hi&#40;name, age, type&#41;
  puts 'Hi ' + name + '! [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby has a cool operator by the name of splat(*) that lets you unpack an array as arguments on the fly. See this code below for an example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># Define the item array</span>
item1 = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Jack'</span>, <span style="color:#996600;">'39'</span>, <span style="color:#996600;">'Panda'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Define a regular function that takes three arguments</span>
<span style="color:#9966CC; font-weight:bold;">def</span> hi<span style="color:#006600; font-weight:bold;">&#40;</span>name, age, type<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Hi '</span> <span style="color:#006600; font-weight:bold;">+</span> name <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'! You are '</span> <span style="color:#006600; font-weight:bold;">+</span> age <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">' and a '</span> <span style="color:#006600; font-weight:bold;">+</span> type <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'!'</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Call the function using item1 and the splat operator</span>
hi<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>item1<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Outputs: Hi Jack! You are 35 and a Panda!</span></pre></div></div>

<p>While JavaScript does not have such a convenient operator, experimenting around, I&#8217;ve found a good enough solution:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> item1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Jack'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'39'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Panda'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> hi<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hi '</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'! You are '</span> <span style="color: #339933;">+</span> age <span style="color: #339933;">+</span> <span style="color: #3366CC;">' and a '</span> <span style="color: #339933;">+</span> type <span style="color: #339933;">+</span> <span style="color: #3366CC;">'!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
hi.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> item1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Outputs to your Firebug console: Hi Jack! You are 39 and a Panda!</span></pre></div></div>

<p>The apply method is built into all functions in JavaScript and allows you to call a method of an object in the context of a different object (<a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Function:apply">Moz dev page here</a>). Think about it as the equivalent of temporarily moving that method off the current object (the window object if it&#8217;s a global function) and attaching it to another object before calling it. </p>
<p>The primary reason to use this method is for changing the <code>this</code> keyword reference inside of the called function, but it has the added benefit of optionally requiring an array of arguments that get passed to the called function. We basically keep the this keyword as the window object (could have specified window instead of <code>this</code>) and use it to unpack our array for us.</p>
<p>This solution is also useful in any situation where you have a function that takes an unlimited number of arguments and you want to to eventually send those arguments to another function that also accepts any number of arguments.  This situation is exactly what was puzzling me as I was working on creating a &#8220;safe&#8221; version of Firebug&#8217;s <code>console.log</code> that would work normally in Firefox but alert each argument separately in Internet Explorer.</p>
<p>Lastly, this is usefully for succinct array merging. Instead of looping through one array and pushing each item onto another you can do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">bob <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
sue <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'b'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
bob.<span style="color: #660066;">push</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>bob<span style="color: #339933;">,</span>sue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bob
<span style="color: #006600; font-style: italic;">// returns [1,2,3,4,a,b]</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/08/17/javascript-argument-unpacking-converting-an-array-into-a-list-of-arguments/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Inject jQuery into any page / A better jQuery bookmarklet</title>
		<link>http://readystate4.com/2008/07/08/inject-jquery-into-any-page-a-better-jquery-bookmarklet/</link>
		<comments>http://readystate4.com/2008/07/08/inject-jquery-into-any-page-a-better-jquery-bookmarklet/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 22:53:32 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/07/08/inject-jquery-into-any-page-a-better-jquery-bookmarklet/</guid>
		<description><![CDATA[The problem
I had to find out the language codes of all supported languages in TinyMCE (this page), but unfortunately, they were only listed in the actual download link for each file. A quick sprinkle of jQuery code in my Firebug console would have returned me a list easily but this page didn&#8217;t have it &#8211; [...]]]></description>
			<content:encoded><![CDATA[<h3>The problem</h3>
<p>I had to find out the language codes of all supported languages in TinyMCE (<a href="http://services.moxiecode.com/i18n/">this page</a>), but unfortunately, they were only listed in the actual download link for each file. A quick sprinkle of jQuery code in my Firebug console would have returned me a list easily but this page didn&#8217;t have it &#8211; which lead me to consider making a bookmarklet to add jQuery on any page. </p>
<h3>The solution:</h3>
<p>My wonderful coworker Bryan told me immediately about a simple bookmarklet that does the above called <a href="http://www.google.com/search?hl=en&#038;client=firefox-a&#038;rls=org.mozilla%3Aen-US%3Aofficial&#038;hs=Dty&#038;q=jQuerify&#038;btnG=Search">jQuerify</a>. Looking through it, I saw a few potential issues with this script and also decided I wanted something a little more personal, so I came up with my own version:</p>
<p><a href="javascript:(function(){if(!window.jQuery){var s=document.createElement('script');s.src='http://jquery.com/src/jquery-latest.js';document.getElementsByTagName('head')[0].appendChild(s);var z=setInterval(function(){if(window.jQuery){$j=jQuery.noConflict();alert('jQuery loaded!');window.clearInterval(z)}},100)}if(!window.$){window.$=function(x){return document.getElementById(x)}}})();">$jQuerify</a></p>
<p>Notable differences:</p>
<ul>
<li>Moved the jQuery shortcut <code>$</code> to <code>$j</code>. I always do this on any site I work on because I find <code>$</code> is used by so many other libraries and scripts that I want to minimize conflicts. Using <code>$j</code> also for sure tells me I&#8217;m using jQuery.</li>
<li>If <code>$</code> isn&#8217;t being used it becomes a shortcut to <code>document.getElementById</code>. I think it&#8217;s what people naturally expect <code>$</code> to do, and in my daily coding I like using <code>$('id');</code> better than <code>$j('#id')[0];</code> when all I want to do is return an element.
<li>Added an initial check for jQuery to prevent running the script unnecessary.</li>
</ul>
<p>The full source looks 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: #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><span style="color: #339933;">!</span>window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> s<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        s.<span style="color: #660066;">src</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://jquery.com/src/jquery-latest.js'</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'head'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> z<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>window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                $j <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'jQuery loaded!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                window.<span style="color: #660066;">clearInterval</span><span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window.$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	window.$<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</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: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="javascript:(function(){if(!window.jQuery){var s=document.createElement('script');s.src='http://jquery.com/src/jquery-latest.js';document.getElementsByTagName('head')[0].appendChild(s);var z=setInterval(function(){if(window.jQuery){$j=jQuery.noConflict();alert('jQuery loaded!');window.clearInterval(z)}},100)}if(!window.$){window.$=function(x){return document.getElementById(x)}}})();">$jQuerify</a> <-- Try me out, drag me to your tool bar.</p>
<h3>Happy endings</h3>
<p>Now I can just hop back over to the <a href="http://services.moxiecode.com/i18n/">TinyMCE Language Pack Download page</a>, run my bookmarklet, type the following in Firebug:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$j<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#examplecontent form:first table tbody tr'</span><span style="color: #009900;">&#41;</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>c<span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> country <span style="color: #339933;">=</span> $j<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;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'td:eq(2) label'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> code<span style="color: #339933;">=</span> $j<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;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'td:eq(1) a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(?:code=)(..)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">,</span>country<span style="color: #339933;">,</span><span style="color: #3366CC;">'='</span><span style="color: #339933;">,</span>code<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>And the list is returned:</p>
<pre>
0 Arabic = ar
1 Bokmål, Norwegian; Norwegian Bokmål = nb
2 Bosnian = bs
3 Bulgarian = bg
4 Catalan; Valencian = ca
5 Chamorro = ch
6 Chinese = zh
7 Croatian = hr
8 Czech = cs
9 Danish = da
10 Dutch; Flemish = nl
11 English = en
12 Estonian = et
13 Finnish = fi
14 French = fr
15 German = de
16 Greek, Modern (1453-) = el
17 Hebrew = he
18 Hungarian = hu
19 Interlingua (International Auxiliary Language Association) = ia
20 Italian = it
21 Japanese = ja
22 Korean = ko
23 Latvian = lv
24 Lithuanian = lt
25 Macedonian = mk
26 Malay = ms
27 Northern Sami = se
28 Norwegian Nynorsk; Nynorsk, Norwegian = nn
29 Persian = fa
30 Polish = pl
31 Portuguese = pt
32 Romanian = ro
33 Russian = ru
34 Sardinian = sc
35 Serbian = sr
36 Sichuan Yi = ii
37 Sinhala; Sinhalese = si
38 Slovak = sk
39 Slovenian = sl
40 Spanish; Castilian = es
41 Swedish = sv
42 Tatar = tt
43 Turkish = tr
44 Twi = tw
45 Ukrainian = uk
46 Vietnamese = vi
</pre>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/07/08/inject-jquery-into-any-page-a-better-jquery-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Note to jQuery developer team: Please use curly brackets!</title>
		<link>http://readystate4.com/2008/07/03/note-to-jquery-developer-team-please-use-parentheses/</link>
		<comments>http://readystate4.com/2008/07/03/note-to-jquery-developer-team-please-use-parentheses/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 17:25:37 +0000</pubDate>
		<dc:creator>Mauvis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://readystate4.com/2008/07/03/note-to-jquery-developer-team-please-use-parentheses/</guid>
		<description><![CDATA[If you haven&#8217;t gathered so far, I think the jQuery JavaScript library is the dog&#8217;s bollocks. Its small, efficient, and has improved my life as a developer almost as much as Firebug. jQuery gets flack occasionally for containing complex or cryptic code full of ternary operators and multiple variable declarations in a single statement (eg. [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t gathered so far, I think the <a href="http://jquery.com/">jQuery</a> JavaScript library is the dog&#8217;s bollocks. Its small, efficient, and has improved my life as a developer almost as much as <a href="http://getfirebug.com/">Firebug</a>. jQuery gets flack occasionally for containing complex or cryptic code full of ternary operators and multiple variable declarations in a single statement (eg. a=b=1), which I mostly see as concise and efficient coding however, there&#8217;s one thing about the jQuery library that I can&#8217;t stand. It&#8217;s something every developer should do and if you&#8217;re not doing it then you&#8217;re a jerk and that, ladies and gentlemen, is omitting your curly brackets.</p>
<p>Look at this code snippet from the jQuery library to see what I mean:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> length<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #006600; font-style: italic;">// Only deal with non-null/undefined values</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>options <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #006600; font-style: italic;">// Extend the base object</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #000066; font-weight: bold;">in</span> options <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> src <span style="color: #339933;">=</span> target<span style="color: #009900;">&#91;</span> <span style="color: #000066;">name</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> copy <span style="color: #339933;">=</span> options<span style="color: #009900;">&#91;</span> <span style="color: #000066;">name</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Prevent never-ending loop</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> target <span style="color: #339933;">===</span> copy <span style="color: #009900;">&#41;</span>
				<span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Recurse if we're merging object values</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> deep <span style="color: #339933;">&amp;&amp;</span> copy <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> copy <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;object&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>copy.<span style="color: #660066;">nodeType</span> <span style="color: #009900;">&#41;</span>
				target<span style="color: #009900;">&#91;</span> <span style="color: #000066;">name</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> deep<span style="color: #339933;">,</span>
					<span style="color: #006600; font-style: italic;">// Never move original objects, clone them</span>
					src <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span> copy.<span style="color: #660066;">length</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">,</span> copy <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Don't bring in undefined values</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> copy <span style="color: #339933;">!==</span> undefined <span style="color: #009900;">&#41;</span>
				target<span style="color: #009900;">&#91;</span> <span style="color: #000066;">name</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> copy<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Return the modified object</span>
<span style="color: #000066; font-weight: bold;">return</span> target<span style="color: #339933;">;</span></pre></div></div>

<p style="margin-top:10px;">
Coding in this way leads is extremely error-prone especially for open-souce projects where many others are modifying your code. Please stop it.</p>
<p>Love,</p>
<p>A dedicated user</p>
]]></content:encoded>
			<wfw:commentRss>http://readystate4.com/2008/07/03/note-to-jquery-developer-team-please-use-parentheses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
