<?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>James Padolsey&#187; JavaScript category &#8211; James Padolsey</title>
	<atom:link href="http://james.padolsey.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 26 Feb 2010 16:17:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery: Novice to Ninja</title>
		<link>http://james.padolsey.com/javascript/jquery-novice-to-ninja/</link>
		<comments>http://james.padolsey.com/javascript/jquery-novice-to-ninja/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 16:17:19 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1442</guid>
		<description><![CDATA[<a href="http://www.sitepoint.com/books/jquery1/">Sitepoint&#8217;s latest title</a> is aimed at the average Front-end developer/designer and promises to teach her the intricate workings of the jQuery library. In fact, if we&#8217;re to go by the title, it promises to take her from &#8220;novice to ninja&#8221;. Let me be honest, I was&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sitepoint.com/books/jquery1/">Sitepoint&#8217;s latest title</a> is aimed at the average Front-end developer/designer and promises to teach her the intricate workings of the jQuery library. In fact, if we&#8217;re to go by the title, it promises to take her from &#8220;novice to ninja&#8221;. Let me be honest, I was originally skeptical of the book, but upon finding out that it was co-authored by the esteemed <a href="http://www.mrspeaker.net/">mrspeaker</a> my mind was quickly changed.</p>

<a href="http://www.sitepoint.com/books/jquery1/"><img class="main-img" src="http://james.padolsey.com/wp-content/uploads/jquerysp.jpg" alt="jQuery: Novice to Ninja" /> </a>

<p>As with any good book, the first chapter outlines what the subject matter is. The authors talk about jQuery itself and touch briefly on the DOM. You have to remember that this book is aimed at developers or designers that don&#8217;t really know that much about JavaScript, the DOM, or jQuery, so the book is accordingly written in such a way that it won&#8217;t confuse the pants off your typical photshop-wizz turned HTML/CSS guy.</p>

<p>So if you know JavaScript well, you might not enjoy this book &#8212; it will probably annoy you, simply because it doesn&#8217;t go into a huge amount of detail on what&#8217;s happening behind the scenes. But, for those that it&#8217;s aimed at, I think it does a pretty good job.</p>

<p>One thing that we have a problem with, in the jQuery community bubble, is the overzealous generation of redundant nomenclature. This book doesn&#8217;t fall short on this front. jQuery&#8217;s methods are referred to as &#8220;actions&#8221;.</p>

<p>The second chapter covers the most basic usages of jQuery, <em>selecting</em> stuff and <em>doing</em> stuff. It&#8217;s all innocent and simple &#8212; I like it. To keep the reader focused the authors have slipped in &#8220;A few tricks&#8221;. Among them is the popular add-a-class-on-mouseover, remove-it-on-mouseout. </p>

<p>Nearing the end of chapter two, the book has the following to say about the <code>hover</code> method:</p>

<span id="more-1442"></span>

<blockquote><p>&#8220;It requires two functions as parameters: one to handle the <code>mouseover</code> event, and one to handle the <code>mouseout</code> event.&#8221;</p></blockquote>

<p>The book <em>does</em> claim to be 1.4-ready, so I think it&#8217;s only right to mention this. In jQuery 1.4 the <a href="http://api.jquery.com/hover/">hover method</a> accepts either one or two parameters, and they don&#8217;t correspond to the <code>mouse(over|out)</code> events; they correspond to the <code>mouse(enter|leave)</code> events, although I guess they&#8217;re not totally incorrect on this because jQuery simulates these events using <code>mouse(over|out)</code> in non-supporting browsers, but it is nevertheless an oversight.</p>

<p>Not soon after that did I find what I consider to be a grievous anti-pattern:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
$<span class="br0">&#40;</span><span class="st0">'&lt;span class=&quot;revealer&quot;&gt;Tell me!&lt;/span&gt;'</span><span class="br0">&#41;</span>
    .<span class="me1">insertBefore</span><span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span><span class="sy0">;</span>
$<span class="br0">&#40;</span><span class="st0">'.revealer'</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>It seems that in their desperation to keep it simple, the authors have chosen to spare the readers of what would be some very good advice. When you add something to the DOM, and it doesn&#8217;t need to be referenced within a CSS StyleSheet, then giving it a class or ID, and then getting a reference to that element via that hook, is normally an almost-entirely redundant process. This would be a far better approach:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> spoilers <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>
    buttons <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'&lt;span&gt;Tell me!&lt;/span&gt;'</span><span class="br0">&#41;</span>.<span class="me1">insertBefore</span><span class="br0">&#40;</span>spoilers<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
buttons.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Or, even:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">before</span><span class="br0">&#40;</span>
    $<span class="br0">&#40;</span><span class="st0">'&lt;span/&gt;'</span><span class="sy0">,</span> <span class="br0">&#123;</span>
        text<span class="sy0">:</span> <span class="st0">'Tell me!'</span><span class="sy0">,</span>
        click<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
            $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span>
<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>If I was the author, I don&#8217;t know what I would have done in this situation&#8230; since it&#8217;s a teaching exercise, do you show the readers how it <em>should</em> be done, or do you show them in the simplest way possible, so as to not bulldoze them with complexities?</p>

<p>Moving on, the book is very <em>cookbook&#8217;y</em> and focuses heavily on an imagined client that has lots of strange requirements (quite typical of your average client). From the introduction to chapter three:</p>

<blockquote><p>&#8220;The client is extremely happy [...] he believes flashy animations will help boost sales.</p>
<p>&#8216;I think it needs some of that Web 2.0 that I&#8217;ve been hearing about,&#8217; he says confidently. &#8216;Can you make it look more like a Web 2.0?&#8217;</p>
<p>&#8216;Errrm, indeed we can,&#8217; you assure him, as he passes you his next wish list chock-full of exciting changes—a list that will allow us to move beyond simply hiding and showing, and closer to our goal of being a jQuery ninja.&#8221;</p></blockquote>

<p>I have a soft spot for these kind of tongue-in-cheek introductions, they&#8217;re quite popular in other programming books too, and I think it does well to mentally elevate the reader &#8212; and to develop that all-important reader-author relationship.</p>

<p>Before you know it, you&#8217;re into the third chapter. Chapter three exhaustively covers animation. The detail is great and the authors even take the time touch on jQuery UI&#8217;s animation capabilities in addition to covering all of the core methods. I liked this chapter, &#8212; it seemed to cover pretty much everything, although I found myself disappointed with some of the code again:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'&lt;div id=&quot;navigation_blob&quot;&gt;&lt;/div&gt;'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    width<span class="sy0">:</span> $<span class="br0">&#40;</span><span class="st0">'#navigation li:first a'</span><span class="br0">&#41;</span>.<span class="me1">width</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span><span class="sy0">,</span>
    height<span class="sy0">:</span> $<span class="br0">&#40;</span><span class="st0">'#navigation li:first a'</span><span class="br0">&#41;</span>.<span class="me1">height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">appendTo</span><span class="br0">&#40;</span><span class="st0">'#navigation'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Let&#8217;s introduce some basic optimisations:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> nav <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'#navigation'</span><span class="br0">&#41;</span><span class="sy0">,</span>
    firstAnchor <span class="sy0">=</span> nav.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">'li:first a'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
$<span class="br0">&#40;</span><span class="st0">'&lt;div id=&quot;navigation_blob&quot;&gt;&lt;/div&gt;'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    width<span class="sy0">:</span> firstAnchor.<span class="me1">width</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span><span class="sy0">,</span>
    height<span class="sy0">:</span> firstAnchor.<span class="me1">height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">appendTo</span><span class="br0">&#40;</span>nav<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Chapter four is all about images, and takes the reader through the process of creating a simple lightbox effect. It also shows you how to download and &#8220;install&#8221; your typical jQuery lightbox plugin (in this case, <a href="http://colorpowered.com/colorbox/">ColorBox</a>). It continues to show you how to create a simple slideshow.</p>

<p>The bulk of this book was obviously written before 1.4 was released. I get the impression that, upon 1.4&#8217;s release, the authors quickly went through adding in some &#8220;actions&#8221; only made possible with 1.4. They covered the new way of creating elements in jQuery 1.4, which involves passing an object as the second argument to <code>jQuery()</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'&lt;div/&gt;'</span><span class="sy0">,</span> <span class="br0">&#123;</span>
    text<span class="sy0">:</span> <span class="st0">'foo'</span><span class="sy0">,</span>
    click<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">,</span>
    id<span class="sy0">:</span> <span class="st0">'muah'</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>The book had the following to say:</p>

<blockquote><p>&#8220;If you use a jQuery method name like text, html, or val, it will use the jQuery methods to set the property. Everything else will be treated as an attribute, as done with the src property.&#8221;</p></blockquote>

<p>This isn&#8217;t entirely accurate. jQuery only uses certain methods &#8212; not all of them. Methods available for usage in this way are specified under <code>jQuery.attrFn</code>.</p>

<p>Continuing&#8230;</p>

<p>Chapter five brings with it a lot of UI icing&#8230; tooltips, menus, tabs and accordions. The book takes the reader through the processes involved in creating these interface elements. Just like all the other chapters, chapter five is littered with little titbits of useful information. For example, it covers event propagation and default event actions in pretty decent detail.</p>

<p>The next chapter is probably my favourite &#8212; it covers code construction and best practices, including namespacing, encapsulation, commenting, DRY, templating and feature detection! I was pleasantly surprised to see these topics covered in a book like this. Their templating techniques were a little lacking &#8212; I would&#8217;ve liked to see some custom-typed <code>&lt;script&gt;</code> elements (like in <a href="http://ejohn.org/blog/javascript-micro-templating/">Resig&#8217;s solution</a>), or perhaps some templates stuffed into HTML comments.</p>

<p>Chapter six continues by explaining Ajax and showing the reader what jQuery offers in the way of Ajax/XHR utilities. You&#8217;re also taken through the process of creating an Ajax-driven image gallery among some other gems.</p>

<p>I&#8217;m going to stop outlining the chapters now&#8230; I&#8217;m bored, and you&#8217;re probably not getting much from that (heck, you could probably just look through the <a href="http://www.sitepoint.com/books/jquery1/toc.php">TOC</a>). The rest of book covers forms, controls, dialogs, validation, lists, trees, tables, plugins, themes and some &#8220;advanced topics&#8221;. There&#8217;s also a hefty appendices that has bunch of useful information, including a brief JavaScript tutorial for those new to the language.</p>

<p>Just so we&#8217;re clear, this is a good book, and if you fit the bill of a front-end wizz that wants to pick up jQuery, then this book will easily deliver. There are a couple of oversights, but in a book over 400 pages long I&#8217;d say that&#8217;s pretty amazing! <a href="http://www.sitepoint.com/books/jquery1/authors.php">Earl Castledine and Craig Sharkie</a> have done a pretty awesome job!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/jquery-novice-to-ninja/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Double bitwise NOT (~~)</title>
		<link>http://james.padolsey.com/javascript/double-bitwise-not/</link>
		<comments>http://james.padolsey.com/javascript/double-bitwise-not/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 18:45:52 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cool Stuff]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1433</guid>
		<description><![CDATA[Who would have thought that JavaScript&#8217;s rare and mysterious bitwise operators could be so useful? I first discovered this trick a while ago from a <a href="http://www.slideshare.net/madrobby/extreme-javascript-performance">slideshow on JavaScript performance</a>, by Thomas Fuchs. The &#8220;trick&#8221; is as follows:


<pre class="javascript" style="font-family:monospace;">~~something<span class="sy0">;</span></pre>




The bitwise NOT operator (<code>~</code>) will take its&#8230;]]></description>
			<content:encoded><![CDATA[<p>Who would have thought that JavaScript&#8217;s rare and mysterious bitwise operators could be so useful? I first discovered this trick a while ago from a <a href="http://www.slideshare.net/madrobby/extreme-javascript-performance">slideshow on JavaScript performance</a>, by Thomas Fuchs. The &#8220;trick&#8221; is as follows:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">~~something<span class="sy0">;</span></pre></div></div>




<p>The bitwise NOT operator (<code>~</code>) will take its operand, convert it to a 32-bit integer, and will invert each bit so that each <code>0</code> becomes a <code>1</code> and vice versa.


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">00000000000000000000000000001001
...becomes
11111111111111111111111111110110</pre></div></div>




<p>The effect of this, given the expression <code>~foo</code> is <code>-(foo + 1)</code>. A double bitwise NOT operation (<code>~~foo</code>) will therefore result in <code>-(-(foo + 1) + 1)</code>. This only remains true for integers though; given all the potential operands, the real equivalent expression to <code>~~</code> is probably something like:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw1">typeof</span> foo <span class="sy0">===</span> <span class="st0">'number'</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span>isNaN<span class="br0">&#40;</span>foo<span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> foo <span class="sy0">!==</span> Infinity
    <span class="sy0">?</span> foo <span class="sy0">&gt;</span> <span class="nu0">0</span> <span class="sy0">?</span> Math.<span class="me1">floor</span><span class="br0">&#40;</span>foo<span class="br0">&#41;</span> <span class="sy0">:</span> Math.<span class="me1">ceil</span><span class="br0">&#40;</span>foo<span class="br0">&#41;</span> <span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">;</span>
<span class="co1">// This is ONLY _effectively_ the same... this is</span>
<span class="co1">// NOT what happens internally!</span></pre></div></div>




<p>This is obviously a fair bit slower than <code>~~</code>.</p>

<p>If the operand is a number and it&#8217;s not <code>NaN</code> or <code>Infinity</code> then <code>~~</code> will have the effect of rounding it towards zero (<code>Math.ceil</code> for negative, <code>Math.floor</code> for positive). If it&#8217;s not a number, then I believe the internal <code>ToInt32</code> function converts it to zero.</p>

<p>Here are some examples of the double-bitwise NOT operation in all its glory:<p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">~~<span class="kw2">null</span><span class="sy0">;</span>      <span class="co1">// =&gt; 0</span>
~~undefined<span class="sy0">;</span> <span class="co1">// =&gt; 0</span>
~~<span class="nu0">0</span><span class="sy0">;</span>         <span class="co1">// =&gt; 0</span>
~~<span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>        <span class="co1">// =&gt; 0</span>
~~<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">;</span>        <span class="co1">// =&gt; 0</span>
~~<span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">/</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>     <span class="co1">// =&gt; 0</span>
~~<span class="kw2">false</span><span class="sy0">;</span>     <span class="co1">// =&gt; 0</span>
~~<span class="kw2">true</span><span class="sy0">;</span>      <span class="co1">// =&gt; 1</span>
~~<span class="nu0">1.2543</span><span class="sy0">;</span>    <span class="co1">// =&gt; 1</span>
~~<span class="nu0">4.9</span><span class="sy0">;</span>       <span class="co1">// =&gt; 4</span>
~~<span class="br0">&#40;</span><span class="sy0">-</span><span class="nu0">2.999</span><span class="br0">&#41;</span><span class="sy0">;</span>  <span class="co1">// =&gt; 2</span></pre></div></div>




<p><code>~~</code>&#8217;s flooring capabilities make it a better alternative to <code>Math.floor</code> if you know you&#8217;re dealing with positives &#8212; it&#8217;s faster and takes up less characters. It&#8217;s not quite as readable though, but I&#8217;m hoping that <code>~~</code> will slowly become a very well-known technique in the JS arena, so we can all use it without fearing accusations.</p><span id="more-1433"></span>

<p>It&#8217;s quite useful for normalizing arguments that you expect to be integers too. Take this example from what the <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf">MDC recommends</a> for providing <code>Array.prototype.indexOf</code> to non-supporting browsers:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="coMULTI">/*Array.prototype.indexOf = function...*/</span>
<span class="kw2">var</span> from <span class="sy0">=</span> Number<span class="br0">&#40;</span>arguments<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="sy0">||</span> <span class="nu0">0</span><span class="sy0">;</span>  
from <span class="sy0">=</span> <span class="br0">&#40;</span>from <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">&#41;</span>  
     <span class="sy0">?</span> Math.<span class="me1">ceil</span><span class="br0">&#40;</span>from<span class="br0">&#41;</span>  
     <span class="sy0">:</span> Math.<span class="me1">floor</span><span class="br0">&#40;</span>from<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>In goes <code>~~</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="coMULTI">/*Array.prototype.indexOf = function...*/</span>
<span class="kw2">var</span> from <span class="sy0">=</span> ~~arguments<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">;</span></pre></div></div>




<p>Go forth and double-NOT!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/double-bitwise-not/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Under jQuery&#8217;s bonnet</title>
		<link>http://james.padolsey.com/javascript/under-jquerys-bonnet/</link>
		<comments>http://james.padolsey.com/javascript/under-jquerys-bonnet/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 22:44:35 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cool Stuff]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1414</guid>
		<description><![CDATA[If there&#8217;s one thing all library users should be doing more, it&#8217;s peeling back the layer of abstraction and seeing what&#8217;s really happening underneath. This is the only way to gain a true understanding of what the library provides, and who knows, maybe you&#8217;ll find&#8230;]]></description>
			<content:encoded><![CDATA[<p>If there&#8217;s one thing all library users should be doing more, it&#8217;s peeling back the layer of abstraction and seeing what&#8217;s really happening underneath. This is the only way to gain a true understanding of what the library provides, and who knows, maybe you&#8217;ll find some gems that you didn&#8217;t know existed.</p>

<p>Libraries like jQuery aren&#8217;t very small when uncompressed. Traversing a long source file trying to look for a specific method&#8217;s implementation is far from ideal. I have frequently found myself in this situation with jQuery, so today I decided to do something about it, and the result is viewable at <strong><a href="http://james.padolsey.com/jquery">http://james.padolsey.com/jquery</a></strong>.</p>

<p class="video"><a href="http://james.padolsey.com/jquery/jQuery"><img alt="Preview of the jQuery Source Viewer" src="http://james.padolsey.com/wp-content/uploads/jquery-src-large.png" /></a></p>

<p>It allows you to study specific parts of jQuery&#8217;s source. You can type in a method name and you&#8217;ll see its implementation straight away, in all its syntax-highlighted glory! It will also link&#8217;ify all function names within the presented source:</p>

<p class="video"><a href="http://james.padolsey.com/jquery/css"><img alt="The css method's source, with certain function names as links" src="http://james.padolsey.com/wp-content/uploads/jquery-src-link.png" /></a></p>

<p>You can link directly to a method using the following URL pattern:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://james.padolsey.com/jquery/[version/]methodName
&nbsp;
E.g.
http://james.padolsey.com/jquery/css
http://james.padolsey.com/jquery/1.3.2/attr
http://james.padolsey.com/jquery/jQuery.proxy
&nbsp;
-OR-
http://james.padolsey.com/jquery/#v=version&amp;fn=methodName</pre></div></div>




<p>If you don&#8217;t specify a version then 1.4 is assumed.</p>

<span id="more-1414"></span>

<p>The source that&#8217;s shown won&#8217;t be identical to the actual source, since this source viewer only works by <code>toString()</code>&#8216;ing the functions. I might eventually implement something a little more robust, but it&#8217;s going to take time &#8212; finding a specific function&#8217;s declaration in a source file is tricky, especially when they&#8217;re not all defined with the straightforward <code>method: function(){...}</code> or <code>function foo(){...}</code>.</p>

<p>I didn&#8217;t really intend it to be an accurate depiction of the real source anyway &#8212; just something I (and others) could use to see how certain things are implemented within jQuery.</p>

<p>I hope you find it useful&#8230; I know I will!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/under-jquerys-bonnet/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>JavaScript&#8217;s Dark Alley</title>
		<link>http://james.padolsey.com/javascript/javascripts-dark-alley/</link>
		<comments>http://james.padolsey.com/javascript/javascripts-dark-alley/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 15:14:26 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1403</guid>
		<description><![CDATA[So, I happened upon an <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8c75c6e8aff78806">interesting thread</a> this morning, concerning the recently released <a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint</a>. For those of you who have not dared to look, the <a href="http://groups.google.com/group/comp.lang.javascript/">comp.lang.javascript</a> newsgroup is truly the proverbial dark alley of JavaScript and DOM development. I can picture it vividly, a dingy dusty&#8230;]]></description>
			<content:encoded><![CDATA[<p>So, I happened upon an <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8c75c6e8aff78806">interesting thread</a> this morning, concerning the recently released <a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint</a>. For those of you who have not dared to look, the <a href="http://groups.google.com/group/comp.lang.javascript/">comp.lang.javascript</a> newsgroup is truly the proverbial dark alley of JavaScript and DOM development. I can picture it vividly, a dingy dusty cobbled walkway blackened by the thick fog of regressive opinion and unprovoked flame wars, riddled with the unquestioned prerequisite of a newsgroup&#8217;s slow death &#8212; spam on every corner!</p>

<p>If you have a read through that thread you&#8217;ll notice none other than the infamous David Mark. This is the same person that <a href="http://groups.google.com/group/comp.lang.javascript/msg/37cb11852d7ca75c?">slammed jQuery</a> (oh, and MooTools) for not being able to &#8220;do *anything* 
right&#8221; and then, not too long ago, <a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/baef5e91bd714033#msg_b8079000b547df15">threatened the jQuery project</a> with legal action over some DOM attributes tests. </p>

<p>David <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8c75c6e8aff78806">had the following to say</a> about jQuery Lint:</p>

<blockquote><p>
Well, sort of.  It&#8217;s not so much the Johnny-come-lastly advisor plug- 
in, but the fact that it is just another horrible plug-in for an 
equally bad pile of JS [he means jQuery].  Anyone who would use jQuery enough to want to 
write a plug-in is not going to be the best candidate to wtite a 
browser scripting &#8220;lint.&#8221;  I can&#8217;t see it. 
</p></blockquote>

<p>He continues:</p>

<blockquote><p>
The idea (which is sound) is to warn developers when they are doing 
something incorrect (or ill-advised).  I haven&#8217;t looked at the code 
for it (and I&#8217;m sure I never will).  I find I don&#8217;t have to do that 
anymore as the basic rules always apply.  <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  
</p></blockquote>

<p>David doesn&#8217;t stop! He still continues:</p>

<blockquote><p>
<strong>[Scott Sauyet]</strong><br/>
&gt; It seems to be a tool to help new users learn things like that although this code is legal:


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">&quot;selector&quot;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">&quot;color&quot;</span><span class="sy0">,</span> <span class="st0">&quot;red&quot;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">&quot;margin&quot;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>



&gt; This would be more efficient:


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">&quot;selector&quot;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>color<span class="sy0">:</span> <span class="st0">&quot;red&quot;</span><span class="sy0">,</span> margin<span class="sy0">:</span> <span class="nu0">0</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>



<strong>[David Mark]</strong><br/>
You dump it and replace it with something better (and faster).  In this 
case, something like:


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span class="me1">style</span>.<span class="me1">color</span> <span class="sy0">=</span> <span class="st0">&quot;red&quot;</span><span class="sy0">;</span> 
el.<span class="me1">style</span>.<span class="me1">margin</span> <span class="sy0">=</span> <span class="st0">'0'</span><span class="sy0">;</span></pre></div></div>



That&#8217;s smaller, faster, more readable, makes no function calls, 
creates no new objects and is impervious to upgrades to jQuery 
(typically poison).  And, as we all know, jQuery doesn&#8217;t work worth a 
shit anyway as the &#8220;logic&#8221; in the script is mostly a diary of confused 
browser watchers.  Get the picture? <br/>
&gt; That doesn&#8217;t indicate anything wrong with the library any more than 
&gt; any lint program indicates problems with its target environment. <br/>
Of course, you don&#8217;t need a lint to see what is wrong with the 
library.  It&#8217;s as obviously unsuitable as a dissertation written in 
crayon. 
</p></blockquote>

<p>According to David we should be sparing ourselves the obvious folly of abstraction and spend more time writing &#8220;smaller, faster, more readable&#8221; JavaScript:</p>
<span id="more-1403"></span>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// We say:</span>
$<span class="br0">&#40;</span><span class="st0">'#elem'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    fontWeight<span class="sy0">:</span> <span class="nu0">700</span><span class="sy0">,</span>
    width<span class="sy0">:</span> <span class="nu0">200</span><span class="sy0">,</span>
    height<span class="sy0">:</span> <span class="nu0">100</span><span class="sy0">,</span>
    color<span class="sy0">:</span> <span class="st0">'#000'</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// He says:</span>
<span class="kw2">var</span> elem <span class="sy0">=</span> document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'elem'</span><span class="br0">&#41;</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">fontWeight</span> <span class="sy0">=</span> <span class="st0">'700'</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">width</span> <span class="sy0">=</span> <span class="st0">'200px'</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">height</span> <span class="sy0">=</span> <span class="st0">'100px'</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">color</span> <span class="sy0">=</span> <span class="st0">'#000'</span><span class="sy0">;</span></pre></div></div>




<p>David, if you&#8217;re reading this, maybe you could offer us a response, detailing why you think jQuery &#8220;doesn&#8217;t work worth a 
shit&#8221; and also perhaps you could enlighten us why verbose and old-fashioned DOM scripting is miles better than progressive abstractions like the one jQuery offers? And if not too much trouble, I would appreciate if you could <strong>spare us your slander and ad hominem</strong>.</p>

<p>And when I say &#8220;detailing why you think jQuery doesn&#8217;t work worth a shit&#8221; I don&#8217;t mean having a go at <a href="http://groups.google.com/group/comp.lang.javascript/msg/37cb11852d7ca75c?">petty code inconsistencies and syntax peculiarities</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/javascripts-dark-alley/feed/</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
		<item>
		<title>jQuery Lint</title>
		<link>http://james.padolsey.com/javascript/jquery-lint/</link>
		<comments>http://james.padolsey.com/javascript/jquery-lint/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 14:38:21 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1380</guid>
		<description><![CDATA[<a href="http://github.com/jamespadolsey/jQuery-Lint"><em>jQuery Lint</em></a> is a simple script you can download and use with jQuery. It works over the top of jQuery and diligently reports errors and any incorrect usage of jQuery. It will also, to some extent, offer guidance on best practices and performance concerns.

Unlike <a href="http://www.jslint.com/">JSLint</a>,&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/jamespadolsey/jQuery-Lint"><em>jQuery Lint</em></a> is a simple script you can download and use with jQuery. It works over the top of jQuery and diligently reports errors and any incorrect usage of jQuery. It will also, to some extent, offer guidance on best practices and performance concerns.</p>

<p>Unlike <a href="http://www.jslint.com/">JSLint</a>, jQuery Lint is a <em>runtime</em> reporter. To use it, you need to include it, after jQuery, in your document:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="sy0">&lt;</span>script src<span class="sy0">=</span><span class="st0">&quot;jquery.js&quot;</span><span class="sy0">&gt;&lt;/</span>script<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>script src<span class="sy0">=</span><span class="st0">&quot;jquery.lint.js&quot;</span><span class="sy0">&gt;&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div>




<p>jQuery lint&#8217;s main objective is to notify you of incorrect usages of jQuery&#8217;s API. So, if you pass incorrect arguments to any method then jQuery Lint will let you know. It compares your arguments to the argument signatures in jQuery&#8217;s API. It reports via Firebug, although you can quite easily plug-in your own console mechanism.</p>

<p>It has four different error-reporting levels (accessible via <code>jQuery.LINT.level</code>), zero reports nothing, three will report everything, including small things like using <code>css().css().css()</code> instead of <code>css({...})</code>. It&#8217;s quite configurable too. You can add your own checks. E.g.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span class="me1">LINT</span>.<span class="me1">special</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">jQuery</span> <span class="sy0">=</span> jQuery.<span class="me1">LINT</span>.<span class="me1">special</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">jQuery</span> <span class="sy0">||</span> <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Add check on error-reporting level one.</span>
<span class="co1">// Check jQuery method.</span>
jQuery.<span class="me1">LINT</span>.<span class="me1">special</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">jQuery</span>.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>selector<span class="sy0">,</span> context<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    <span class="kw1">if</span> <span class="br0">&#40;</span>selector <span class="sy0">===</span> <span class="st0">'*'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">&quot;Don't use the universal selector!&quot;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>jQuery Lint tries to help you in determining where the problem occurred in your code. It&#8217;s not much help to you if it just says, &#8220;Err, you called <code>css()</code> incorrectly!&#8221;. If it occurred as a result of an event then Lint will say so, and if you&#8217;re using a browser that provides a stack-trace as part of its Error object (like Firefox) then Lint will also provide you with the file-name and line number. E.g.</p>

<p><img src="http://img11.imageshack.us/img11/9862/20100118140644.png" alt="jquery Lint - Reporting line number and file name where problem occured." /></p>

<p>You can read more about jQuery Lint and download it at Github:</p>

<p class="video">
    <strong><a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint @ Github</a></strong>
</p>

<p>The idea of a lint-like script for jQuery has been floating around for some time. I want to thank Dave Methvin in particular, for it was <a href="http://markmail.org/message/wzkosk2s5jklpkv4">his idea</a> that sparked my interest originally.</p>

<p>This is quite a young project, so there will be bugs. Please <a href="http://github.com/jamespadolsey/jQuery-Lint/issues">report them</a>!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/jquery-lint/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Cross-domain requests with jQuery</title>
		<link>http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/</link>
		<comments>http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 13:44:41 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[YQL]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1365</guid>
		<description><![CDATA[Chris Heilmann <a href="http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/">recently posted</a> on how to use YQL to make cross-domain requests, which would usually be prohibited due to the same-domain-policy. I already knew about YQL, but I had no idea that it]]></description>
			<content:encoded><![CDATA[<p>Chris Heilmann <a href="http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/">recently posted</a> on how to use YQL to make cross-domain requests, which would usually be prohibited due to the same-domain-policy. I already knew about YQL, but I had no idea that it allowed retrieval of HTML from other sites, via JSON, returned <strong>as a single string</strong>! </p>

<p>Instead of asking for <code>JSON</code> format, ask for <code>XML</code>, but also add a <code>callback</code> parameter to your query. <a href="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%20%3D%20%22http://google.com%22%20and%20xpath%3D%22*%22&#038;format=xml&#038;callback=cbfunc">Voila</a>!</p>

<p>So, in short, YQL allows us to make cross-domain GET requests!</p>

<p>Chris also posted <a href="http://icant.co.uk/articles/crossdomain-ajax-with-jquery/error-handling.html">a demo</a>!</p>

<p>With a bit of hacking, we can make jQuery work with YQL for all cross-domain GET requests. <strong>UPDATE</strong>: I&#8217;ve decided to put this in my &#8220;<a href="http://github.com/jamespadolsey/jQuery-Plugins/">jQuery Plugins</a>&#8221; repo at Github:</p>

<p class="video">
    <strong><a href="http://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/">Cross-Domain Ajax mod @ Github</a></strong>
</p>

<p>With this mod, any GET request made via <code>jQuery.ajax</code> to another domain will work!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'#container'</span><span class="br0">&#41;</span>.<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">'http://google.com'</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// SERIOUSLY!</span>
&nbsp;
$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    url<span class="sy0">:</span> <span class="st0">'http://news.bbc.co.uk'</span><span class="sy0">,</span>
    type<span class="sy0">:</span> <span class="st0">'GET'</span><span class="sy0">,</span>
    success<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>res<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw2">var</span> headline <span class="sy0">=</span> $<span class="br0">&#40;</span>res.<span class="me1">responseText</span><span class="br0">&#41;</span>.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">'a.tsh'</span><span class="br0">&#41;</span>.<span class="me1">text</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">alert</span><span class="br0">&#40;</span>headline<span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Works with $.get too!</span></pre></div></div>




<p>Have fun!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>jQuery code smells</title>
		<link>http://james.padolsey.com/javascript/jquery-code-smells/</link>
		<comments>http://james.padolsey.com/javascript/jquery-code-smells/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 17:15:05 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1344</guid>
		<description><![CDATA[Here&#8217;s a quick line-up of some <em>smelly</em> jQuery code! &#8220;Code smells&#8221; are pieces of code that do for your eyes what bad smells do for your nostrils, and usually result in erroneous or harder-to-maintain code. I have no doubt that at least half of you will&#8230;]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick line-up of some <em>smelly</em> jQuery code! &#8220;Code smells&#8221; are pieces of code that do for your eyes what bad smells do for your nostrils, and usually result in erroneous or harder-to-maintain code. I have no doubt that at least half of you will think that I&#8217;m wrong about at least half of these. </p>

<p>Before you stop paying attention, I&#8217;d like to wish everyone a Happy New Year! I hope you find the new design more consistent and cleaner.</p>

<p>May the flaming commence&#8230;</p>

<h3>Prefixxing all jQuery objects with &#8220;$&#8221;</h3>

<p>It&#8217;s ugly, and my text editor doesn&#8217;t like it! But mainly, it&#8217;s ugly, and is only useful for beginners. Hungarian notation is a hotly debated technique &#8212; like Marmite (and Jade Goody?), you either love it or hate it!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> $body <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'body'</span><span class="br0">&#41;</span><span class="sy0">;</span>
$body.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    $this <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>I won&#8217;t say much more; I appreciate that it&#8217;s a sensitive topic for some and is mostly about personal preference.</p>

<h3>Concatenating a bunch of things together to make a selector</h3>

<p>I see a lot of this! It looks dirty, and just screams, &#8220;I&#8217;m succumbing to the API because I don&#8217;t know what else to do!&#8221;</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'.'</span> <span class="sy0">+</span> className <span class="sy0">+</span> <span class="st0">'['</span> <span class="sy0">+</span> attrLookup <span class="sy0">+</span> <span class="st0">'^='</span> <span class="sy0">+</span> attrPrefix <span class="st0">']:not(.'</span> <span class="sy0">+</span> notClass <span class="sy0">+</span> <span class="st0">')'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>This is why I think jQuery needs some other way of filtering DOM elements (possibly something like <a href="http://github.com/jamespadolsey/jQuery-plus/blob/master/jquery.plus.js#L112">my <code>filter</code> hack</a>). For now, seperating out our selectors will have to suffice:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'.'</span> <span class="sy0">+</span> blah.<span class="me1">className</span><span class="br0">&#41;</span>
    .<span class="me1">filter</span><span class="br0">&#40;</span><span class="st0">'['</span> <span class="sy0">+</span> attrLookup <span class="sy0">+</span> <span class="st0">'^='</span> <span class="sy0">+</span> attrPrefix <span class="st0">']'</span><span class="br0">&#41;</span>
    .<span class="me1">not</span><span class="br0">&#40;</span><span class="st0">'.'</span> <span class="sy0">+</span> notClassName<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>To be truthful, I&#8217;m on the fence with this&#8230; Maybe I&#8217;m just against string concatenation!</p>

<span id="more-1344"></span>

<h3>Going over the top with chaining</h3>

<p>Some developers have obviously been dared to never exit a chain, ever!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'a'</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">'reg-link'</span><span class="br0">&#41;</span>
    .<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">'span'</span><span class="br0">&#41;</span>
        .<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">'inner'</span><span class="br0">&#41;</span>
    .<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    .<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span>
        .<span class="me1">mouseenter</span><span class="br0">&#40;</span>mouseEnterHandler<span class="br0">&#41;</span>
        .<span class="me1">mouseleave</span><span class="br0">&#40;</span>mouseLeaveHandler<span class="br0">&#41;</span>
    .<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    .<span class="me1">explode</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Long chains are okay, as long as you&#8217;re not doing a massive amount of DOM traversing in them&#8230; they can become unwieldy beasts!</p>

<h3>Not caching collections</h3>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'a'</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    $<span class="br0">&#40;</span><span class="st0">'a'</span><span class="br0">&#41;</span>.<span class="me1">not</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">'wow'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Cache please!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> anchors <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'a'</span><span class="br0">&#41;</span><span class="sy0">;</span>
anchors.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    anchors.<span class="me1">not</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">'wow'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>That is, unless you have a good reason not to, e.g. new anchors are constantly being added to the document.</p>

<h3>HTML</h3>

<p>Long strings of HTML scarring your otherwise beautiful code. Keep the HTML&#8230; in the HTML, no in the JavaScript!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'#something'</span><span class="br0">&#41;</span>
    .<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">'&lt;div class=&quot;gall&quot;&gt;&lt;a href=&quot;javascript:void(0)&quot;&gt;Linky&lt;/a&gt;&lt;/div&gt;'</span><span class="br0">&#41;</span>
    .<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">'&lt;div class=&quot;gall&quot;&gt;&lt;a href=&quot;javascript:void(0)&quot;&gt;Linky&lt;/a&gt;&lt;/div&gt;'</span><span class="br0">&#41;</span>
    .<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">'&lt;button onclick=&quot;app.doStuff()&quot;&gt;Button&lt;/button&gt;'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>I added a couple of other code smells (really bad ones!) in there. Please, oh please, learn how to use jQuery for your event handling!</p>

<p>Here&#8217;s a nicer smelling (and more readable) alternative:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> div <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'&lt;div/&gt;'</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">'gall'</span><span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span>
    $<span class="br0">&#40;</span><span class="st0">'&lt;a/&gt;'</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span>
<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
$<span class="br0">&#40;</span><span class="st0">'#something'</span><span class="br0">&#41;</span>
    .<span class="me1">append</span><span class="br0">&#40;</span>div<span class="br0">&#41;</span>
    .<span class="me1">append</span><span class="br0">&#40;</span>div.<span class="me1">clone</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    .<span class="me1">append</span><span class="br0">&#40;</span>
        $<span class="br0">&#40;</span><span class="st0">'&lt;button&gt;Button&lt;/button&gt;'</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
            app.<span class="me1">doStuff</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Leave a comment: <strong>Submit your own code smell! Or&#8230; tell me why I&#8217;m wrong!</strong></p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/jquery-code-smells/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Macros in jQuery</title>
		<link>http://james.padolsey.com/javascript/macros-in-jquery/</link>
		<comments>http://james.padolsey.com/javascript/macros-in-jquery/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 20:28:42 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1327</guid>
		<description><![CDATA[Creating a plugin for jQuery is incredibly simple and is a very useful way to abstract complex behaviours so that they can be used repeatedly as part of your jQuery &#8220;chains&#8221;. 

But, when the time comes, and you&#8217;re faced with the decision to either create&#8230;]]></description>
			<content:encoded><![CDATA[<p>Creating a plugin for jQuery is incredibly simple and is a very useful way to abstract complex behaviours so that they can be used repeatedly as part of your jQuery &#8220;chains&#8221;. </p>

<p>But, when the time comes, and you&#8217;re faced with the decision to either create a jQuery plugin or to simply create a regular function, everything suddenly becomes quite complicated. First, you&#8217;ll wonder whether the piece of behaviour you want to abstract is best kept under the jQuery namespace, and then you&#8217;ll doubt its applicability to the DOM-centred jQuery chain, and then sometimes you&#8217;ll recede to something you&#8217;re much more comfortable with, a regular ol&#8217; JavaScript function.</p>

<p>If we forget about the plugins available online, and we simply focus on <em>your</em> plugins, made and used within a specific project, then the question of whether a plugin is really the right route becomes all the more difficult to answer. Are you going to benefit from extending jQuery&#8217;s API? Will the readability of your code benefit?</p>

<p>For example:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> applyColors<span class="br0">&#40;</span>elems<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $<span class="br0">&#40;</span>elems<span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
        color<span class="sy0">:</span> config.<span class="me1">color</span><span class="sy0">,</span>
        backgroundColor<span class="sy0">:</span> config.<span class="me1">bgColor</span><span class="sy0">,</span>
        borderColor<span class="sy0">:</span> config.<span class="me1">bdColor</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">// Call it:</span>
<span class="kw2">var</span> myElems <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'div.something'</span><span class="br0">&#41;</span><span class="sy0">;</span>
applyColors<span class="br0">&#40;</span>myElems<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p><code>applyColors</code> encapsulates some behaviour that is needed frequently, and that&#8217;s why it&#8217;s been abstracted into a function. To some, this approach is lacking in that it doesn&#8217;t harness the full power of jQuery, and more specifically, jQuery&#8217;s plugin mechanism. How about this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span class="me1">fn</span>.<span class="me1">applyColors</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
        color<span class="sy0">:</span> config.<span class="me1">color</span><span class="sy0">,</span>
        backgroundColor<span class="sy0">:</span> config.<span class="me1">bgColor</span><span class="sy0">,</span>
        borderColor<span class="sy0">:</span> config.<span class="me1">bdColor</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Call it:</span>
$<span class="br0">&#40;</span><span class="st0">'div.something'</span><span class="br0">&#41;</span>.<span class="me1">applyColors</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Cleaner? More readable? I think so.</p>

<p>Many developers are not prepared to extend jQuery&#8217;s API with their own simple abstractions. I don&#8217;t know why. But, I hope, that jQuery macros can help in lowering the barrier to extending jQuery.</p>

<span id="more-1327"></span>

<h2>The humble macro</h2>

<p><em>(the following examples require <code>jQuery.macro</code>, which you can <a href="http://github.com/jamespadolsey/jQuery-Plugins/tree/master/macro/">get at Github</a>!)</em></p>

<p>If we take the example from above, we can create a macro with the same functionality:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span class="me1">macro</span><span class="br0">&#40;</span><span class="st0">'applyColors'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    color<span class="sy0">:</span> config.<span class="me1">color</span><span class="sy0">,</span>
    backgroundColor<span class="sy0">:</span> config.<span class="me1">bgColor</span><span class="sy0">,</span>
    borderColor<span class="sy0">:</span> config.<span class="me1">bdColor</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Call it:</span>
$<span class="br0">&#40;</span><span class="st0">'div.something'</span><span class="br0">&#41;</span>.<span class="me1">applyColors</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p><code>jQuery.macro</code> allows you to record a set of jQuery method calls, and it will create a regular jQuery plugin that will play back the macro. A more verbose example:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> myMacro <span class="sy0">=</span> jQuery.<span class="me1">macro</span><span class="br0">&#40;</span><span class="st0">'myMacro'</span><span class="br0">&#41;</span><span class="sy0">;</span>
myMacro.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">'color'</span><span class="sy0">,</span> <span class="st0">'red'</span><span class="br0">&#41;</span>.<span class="me1">scrollTop</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">'foo'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
jQuery<span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span>.<span class="me1">myMacro</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// All that stuff happens! css()-&gt;scrollTop()-&gt;addClass()</span>
&nbsp;
myMacro.<span class="me1">removeClass</span><span class="br0">&#40;</span><span class="st0">'bar'</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// Record another action</span>
&nbsp;
jQuery<span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span>.<span class="me1">myMacro</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// css()-&gt;scrollTop()-&gt;addClass()-&gt;removeClass()</span></pre></div></div>




<p>Calling <code>jQuery.macro</code> will give you a macro object, which you can think of as a blank disc that will record anything you do. </p>

<p>This is obviously not a replacement for regular jQuery plugins; it&#8217;s simply an easier way to abstract multiple simple behaviours, without having to get caught up in the plugin dilemma.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// Record a macro:</span>
jQuery.<span class="me1">macro</span><span class="br0">&#40;</span><span class="st0">'foo'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    color<span class="sy0">:</span><span class="st0">'red'</span><span class="sy0">,</span>
    border<span class="sy0">:</span> <span class="st0">'1px solid #000'</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Create a plugin:</span>
jQuery.<span class="me1">fn</span>.<span class="me1">foo</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
        color<span class="sy0">:</span><span class="st0">'red'</span><span class="sy0">,</span>
        border<span class="sy0">:</span> <span class="st0">'1px solid #000'</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//... Both have, effectively, the same result ...</span></pre></div></div>




<p><code>jQuery.macro</code> aims to lower the barrier to extending jQuery&#8217;s API &#8212; to rid your global namespace of misplaced functions &#8212; to spare you the superfluous function notation when creating a plugin that only calls a bunch of jQuery methods.</</p>

<p>Like I said, this is certainly not a replacement for regular plugin creation. When recording a macro you are without many luxuries, like a nice closure to work within, plugin arguments/options, using jQuery getters etc.</p>

<p>A jQuery plugin is something that allows you to deeply embed your abstracted behaviour within the jQuery API, a macro is essentially just a list of method calls &#8212; even though each macro disguises itself as a plugin, it is meant for a wholly different purpose.</p>

<p class="video">Please <a href="http://github.com/jamespadolsey/jQuery-Plugins/tree/master/macro/">download and experiment with <code>jQuery.macro</code></a>.</p>

<p>To round off this post, a <em>last ditch</em> example:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span class="me1">macro</span><span class="br0">&#40;</span><span class="st0">'cousins'</span><span class="br0">&#41;</span>.<span class="me1">parent</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">siblings</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">children</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
jQuery<span class="br0">&#40;</span><span class="st0">'#elem'</span><span class="br0">&#41;</span>.<span class="me1">cousins</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// YEH!</span></pre></div></div>


]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/macros-in-jquery/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Gapless wall of images</title>
		<link>http://james.padolsey.com/javascript/gapless-wall-of-images/</link>
		<comments>http://james.padolsey.com/javascript/gapless-wall-of-images/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 21:35:24 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Procrastination]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1315</guid>
		<description><![CDATA[Given a set of arbitrarily sized images and a container with fixed dimensions, fit as many of the images within the container as you can, without leaving any big gaps in between images. How would you do it?

Well, I had a go, and this&#8230;]]></description>
			<content:encoded><![CDATA[<p>Given a set of arbitrarily sized images and a container with fixed dimensions, fit as many of the images within the container as you can, without leaving any big gaps in between images. How would you do it?</p>

<p>Well, I had a go, and this is a brief overview of how it works.</p>

<p>Obviously, it wasn&#8217;t possible to simply line the images up as a grid, because they all have varying (and unpredictable) widths and heights. Considering this, I only saw one logical way of doing it&#8230; progressively!</p>

<p>Place an image &#8212; determine the remaining space and use it to place the next image, and so on. Placing the first image is easy; just stick it up in the top left corner and go from there!</p>

<p><img alt="Diagram of placement mechanism" src="http://james.padolsey.com/wp-content/uploads/PhotoWallDiagram.png" /></p>

<p>A single placement will open up, potentially, three new positions. Out of them, the top-most one could be used for next placement, or the left-most, whichever you fancy.</p>

<p>I chose to track the availability of pixels using a basic two-dimensional array:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// Nine occupied pixels:</span>
<span class="br0">&#91;</span>
    <span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#93;</span>
<span class="br0">&#93;</span><span class="sy0">;</span></pre></div></div>




<p>The width given to any image upon placement is defined by the amount of pixels free to the right of that image and the height is calculated similarly. There&#8217;s a configurable maximum width and height so that the first image doesn&#8217;t take up the entire space. Eventually, there is no space left, and the procedure halts.</p>

<p>You can find the code <strong><a href="http://github.com/jamespadolsey/PhotoWall">at Github, under &#8220;PhotoWall&#8221;</a></strong>.</p>

<p>I&#8217;ve also put a <a href="http://qd9.co.uk/projects/photoWall/src/demo.html">pretty boring demo</a> online, and yes, I know it&#8217;s a bit messed up at the bottom edge &#8212; like everything, it&#8217;s a work in progress <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/gapless-wall-of-images/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easing in jQuery 1.4a2</title>
		<link>http://james.padolsey.com/javascript/easing-in-jquery-1-4a2/</link>
		<comments>http://james.padolsey.com/javascript/easing-in-jquery-1-4a2/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 21:50:43 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cool Stuff]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1308</guid>
		<description><![CDATA[I&#8217;m happy to write that <a href="http://james.padolsey.com/javascript/getting-fancy-with-easing/">my proposed change</a> has been <a href="http://github.com/jquery/jquery/commit/93fdbeb963a9c350f807818c7cc99982942a92f3">accepted</a> into the jQuery &#8220;fx&#8221; core. I thought it worth explaining how this feature works and how you can make it work in your code.

Currently, jQuery (1.3) only allows you to specify one easing function per&#8230;]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m happy to write that <a href="http://james.padolsey.com/javascript/getting-fancy-with-easing/">my proposed change</a> has been <a href="http://github.com/jquery/jquery/commit/93fdbeb963a9c350f807818c7cc99982942a92f3">accepted</a> into the jQuery &#8220;fx&#8221; core. I thought it worth explaining how this feature works and how you can make it work in your code.</p>

<p>Currently, jQuery (1.3) only allows you to specify one easing function per animation. This is perfectly fine in most situations but in some unique cases there is a need for a different easing function for each animated CSS property. I mentioned, in <a href="http://james.padolsey.com/javascript/getting-fancy-with-easing/">my previous post</a> on the topic, that such a thing could be emulated by initiating two separate animations and disabling the default animation queueing on the first one. It does work, but it&#8217;s a bit of a hack, and I felt that the API could be easily extended to serve these types of use cases.</p>

<p>So, as of <a href="http://blog.jquery.com/2009/12/18/jquery-14-alpha-2-released/">jQuery 1.4 alpha version 2</a>, jQuery provides you with the possibility of defining an easing function (or rather, the name of an easing function) for each property that you&#8217;re animating. It&#8217;s done in the following manner:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span class="br0">&#40;</span>myElement<span class="br0">&#41;</span>.<span class="me1">animate</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    left<span class="sy0">:</span> <span class="br0">&#91;</span><span class="nu0">500</span><span class="sy0">,</span> <span class="st0">'swing'</span><span class="br0">&#93;</span><span class="sy0">,</span>
    top<span class="sy0">:</span> <span class="br0">&#91;</span><span class="nu0">200</span><span class="sy0">,</span> <span class="st0">'easeOutBounce'</span><span class="br0">&#93;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Another possibility is to define the per-property easing functions within the optional options object, passed as the second argument to <code>animate</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span class="br0">&#40;</span>myElement<span class="br0">&#41;</span>.<span class="me1">animate</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    left<span class="sy0">:</span> <span class="nu0">500</span><span class="sy0">,</span>
    top<span class="sy0">:</span> <span class="nu0">200</span>
<span class="br0">&#125;</span><span class="sy0">,</span> <span class="br0">&#123;</span>
    specialEasing<span class="sy0">:</span> <span class="br0">&#123;</span>
        left<span class="sy0">:</span> <span class="st0">'swing'</span><span class="sy0">,</span>
        top<span class="sy0">:</span> <span class="st0">'easeOutBounce'</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p class="video">
    <strong><a href="/demos/jquery/easing/easing-jq14.html">See a demo!</a></strong>
</p>

<p>The conventional easing argument can still be used to set a default easing function for those properties that don&#8217;t already specify a &#8220;special&#8221; easing function:</p>

<span id="more-1308"></span>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span class="br0">&#40;</span>myElement<span class="br0">&#41;</span>.<span class="me1">animate</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    x<span class="sy0">:</span> <span class="br0">&#91;</span><span class="nu0">100</span><span class="sy0">,</span> <span class="st0">'easeInQuad'</span><span class="br0">&#93;</span><span class="sy0">,</span>
    y<span class="sy0">:</span> <span class="br0">&#91;</span><span class="nu0">100</span><span class="sy0">,</span> <span class="st0">'easeOutBounce'</span><span class="br0">&#93;</span><span class="sy0">,</span>
    z<span class="sy0">:</span> <span class="nu0">100</span>
<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">1000</span><span class="sy0">,</span> <span class="st0">'linear'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>The <code>z</code> property will be animated using the <code>linear</code> easing function, and the other two properties (<code>x</code> and <code>y</code>) will be animated using their respective easing functions.</p>

<p>Any array specified as a value within the &#8220;properties&#8221; object (the first argument to <code>animate</code>) is assumed to be specifying both a value and an easing function just for that property. You can continue to use <code>animate</code> in the traditional way; no incompatibilities have arisen!</p>

<p>The easing function you specify is the <em>only</em> easing function that will be used for that property. Unlike the demos shown in my previous post on the topic, the functions are not overlaid &#8212; it&#8217;s either one or the other.</p>

<p>Note that you must <a href="http://gsgd.co.uk/sandbox/jquery/easing/">download the easing functions</a> in order to use them in your animations &#8212; they&#8217;re not included in the jQuery core (other than the default &#8220;swing&#8221;, and &#8220;linear&#8221;).</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/easing-in-jquery-1-4a2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
