<?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>Eric Hynds - Website Developer</title>
	<atom:link href="http://www.erichynds.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.erichynds.com</link>
	<description>Web Developer&#039;s Ramblings on ColdFusion, jQuery, MySQL, and other technologies.</description>
	<lastBuildDate>Wed, 24 Feb 2010 15:08:31 +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>FamFamFam Silk Icon Set in Base64 Encoding</title>
		<link>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/</link>
		<comments>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 15:05:26 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[icons]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=348</guid>
		<description><![CDATA[This one can be filed under both &#8220;total impractical&#8221; and &#8220;somewhat useful&#8221;.  For those who haven&#8217;t heard of this trick before, base64 encoding enables you to embed binary image data directly into your HTML or CSS source.  Therefore, instead of the browser requesting two files from the server &#8211; the CSS file and [...]


Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/test/' rel='bookmark' title='Permanent Link: CFIMAGE resize after CFFILE upload'>CFIMAGE resize after CFFILE upload</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This one can be filed under both &#8220;total impractical&#8221; and &#8220;somewhat useful&#8221;.  For those who haven&#8217;t heard of this trick before, base64 encoding enables you to embed binary image data directly into your HTML or CSS source.  Therefore, instead of the browser requesting two files from the server &#8211; the CSS file and the image file &#8211; only the CSS file need be requested.  There are a number of drawbacks/pros/cons to this approach: IE doesn&#8217;t support it, file size limitations, larger CSS files, inability to separately cache CSS vs. images, etc. etc., but if you feel the need to use FamFamFam Silk icon&#8217;s in base64, look no further.</p>
<ul>
<li><a href="/examples/famfamfam/icons.css">The CSS file</a>.  It&#8217;s 2.1mb, so be careful.</li>
<li><a href="/examples/famfamfam/">Examples</a></li>
</ul>
<p>I use icons very liberally on all my projects; they&#8217;re classy and aid in usability.  I typically start with a &#8220;.icon&#8221; class that configures the correct padding/background details for the element, followed by a &#8220;.icon-*&#8221; class, which sets the background-image property to that of the desired icon.  For example, this CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">.<span style="color: #993333;">icon</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span><span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">3px</span> 0 <span style="color: #933;">3px</span> <span style="color: #933;">19px</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.icon-pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">/images/icons/pdf.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Applied to this anchor element:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;icon icon-pdf&quot;</span>&gt;</span>Download PDF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>Produces:  &nbsp;<a class="icon pdf" href="#">Download PDF</a></p>
<p>Can you dig it?<span id="more-348"></span>  The same .icon-pdf class in base64 looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.icon-pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>data<span style="color: #00AA00;">:</span>image/png<span style="color: #00AA00;">;</span>base64<span style="color: #00AA00;">,</span>iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHhSURBVDjLjZPLSxtRFIfVZRdWi0oFBf<span style="color: #00AA00;">+</span>BrhRx5dKVYKG4tLhRqlgXPmIVJQiC60JCCZYqFHQh7rrQlUK7aVUUfCBRG5RkJpNkkswrM5NEf73n6gxpHujAB/fOvefjnHM5VQCqCPa1MNoZnU/Qxqhx4woE7ZZlpXO53F0<span style="color: #00AA00;">+</span>n0c52Dl8Pt/nQkmhoJOCdUWBsvQJ2u4ODMOAwvapVAqSJHGJKIrw<span style="color: #00AA00;">+</span>/2uxAmuJgFdMDUVincSxvEBTNOEpmlIp9OIxWJckMlkoOs6AoHAg6RYYNs2kp4RqOvfuIACVFVFPB4vKYn3pFjAykDSOwVta52vqW6nlEQiwTMRBKGygIh9GEDCMwZH6EgoE<span style="color: #00AA00;">+</span>qHLMuVBdbfKwjv3yE6Ogjz/PQ/CZVDPSFRRYE4/RHy1y8wry8RGWGSqyC/nM1meX9IQpQV2JKIUH8vrEgYmeAFwuPDCHa9QehtD26HBhCZnYC8ucGzKSsIL8wgsjiH1PYPxL<span style="color: #00AA00;">+</span>vQvm5B/3sBMLyIm7GhhCe90BaWykV/Gp<span style="color: #00AA00;">+</span>VR9oqPVe9vfBTsruM1HtBKVPmFIUNusBrV3B4ev6bsbyXlPdkbr/u<span style="color: #00AA00;">+</span>StHUkxruBPY<span style="color: #00AA00;">+</span>0KY8f38oWX/byvNAdluHNLeOxDB<span style="color: #00AA00;">+</span>uyQQfPCWZ3NT69BYJWkjxjnB1o9Fv/ASQ5s<span style="color: #00AA00;">+</span>ABz8i2AAAAAElFTkSuQmCC<span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>With this strategy in mind, I took each Silk icon and created a <code>.icon-*</code> class, where <code>*</code> is the name of the icon (underscores replaced with a hyphen).  Each icon has a background-image property of the base64 encoded image.</p>
<h2>The Code</h2>
<p>The code to convert a directory of icons into base64 representation is very straightforward and revolves around ColdFusion&#8217;s fileReadBinary() and toBase64():</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffunction</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;base64CSS&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> returntype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;void&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;imgdir&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the directory of images to base64 convert&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;destination&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the path and name to dump the css file&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;examples&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the path and name to dump the example html&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> images <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> icon <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span><span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- read the image directory ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdirectory</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;images&quot;</span> directory<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#arguments.imgdir#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- write our CSS and example files ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> filewrite<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">expandpath</span><span style="color: #0000FF;">&#40;</span>arguments.destination<span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">structkeyexists</span><span style="color: #0000FF;">&#40;</span>arguments, <span style="color: #009900;">&quot;examples&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> filewrite<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">expandpath</span><span style="color: #0000FF;">&#40;</span>arguments.examples<span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- loop through each image ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfloop</span> <span style="color: #0000FF;">query</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;images&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- read the image and convert it to base64 ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.<span style="color: #0000FF;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">toBase64</span><span style="color: #0000FF;">&#40;</span>fileReadBinary<span style="color: #0000FF;">&#40;</span>directory <span style="color: #0000FF;">&amp;</span><span style="color: #009900;">'/'</span><span style="color: #0000FF;">&amp;</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- famfamfam icons separate words with an underscore.  hyphens look better in CSS ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.<span style="color: #0000FF;">name</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">replace</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">replace</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">name</span>, <span style="color: #009900;">&quot;_&quot;</span>, <span style="color: #009900;">&quot;-&quot;</span>, <span style="color: #009900;">&quot;all&quot;</span><span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">'.png'</span>, <span style="color: #009900;">''</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- create the actual CSS and HTML ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.css <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;.icon-&quot;</span> <span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">&quot; { background-image:url(data:image/png;base64,&quot;</span> <span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">data</span><span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">&quot;) }&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.html <span style="color: #0000FF;">=</span> <span style="color: #009900;">'&lt;div&gt;&lt;span class=&quot;icon icon-'</span><span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&amp;</span><span style="color: #009900;">'&quot;&gt;'</span> <span style="color: #0000FF;">&amp;</span> icon.<span style="color: #0000FF;">name</span> <span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">'&lt;/span&gt;'</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- append to the css file ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;append&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(arguments.destination)#&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#icon.css#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- append to the examples file ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">structkeyexists</span><span style="color: #0000FF;">&#40;</span>arguments, <span style="color: #009900;">&quot;examples&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
			<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;append&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(arguments.examples)#&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#icon.html#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfset</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfloop</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cffunction</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> base64CSS<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;/home/ehynds/Assets/icons/silk/&quot;</span>, <span style="color: #009900;">&quot;icons.css&quot;</span>, <span style="color: #009900;">&quot;examples.htm&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span></pre></div></div>

<p>I started by creating a &#8220;base64CSS&#8221; function which accepts three arguments: a path to a directory of images, the CSS file to generate, and the examples HTML file to generate.  cfdirectory reads the image directory, the two CSS/example files are created, and then I loop through each image in the directory.  Each image is read as binary data and converted to base64, given a name (separated with hyphens instead of underscores), and the CSS and HTML is generated.  Finally, the CSS is appended to the CSS file and the HTML to the examples file.</p>
<p>Simple, right?  Does anyone else use some kind of icon class?  I&#8217;d like to see other approaches!</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/test/' rel='bookmark' title='Permanent Link: CFIMAGE resize after CFFILE upload'>CFIMAGE resize after CFFILE upload</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery MultiSelect Plugin w/ ThemeRoller Support</title>
		<link>http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/</link>
		<comments>http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 14:47:03 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=240</guid>
		<description><![CDATA[Update 02/15/2010: Version 0.4.1 is out!  Download below, and read the changelog.
I&#8217;ve been working on a multiple select plugin on and off for the past couple months and finally have it stable enough for a first release.  When I started this project my intentions were only to re-factor Cory LaViska&#8217;s MultiSelect implementation, but [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/' rel='bookmark' title='Permanent Link: jQuery Related (Dependent) Selects Plugin'>jQuery Related (Dependent) Selects Plugin</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="update" style="margin:5px 0"><b>Update 02/15/2010:</b> Version 0.4.1 is out!  Download below, and read the <a href="http://github.com/ferric84/jquery-multiselect/raw/0.4.1/CHANGELOG" class="blank">changelog</a>.</div>
<p>I&#8217;ve been working on a multiple select plugin on and off for the past couple months and finally have it stable enough for a first release.  When I started this project my intentions were only to re-factor <a href="http://abeautifulsite.net/2008/04/jquery-multiselect/">Cory LaViska&#8217;s MultiSelect</a> implementation, but it quickly turned into a complete re-write with a focus on speed and ThemeRoller support.  This plugin turns an ordinary HTML select control into an elegant drop down list of checkboxes.<br />
<img style="float: right; padding: 5px 0 10px 10px;" src="/examples/jquery-multiselect/screenshot.gif" alt="jQuery MultiSelect" /></p>
<ul>
<li>Current version: 0.4.1 (02/15/2010 &#8211; <a href="http://github.com/ferric84/jquery-multiselect/raw/0.4.1/CHANGELOG" class="blank">Changelog</a>)</li>
<li><a class="blank" href="/examples/jquery-multiselect/examples.htm">View demos</a></li>
<li>Download <a class="blank" href="http://github.com/ferric84/jquery-multiselect/raw/0.4.1/src/jquery.multiselect.js">source (11.7 kb)</a> or <a class="blank" href="http://github.com/ferric84/jquery-multiselect/raw/0.4.1/src/jquery.multiselect.min.js">minified (6.5 kb)</a>, and the <a href="http://github.com/ferric84/jquery-multiselect/raw/0.4.1/jquery.multiselect.css" class="blank">CSS file</a>.</li>
<li>Follow this project on <a class="blank" href="http://github.com/ferric84/jquery-multiselect">GitHub</a></li>
</ul>
<h2>Features</h2>
<ul>
<li>Full ThemeRoller support.</li>
<li>Optgroup support with clickable labels.</li>
<li>Optional header with check all / uncheck all / close links.</li>
<li>Degrades gracefully.</li>
<li>Keyboard support (albeit, somewhat shaky at this point).</li>
<li>Ability to hook into 5 different event callbacks.</li>
<li>Display the checked options in a list with a configurable maximum.</li>
<li>Easily change the position, fade speed, scroll container height, links text, and input text.</li>
<li>The widget width inherits from the original element, but is also configurable as a parameter.</li>
<li>Pre-selected &#038; disabled option support.</li>
<li>bgiframe support.</li>
<li>Only 6.5kb minified.</li>
</ul>
<p><span id="more-240"></span></p>
<h2>Compatibility</h2>
<p>MultiSelect is compatible with jQuery 1.4.0+ and all themes from jQuery UI 1.7+.  This plugin is known to work in (but not limited to):</p>
<ul>
<li>Firefox 2 &#8211; 3.6</li>
<li>IE 6 &#8211; 8</li>
<li>Chrome Beta/4</li>
<li>Safari 4</li>
<li>Opera 10</li>
</ul>
<h2>Usage</h2>
<p>First ensure you&#8217;ve included jQuery 1.4, a jQuery UI theme of your choice, and the jquery.multiselect.css file.  You don&#8217;t need the jQuery UI library itself, just the theme files.  The simplest use of MultiSelect is to bind the widget to your select box:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MySelectBox&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multiple&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MySelectBox&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>Option 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>Option 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Do not forget to set your select boxes to <code>multiple="multiple"</code> if you want this plugin to degrade gracefully.  Any option tags with the <code>selected="selected"</code> attribute will automatically be checked by default, and any option tags with the <code>disabled="disabled"</code> attribute will automatically be disabled by default.</p>
<p>Inside the <code>onOpen</code> callback, <code>this</code> refers to the container holding the header and checkboxes.  If you want to gather all the checkboxes inside the multiselect that just opened, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	onOpen<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> $checkboxes <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</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></pre></div></div>

<p>Inside the <code>onClick</code> callback, <code>this</code> refers to the checkbox that received the event.  Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	onClick<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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checked</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was just checked!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Options for this plugin are exposed in <code>$.fn.multiSelect.defaults</code>, so you can override the default options for all instances like such:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// set the minWidth parameter for all instances to 500 pixels</span>
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">multiSelect</span>.<span style="color: #660066;">defaults</span>.<span style="color: #660066;">minWidth</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Options</h2>
<p>This plugin is configurable with the following options:</p>
<table class="parameters" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:125px">Parameter</th>
<th>Description</th>
<th style="width:115px">Default</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">showHeader</td>
<td class="">A boolean value denoting whether or not to display the header containing the check all, uncheck all, and close links.</td>
<td>true</td>
</tr>
<tr>
<td class="parameter">maxHeight</td>
<td class="">The maximum height in pixels of the scrolling container that holds the checkboxes.</td>
<td>175</td>
</tr>
<tr>
<td class="parameter">minWidth</td>
<td class="">The minimum width in pixels of widget.  Setting to <code>auto</code> (default) will inherit the width from the original select element.</td>
<td>200</td>
</tr>
<tr>
<td class="parameter">checkAllText</td>
<td class="">The default text of the &#8220;Check all&#8221; link.</td>
<td>Check all</td>
</tr>
<tr>
<td class="parameter">unCheckAllText</td>
<td class="">The default text of the &#8220;Uncheck all&#8221; link.</td>
<td>Uncheck all</td>
</tr>
<tr>
<td class="parameter">noneSelected</td>
<td class="">The default text the select box when no options have been selected.</td>
<td>Select options</td>
</tr>
<tr>
<td class="parameter">selectedList</td>
<td class="">A boolean/numeric value denoting whether or not to display the checked opens in a list, and how many.  A number greater than 0 denotes the maximum number of list items to display before switching over to the <code>selectedText</code> parameter.  A value of 0 or <code>false</code> is disabled.</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">selectedText</td>
<td class="">The text to display in the select box when options are selected (if selectedList is false).  The pound sign (#) is automatically replaced by the number of checkboxes selected.  If two pound signs are present in this parameter, the second will be replaced by the total number of checkboxes available.  Example: &#8220;# of # checked&#8221;</td>
<td># selected</td>
</tr>
<tr>
<td class="parameter">position</td>
<td class="">The position of the options menu relative to the input control: top, middle, or bottom.</td>
<td>bottom</td>
</tr>
<tr>
<td class="parameter">shadow</td>
<td class="">A boolean value denoting whether to apply a CSS shadow (class ui-multiselect-shadow).</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">fadeSpeed</td>
<td class="">How fast (in ms) to fade out the options menu on close.</td>
<td>200</td>
</tr>
<tr>
<td class="parameter">onCheck</td>
<td class="">A callback to fire when a checkbox is checked.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onOpen</td>
<td class="">A callback to fire when the options menu is opened.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onCheckAll</td>
<td class="">A callback to fire when the &#8220;Check all&#8221; link is clicked.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onUncheckAll</td>
<td class="">A callback to fire when the &#8220;Uncheck all&#8221; link is clicked.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onOptgroupToggle</td>
<td class="">A callback to fire when the opgroup header is clicked.  An array of checkboxes inside the optgroup is passed as an optional argument.</td>
<td>Function</td>
</tr>
</tbody>
</table>
<p></p>
<h2>Known Issues</h2>
<p>This is the first stable release, but there are a few outstanding issues to be resolved:</p>
<ul>
<li>No onClose callback yet.</li>
<li>Keyboard support isn&#8217;t 100% where I&#8217;d like it to be.</li>
<li>The position should automatically change if the current position will prevent the options menu from showing.  E.g., if the input is at the bottom of the page, the options should appear not appear below the input.</li>
<li>In IE, the shadow parameter increases the width and height of the container, rendering it a few pixels wider than the input and throwing off the alignment.  You can easily edit the CSS file and remove the IE-specific shadow filters, however, or opt not to use it.</li>
</ul>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/' rel='bookmark' title='Permanent Link: jQuery Related (Dependent) Selects Plugin'>jQuery Related (Dependent) Selects Plugin</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>jQuery and Performance: What is &#8216;this&#8217;?</title>
		<link>http://www.erichynds.com/jquery/jquery-and-performance-what-is-this/</link>
		<comments>http://www.erichynds.com/jquery/jquery-and-performance-what-is-this/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:14:50 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=173</guid>
		<description><![CDATA[In my opinion, one of the best jQuery performance tips is to minimize your use of jQuery.  That is, find a balance between using jQuery and plain ol&#8217; JavaScript, and a good place to start is with &#8216;this&#8216;. Many developers use $(this) exclusively as their hammer inside callbacks and forget about this, but the [...]]]></description>
			<content:encoded><![CDATA[<p>In my opinion, one of the best jQuery performance tips is to minimize your use of jQuery.  That is, find a balance between using jQuery and plain ol&#8217; JavaScript, and a good place to start is with &#8216;<code>this</code>&#8216;. Many developers use $(this) exclusively as their hammer inside callbacks and forget about <code>this</code>, but the difference is distinct:</p>
<p>When inside a jQuery method&#8217;s anonymous callback function, <code>this</code> is a reference to the current DOM element.  $(this) turns <code>this</code> into a jQuery object and exposes jQuery&#8217;s methods.  A jQuery object is nothing more than a beefed-up array of DOM elements.</p>
<h2>Digging a little deeper</h2>
<p>As you begin to nest functions the context of <code>this</code> changes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">&gt;&lt;</span>span<span style="color: #339933;">&gt;</span>Hello World<span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #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;">&quot;span&quot;</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><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>self<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example I save <code>this</code> to the variable &#8220;self&#8221; so it can be referenced inside the each() loop.  Otherwise it&#8217;s impossible to reference <code>this</code> as the anchor element inside the loop, because <code>this</code> is now the &#8220;span&#8221; DOM element.</p>
<p>Depending on what you&#8217;re doing, using <code>this</code> is sometimes easier and always faster than $(this).  I&#8217;m not talking about scraping jQuery altogether, but rather recognizing that a lot of DOM interaction (especially the simple stuff) can just as easily be done in vanilla JS using <code>this</code>; you are not <em>required</em> to use the jQuery API.  Example:</p>
<p><span id="more-173"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;test.htm&quot;</span><span style="color: #339933;">&gt;</span>Test<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> href <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// the href property can be accessed through 'this'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A more verbose example demonstrating the uses of <code>this</code> and $(this) to get an anchor&#8217;s href value.  These statements are equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> href<span style="color: #339933;">;</span>
	href <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// sets &quot;href&quot; to &quot;test.htm&quot;</span>
	href <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// also sets &quot;href&quot; to &quot;test.htm&quot; after going through the jQuery attr() method.</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// because $(this) is really just a beefed-up array, we can pull off the DOM element using array notation.</span>
	href <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">href</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>The point here is that turning <code>this</code> into a jQuery object and using the attr() method is more time consuming than just referencing the &#8220;href&#8221; property of <code>this</code>.  When manipulation is involved the performance implications are even greater.</p>
<h2>Benchmarks</h2>
<p>To quantify my claims, I wrote a profiling function that calculates the execution time of looping a block of code 50,000 times:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> profile <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>label<span style="color: #339933;">,</span> code<span style="color: #339933;">,</span> iterations<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> start<span style="color: #339933;">,</span> total<span style="color: #339933;">,</span> i <span style="color: #339933;">=</span> iterations <span style="color: #339933;">=</span> iterations <span style="color: #339933;">||</span> <span style="color: #CC0000;">50000</span><span style="color: #339933;">;</span>
&nbsp;
	start <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> code.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	total <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>start<span style="color: #339933;">;</span>
&nbsp;
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">+</span> <span style="color: #3366CC;">': %d ms (looping %l times)'</span><span style="color: #339933;">,</span> total<span style="color: #339933;">,</span> iterations<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I then setup a page with one anchor element and a click event to call the profile function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// save $(this) and 'this' - their reference change inside the profile function!</span>
	<span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Accessing the href attribute of a link:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'$this.attr(&quot;href&quot;)'</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>
		href <span style="color: #339933;">=</span> $this.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'self.href'</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>
		href <span style="color: #339933;">=</span> self.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'self.getAttribute(&quot;href&quot;)'</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>
		href <span style="color: #339933;">=</span> self.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p class="update"><strong>Update 1/7/2010:</strong> JAlpino pointed out in the comments that the getAttribute(&#8220;href&#8221;) method can be called on <code>this</code> to gain an additional +/- 350ms, as opposed to using <code>this.property</code> syntax.  I&#8217;ve added this method to the benchmarks &amp; results. Thanks, <a href="http://jalpino.com/">JAlpino!</a></p>
<p>The results using jQuery 1.3.2 and 1.4a2 respectively on Firefox 3.5.6:</p>
<div style="text-align: center;"><img src="/examples/this/1-1.gif" alt="" /></div>
<div style="text-align: center;"><img src="/examples/this/1-2.gif" alt="" /></div>
<p><code>self.href</code> provides a 270% improvement when using jQuery 1.3.2 and a 200% improvement when using 1.4a2.  Using the getAttribute() method provides an additional 72% boost over <code>self.href</code>! Keep in mind that in &#8216;the jQuery way&#8217; these numbers do not include the time it takes to convert <code>this</code> into a jQuery object; we&#8217;re caching that outside the profile function, and thus are only benchmarking the performance of the attr() method.  How does <code>this</code> perform over $(this) when used for manipulation?  I ran the profiler again, this time changing the link color to red:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'the jQuery way'</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>
	$this.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#ff0000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'vanilla javascript'</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>
	self.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#ff0000&quot;</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>

<div style="text-align: center;"><img src="/examples/this/2-1.gif" alt="" /></div>
<div style="text-align: center;"><img src="/examples/this/2-2.gif" alt="" /></div>
<p>We&#8217;re looking at a 632% improvement under jQuery 1.3.2 and a 206% improvement under 1.4a2.  Pretty good for changing one line of code.</p>
<h2>Putting <code>this</code> into <code>context</code></h2>
<p>You can pass an optional second argument called context to the jQuery object constructor, which gives jQuery a starting point when searching the DOM for your selector.  The context can be either a DOM element or jQuery object, but ideally should be a DOM element &#8211; like <code>this</code>!  Given the markup below and what we know about <code>this</code> and $(this), the &#8220;span&#8221; child of the anchor can be found four ways:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;testing.htm&quot;</span><span style="color: #339933;">&gt;&lt;</span>span<span style="color: #339933;">&gt;</span>test link<span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// these statements accomplish the same thing</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>0<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: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first example turns <code>this</code> into a jQuery object and calls the find() method.  The second example looks for a span element using <code>this</code> &#8211; a reference to the anchor DOM element &#8211; as the starting point.  Because the context argument in the third example is a jQuery object, jQuery internally converts the statement to $(this).find(&#8220;span&#8221;), which is equivalent to the first example.  We&#8217;re about to see that this is the slowest method, however, probably due to the overhead in converting the context to a find().  The fourth method is equivalent to $(&#8220;span&#8221;, this), except that <code>this</code> is first converted into a jQuery object, and then the DOM node is pulled off the array.</p>
<p>My original plan going into this blog post was to show you the performance benefits you can gain when using <code>this</code> as the context argument to the jQuery constructor as opposed to $(this), but my results show otherwise.  Note that I ran these tests 20,000 times instead of 50,000 (Firefox got cranky):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// cache $(this) and 'this'</span>
	<span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' $this.find() '</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>
		$this.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 20000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' $(&quot;span&quot;, this) '</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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #339933;">,</span> self<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 20000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' $(&quot;span&quot;, $this) '</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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #339933;">,</span> $this<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 20000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	profile<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' $(&quot;span&quot;, $this[0]) '</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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #339933;">,</span> $this<span style="color: #009900;">&#91;</span>0<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: #339933;">,</span> 20000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div style="text-align: center;"><img src="/examples/this/3-1.gif" alt="" /></div>
<div style="text-align: center;"><img src="/examples/this/3-2.gif" alt="" /></div>
<p>When using jQuery 1.3.2, $(this).find() was 4% faster than providing a DOM element as the context and 53% faster than using a jQuery object &#8211; pretty negligible.  1.4a2 shipped with some nice speed improvements; the performance of all four are about the same, but $(this).find() is still a bit faster.</p>
<p>To reiterate, I&#8217;m not saying skip jQuery and code everything in plain JS because it&#8217;ll make your site faster.  The speed improvements I pointed out are trivial but can absolutely add up, especially in larger applications.  There are a number of good reasons to use $(this) over <code>this</code>; cross-browser compatibility is one, the extreme ease of use another, but just be aware <code>this</code> exists.</p>
<p>Click <a href="/examples/this/benchmarks.htm">here</a> to see the code and run these benchmarks yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-and-performance-what-is-this/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Executing jQuery Methods Using Array Notation</title>
		<link>http://www.erichynds.com/jquery/accessing-jquery-methods-using-array-syntax/</link>
		<comments>http://www.erichynds.com/jquery/accessing-jquery-methods-using-array-syntax/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 14:33:23 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=157</guid>
		<description><![CDATA[I was refactoring some code this morning for a plugin I&#8217;m writing and ran into a case where I needed to dynamically execute a jQuery method.  I had a variable that holds the value of a jQuery function I wanted to execute based on which key code was pressed; prev() for the up arrow, [...]]]></description>
			<content:encoded><![CDATA[<p>I was refactoring some code this morning for a plugin I&#8217;m writing and ran into a case where I needed to dynamically execute a jQuery method.  I had a variable that holds the value of a jQuery function I wanted to execute based on which key code was pressed; prev() for the up arrow, and next() for the down arrow.  As it turns out you can do this with array notation.</p>
<p><span id="more-157"></span></p>
<p>These two examples accomplish the same thing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// HTML:  &lt;p&gt;&lt;a href=&quot;&quot;&gt;Click me&lt;/a&gt;&lt;/p&gt;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// vanilla jQuery</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// dynamically calling a method</span>
<span style="color: #003366; font-weight: bold;">var</span> theMethod <span style="color: #339933;">=</span> <span style="color: #3366CC;">'find'</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>theMethod<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Good to know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/accessing-jquery-methods-using-array-syntax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Loop Performance: Caching the &#8216;.length&#8217; Property of an Array</title>
		<link>http://www.erichynds.com/javascript/javascript-loop-performance-caching-the-length-property-of-an-array/</link>
		<comments>http://www.erichynds.com/javascript/javascript-loop-performance-caching-the-length-property-of-an-array/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 21:52:48 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=146</guid>
		<description><![CDATA[I was reading this article from SitePoint earlier about how inefficient the JavaScript is behind Google&#8217;s new Closure library, and saw some interesting discussion about caching an array&#8217;s length property before entering a &#8216;for&#8217; loop. This is what I&#8217;m talking about:

// uncached length property
for&#40;var x=0; x &#60; myArray.length; x++&#41;&#123;&#125;
&#160;
// cached length property
for&#40;var x=0, len=myArray.length; x [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-and-performance-what-is-this/' rel='bookmark' title='Permanent Link: jQuery and Performance: What is &#8216;this&#8217;?'>jQuery and Performance: What is &#8216;this&#8217;?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I was reading <a href="http://www.sitepoint.com/blogs/2009/11/12/google-closure-how-not-to-write-javascript/" class="blank">this</a> article from SitePoint earlier about how inefficient the JavaScript is behind Google&#8217;s new Closure library, and saw some interesting discussion about caching an array&#8217;s length property before entering a &#8216;for&#8217; loop. This is what I&#8217;m talking about:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// uncached length property</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> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> myArray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// cached length property</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> x<span style="color: #339933;">=</span>0<span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>myArray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>I saw this tip and didn&#8217;t think twice about it; I&#8217;ve always been told that caching the length property is indeed faster.  I then saw a couple comments saying otherwise:</p>
<p><span id="more-146"></span></p>
<blockquote>
<p><em>Anyway, the loop performance is not as big deal as you present. Of course, it&#8217;s nicer and more professional to preassign the array length instead of testing it again in every step. But the performance is not such problem. The Javascript basic operations are very fast, making the difference quite unnoticeable. Those two loops performance will differ only in milliseconds for a million-iterations loop in today&#8217;s average computer. This means the same loop traversing an array containing a million items will take about 15 ms if the array.length is tested every time, and about 10 ms if the length is preassigned. So, we are talking about saving nanoseconds per a thousand-iterations loop. This makes no sense from the performance point of view.</em></p>
</blockquote>
<p>And:</p>
<blockquote>
<p><em>These are nearly Identical in performance. Even if the loop does nothing, so that the time spent in the loop is all spent incrementing i and checking its value, it is STILL hard to detect the performance difference. And if you do anything of substance in this loop, the time spent checking against someArray.length is dwarfed by the loop body. This is a stupid complaint.</em></p>
</blockquote>
<p>I have always blindly assumed that the performance hit was due to the interpreter calculating the .length value each time it is referenced, but after talking to some javascript folk (#javascript irc.freenode.net), I discovered that .length is actually a stored value.  Since JavaScript doesn&#8217;t need to re-calculate it on every iteration of the loop, where would the performance hit come from?  Anyway, I thought it would be fun to run some benchmarks of my own to see the difference.  Below is little script I put together, but please take it as a grain of salt because <a href="http://ejohn.org/blog/accuracy-of-javascript-time/" class="blank">JavaScript time is not accurate</a>.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> array <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> start<span style="color: #339933;">,</span> diff<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> nocache<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> cached<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// build array to loop over</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> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1000000</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	array<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// run tests 25 times</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> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// length not cached</span>
	start <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: #660066;">getTime</span><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> y<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
	results.<span style="color: #660066;">nocache</span><span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <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: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// length cached</span>
	start <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: #660066;">getTime</span><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> y<span style="color: #339933;">=</span>0<span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
	results.<span style="color: #660066;">cached</span><span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <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: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// output results</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&lt;p&gt;No cache time: '</span> <span style="color: #339933;">+</span> avg<span style="color: #009900;">&#40;</span>results.<span style="color: #660066;">nocache</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' Values: '</span> <span style="color: #339933;">+</span> results.<span style="color: #660066;">nocache</span>.<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: #3366CC;">''</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&lt;p&gt;Cached time: '</span> <span style="color: #339933;">+</span> avg<span style="color: #009900;">&#40;</span>results.<span style="color: #660066;">cached</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' Values: '</span> <span style="color: #339933;">+</span> results.<span style="color: #660066;">cached</span>.<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: #3366CC;">'&lt;/p&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// logic from http://javascript.about.com/library/blaravg.htm</span>
<span style="color: #003366; font-weight: bold;">function</span> avg<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> av <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> cnt <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> arr.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
	<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> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		e <span style="color: #339933;">=</span> <span style="color: #339933;">+</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>e <span style="color: #339933;">&amp;&amp;</span> arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span> e<span style="color: #339933;">--;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> av <span style="color: #339933;">+=</span> e<span style="color: #339933;">;</span> cnt<span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> av<span style="color: #339933;">/</span>cnt<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code first builds an array with 1,000,000 items, records the execution time of looping over the array 25 times using both methods, and then figures the average.  <a href="/examples/length_cache/chart.jpg">Here are the results:</a></p>
<p class="center"><a href="/examples/length_cache/chart.jpg"><img src="/examples/length_cache/chart.jpg" alt="" style="width:600px" /></a></p>
<p>The results are what I expected except in the Windows versions of Firefox 3.5.2/3.5.5. In all other browsers I tested it is faster to cache the .length value, but in FF3.5 for Windows it is actually faster not to cache.  Internet Explorer receives the most benefit with a ~30% increase in performance when caching, with Opera (Linx) and Chrome (Linux) in fourth and fifth place respectively.  The amount of time is indeed negligible, but every millisecond counts, right?</p>
<p>If nothing else it is interesting to see how long it takes different browsers to loop through a large array.  Firefox and Safari FTW!</p>
</pre>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-and-performance-what-is-this/' rel='bookmark' title='Permanent Link: jQuery and Performance: What is &#8216;this&#8217;?'>jQuery and Performance: What is &#8216;this&#8217;?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/javascript/javascript-loop-performance-caching-the-length-property-of-an-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Related Selects Plugin 0.3</title>
		<link>http://www.erichynds.com/jquery/jquery-related-selects-0-3/</link>
		<comments>http://www.erichynds.com/jquery/jquery-related-selects-0-3/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 22:34:30 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=142</guid>
		<description><![CDATA[I&#8217;ve just pushed version 0.3 of my related selects jQuery plugin to GitHub.  Along with refactoring the code and some minor performance improvements, 0.3 allows you to return data in HTML format instead of JSON.  This gives you direct control over the markup and individual option tags in case you want to use [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/' rel='bookmark' title='Permanent Link: jQuery Related (Dependent) Selects Plugin'>jQuery Related (Dependent) Selects Plugin</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just pushed version 0.3 of my <a href="http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/">related selects jQuery plugin</a> to <a href="http://github.com/ferric84/jquery-related-selects">GitHub</a>.  Along with refactoring the code and some minor performance improvements, 0.3 allows you to return data in HTML format instead of JSON.  This gives you direct control over the markup and individual option tags in case you want to use optgroups, assign classes to options, etc.</p>
<p>I&#8217;m still undecided whether or not I want to add caching.  I thought about it when I first wrote it and a user suggested it, but I cannot picture a use case where caching would be necessary; people are likely to choose an option and either decide it was incorrect and change it, or stick with it.  In either of these cases, caching doesn&#8217;t do anything other than add overhead.  If a user selects an option and then continuously changes/reverts back to it, caching would be helpful, but how often is this likely to happen?</p>
<p>Anyways, here&#8217;s she is:</p>
<ul>
<li><a href="http://www.erichynds.com/examples/jquery-related-selects/">Examples</a></li>
<li><a href="http://github.com/ferric84/jquery-related-selects">Download &amp; documentation</a></li>
<li><a href="http://plugins.jquery.com/project/related-selects">jQuery plugin project page</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/' rel='bookmark' title='Permanent Link: jQuery Related (Dependent) Selects Plugin'>jQuery Related (Dependent) Selects Plugin</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-related-selects-0-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Related (Dependent) Selects Plugin</title>
		<link>http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/</link>
		<comments>http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 15:20:50 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=113</guid>
		<description><![CDATA[I&#8217;ve finally gotten around to writing &#38; releasing a jQuery plugin that allows you to create any number of related/dependent select boxes in a form.  I&#8217;ve yet to find a decent solution and needed something that was fairly customizable and could retrieve data using AJAX/JSON, so here she is.

Demos &#38; examples
jQuery plugin page
Source &#38; [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-related-selects-0-3/' rel='bookmark' title='Permanent Link: jQuery Related Selects Plugin 0.3'>jQuery Related Selects Plugin 0.3</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve finally gotten around to writing &amp; releasing a jQuery plugin that allows you to create any number of related/dependent select boxes in a form.  I&#8217;ve yet to find a decent solution and needed something that was fairly customizable and could retrieve data using AJAX/JSON, so here she is.</p>
<ul>
<li><a href="http://www.erichynds.com/examples/jquery-related-selects/" class="blank">Demos &amp; examples</a></li>
<li><a href="http://plugins.jquery.com/project/related-selects" class="blank">jQuery plugin page</a></li>
<li><a href="http://github.com/ferric84/jquery-related-selects" class="blank">Source &amp; documentation @ GitHub</a></li>
</ul>
<p>From the GitHub docs:</p>
<p>jQuery Related Selects is a plugin that allows you to create any number of select boxes whose options are determined upon the selected value of another.  You pass an array or object of select box names, and the select boxes will depend on each other in the order in which they are passed in.</p>
<p>When a select box is changed an AJAX request is sent to the file specified in the onChangeLoad property, passing the selected value of each select box as parameters.  For now the data must be returned in JSON format.  An &lt;option&gt; must have a legitimate value in order to trigger the script.  An &lt;option&gt; where the value is blank (value=&quot;&quot;) is used for the defaultOptionText option (see below).</p>
<p>This script provides a high level of customization.  You can set the options once for each select to use, or set the options on a select-by-select basis. You can also start your markup with pre-populated options and selected values.</p>
<h2>Usage</h2>
<p>A basic example.  Changing the &quot;stateID&quot; select updates the &quot;countyID&quot; select and so on.  The values of all four select boxes are serialized and passed to datasupplier.php, which returns data for the next select box in JSON format.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stateID&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>Choose State<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MA&quot;</span>&gt;</span>Massachusetts<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;VT&quot;</span>&gt;</span>Vermont<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;countyID&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>Choose County<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;townID&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>Choose Town<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;villageID&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>Choose Village<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">relatedSelects</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	onChangeLoad<span style="color: #339933;">:</span> <span style="color: #3366CC;">'datasupplier.php'</span><span style="color: #339933;">,</span>
	selects<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'stateID'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'countyID'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'townID'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'villageID'</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Options</h2>
<p><code>selects</code></p>
<p>Required.  An array or object of select boxes you want to be dependent.  Each one will depend on each other <b>in the order in which they are passed in.</b>  If  you want to override any of the options below on a select-by-select basis you can pass this as an object.  See examples.</p>
<p><code>onChangeLoad</code></p>
<p>Required.  The file to call to retrieve data when a select box is changed.  Data must be returned in JSON format for now.</p>
<p><code>loadingMessage</code></p>
<p>Optional.  The text to change the sibling select box to during the AJAX request.</p>
<p><code>defaultOptionText</code></p>
<p>Optional.  The text inside the default &lt;option&gt; tag after AJAX populates a select box.  This is typically the text where you instruct the user to choose an option.  If this option is not provided and an &lt;option&gt; tag with a blank value attribute exists in your markup, that text will be used.</p>
<p><code>onLoadingStart</code></p>
<p>Optional.  A callback to fire when the AJAX call starts.</p>
<p><code>onLoadingEnd</code></p>
<p>Optional.  A callback to fire when the AJAX call ends.</p>
<p><code>onChange</code></p>
<p>Optional.  A callback to fire when a select box is changed.</p>
<p class="update"><b>11-18-2009:</b> Bug fix &amp; new features update</p>
<p>I&#8217;ve fix a bug where refreshing the page makes all selects become active, and added a new option and callback:</p>
<p><code>disableIfEmpty</code></p>
<p>Set this option to true to keep the next select disabled if the previous does not return any results.</p>
<p><code>onEmptyResult</code></p>
<p>Optional. A callback to fire when no results are returned after changing a select.</p>
<p class="update"><b>12-2-2009:</b> Update: Return data in HTML format</p>
<p>Data can now be returned in HTML format by using the new <code>dataType</code> option.  Set it to either &#8216;html&#8217; or &#8216;json&#8217; (default).</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-related-selects-0-3/' rel='bookmark' title='Permanent Link: jQuery Related Selects Plugin 0.3'>jQuery Related Selects Plugin 0.3</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Creating a Mint.com Style Idle Logout Timer Using jQuery</title>
		<link>http://www.erichynds.com/jquery/creating-a-mint-com-style-idle-logout-timer-using-jquery/</link>
		<comments>http://www.erichynds.com/jquery/creating-a-mint-com-style-idle-logout-timer-using-jquery/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:24:58 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/blog/?p=66</guid>
		<description><![CDATA[One of our real estate applications allows agents to write up e-mails and send MLS property listings to prospective buyers.  One problem we ran into was sometimes agents would take a long, long time to write their e-mails.  Somewhere in the middle of writing, the agent&#8217;s session would expire, so when the form [...]]]></description>
			<content:encoded><![CDATA[<p>One of our real estate applications allows agents to write up e-mails and send MLS property listings to prospective buyers.  One problem we ran into was sometimes agents would take a long, long time to write their e-mails.  Somewhere in the middle of writing, the agent&#8217;s session would expire, so when the form was finally submitted, s/he was brought back to the login page and the e-mail was lost.  While this probably should be been fixed server-side, I thought this would be a perfect opportunity to re-create Mint.com&#8217;s idle timeout functionality in jQuery.</p>
<p>Essentially, if a user is considered idle after a certain period of time, a message appears informing him/her that the session is about to timeout and offers a link to continue the session.  &quot;Idle&quot; is a state where the following events have not been fired after a specified period of time: mousemove, keydown DOMMouseScroll, mousewheel, and mousedown.  If the user clicks on the link to continue, an AJAX request is fired off that will keep the session alive.  I am not sure about other programming languages, but in ColdFusion calling a .cfm or .cfc file will reset Java&#8217;s internal session timeout clock back to zero.</p>
<p><img src="/examples/jquery-idle-timeout/screenshot.gif" alt="jQuery Idle Timeout" /></p>
<p>The code is fairly straightforward thanks to Paul Irish&#8217;s <a href="http://paulirish.com/2009/jquery-idletimer-plugin/" class="blank">idleTimer plugin</a> which I use to handle the idle detection.  The rest involves interacting with the idle state and recovering from it when the user is no longer idle.</p>
<p>You can see an example <a href="/examples/jquery-idle-timeout/index.htm">here</a>.</p>
<p>The (heavily commented) code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> $bar <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#idletimeout&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// id of the warning div</span>
		$countdown <span style="color: #339933;">=</span> $bar.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// span tag that will hold the countdown value</span>
		redirectAfter <span style="color: #339933;">=</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// number of seconds to wait before redirecting the user</span>
		redirectTo <span style="color: #339933;">=</span> <span style="color: #3366CC;">'timeout.htm'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// URL to relocate the user to once they have timed out</span>
		keepAliveURL <span style="color: #339933;">=</span> <span style="color: #3366CC;">'keepalive.php'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// URL to call to keep the session alive, if the link in the warning bar is clicked</span>
		expiredMessage <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Your session has expired.  You are being logged out for security reasons.'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// message to show user when the countdown reaches 0</span>
		running <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// var to check if the countdown is running</span>
		timer<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// reference to the setInterval timer so it can be stopped</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// start the idle timer.  the user will be considered idle after 2 seconds (2000 ms)</span>
	$.<span style="color: #660066;">idleTimer</span><span style="color: #009900;">&#40;</span>2000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// bind to idleTimer's idle.idleTimer event</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;idle.idleTimer&quot;</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>
&nbsp;
		<span style="color: #006600; font-style: italic;">// if the user is idle and a countdown isn't already running</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>document<span style="color: #339933;">,</span><span style="color: #3366CC;">'idleTimer'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'idle'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>running <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> redirectAfter<span style="color: #339933;">;</span>
			running <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// set inital value in the countdown placeholder</span>
			$countdown.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span> redirectAfter <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// show the warning bar</span>
			$bar.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// create a timer that runs every second</span>
			timer <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>
				counter <span style="color: #339933;">-=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// if the counter is 0, redirect the user</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>counter <span style="color: #339933;">===</span> 0<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					$bar.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span> expiredMessage <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> redirectTo<span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
					$countdown.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span> counter <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: #339933;">,</span> 1000<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;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// if the continue link is clicked..</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> $bar<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// stop the timer</span>
		clearInterval<span style="color: #009900;">&#40;</span>timer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// stop countdown</span>
		running <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// hide the warning bar</span>
		$bar.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// ajax call to keep the server-side session alive</span>
		$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span> keepAliveURL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The idleTimer plugin fires a custom event called idle.idleTimer which this plug-in binds to.  As long as the countdown is not already running, the countdown value is set (starts at 30), the warning bar slides down, and a timer is created using setInterval.  This timer fires every second until either the link is clicked to stop it, or the counter reaches 0 and the user is redirected.  If the link is clicked, the timer is stopped, the warning bar slides back up, and a server-side file is called that will actually keep the session alive.</p>
<p>Again, you can see a demo <a href="/examples/jquery-idle-timeout/index.htm">here</a> and view source to see the structure/styling of the warning bar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/creating-a-mint-com-style-idle-logout-timer-using-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Automatically crawl a website looking for errors</title>
		<link>http://www.erichynds.com/ubuntulinux/automatically-crawl-a-website-looking-for-errors/</link>
		<comments>http://www.erichynds.com/ubuntulinux/automatically-crawl-a-website-looking-for-errors/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 20:02:33 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[Ubuntu/Linux]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/blog/?p=58</guid>
		<description><![CDATA[For all you Linux/Mac folks out there, one useful way to crawl your website looking for errors (web server or server-side; any kind of error that would appear in your log files) is to use the terminal. Start by ensuring your log levels are set correctly for whatever you want to catch; if you&#8217;re using [...]]]></description>
			<content:encoded><![CDATA[<p>For all you Linux/Mac folks out there, one useful way to crawl your website looking for errors (web server or server-side; any kind of error that would appear in your log files) is to use the terminal. Start by ensuring your log levels are set correctly for whatever you want to catch; if you&#8217;re using Apache set the right <code>LogLevel</code>, if you want to catch PHP errors check your <code>error_log</code> and <code>error_reporting</code> variables, etc. You&#8217;ll also want to backup and clear your existing log files to keep this cleaner.</p>
<p>Next, open up your terminal and issue this command:</p>
<p><code>wget -r --level=10 --delete-after -nd http://www.mydomain.com/</code></p>
<p>wget will crawl through the site and retrieve just about everything 10 levels deep (change <code>--level=10</code> to however many levels you want). <code>-r</code> searches recursively, <code>--delete-after</code> deletes the wget&#8217;d files right after downloading them, and <code>-nd</code> prevents wget from creating directories locally.</p>
<p>Once done, check your log files and see what it found.  If you&#8217;re impatient like me, you can <code>tail -f</code> your log files on the server to stream new data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/ubuntulinux/automatically-crawl-a-website-looking-for-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computer Janitor corrupting AIR installs on Ubuntu 8.04</title>
		<link>http://www.erichynds.com/ubuntulinux/computer-janitor-corrupting-air-installs-on-ubuntu-8-04/</link>
		<comments>http://www.erichynds.com/ubuntulinux/computer-janitor-corrupting-air-installs-on-ubuntu-8-04/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 16:09:01 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[Ubuntu/Linux]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/blog/?p=54</guid>
		<description><![CDATA[This is one of those things that took far too long to figure out so I figure I&#8217;d save some people the headache.  Running computer janitor on Ubuntu 8.04 will remove AIR (v. 1.5 at the time of this writing) if you have it installed, but you cannot simply reinstall the AIR package.  [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of those things that took far too long to figure out so I figure I&#8217;d save some people the headache.  Running computer janitor on Ubuntu 8.04 will remove AIR (v. 1.5 at the time of this writing) if you have it installed, but you cannot simply reinstall the AIR package.  You might get something like &#8220;Error 1&#8243; or &#8220;An error occurred while installing Adobe AIR. Installation may not be allowed by your administrator. Please contact your administrator.&#8221;</p>
<p>The reason is because computer janitor removes the adobeair package while corrupting the adobe-certs package.  Run the commands below to fix, but as a disclaimer, my ~/.appdata and ~/.adobe directories had nothing important in them.  I&#8217;m not sure if AIR holds application data in either, so you might want to back it up first.</p>
<ol>
<li>Open synaptic package manager and remove the adobeair and adobe-certs packages if they exist.</li>
<li>Delete anything to do with AIR: <code>sudo rm -rf /opt/Adobe\ AIR/ /var/opt/Adobe\ AIR/ ~/.adobe/ ~/.appdata/</code></li>
<li>At this point you can do a <code>sudo dpkg -l | grep "Adobe"</code> and if you still see the adobe-certs package, run <code>sudo dpkg --purge adobe-certs</code></li>
<li>Close your browser and <code>sudo ./AdobeAIRInstaller.bin</code> to reinstall.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/ubuntulinux/computer-janitor-corrupting-air-installs-on-ubuntu-8-04/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
