<?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>MLNTN &#187; Javascript</title>
	<atom:link href="http://mlntn.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://mlntn.com</link>
	<description>Maniacal musings of a pixel perfectionist</description>
	<lastBuildDate>Wed, 06 Jan 2010 16:36:13 +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>jQuery IP address plugin</title>
		<link>http://mlntn.com/2009/12/30/jquery-ip-address-plugin/</link>
		<comments>http://mlntn.com/2009/12/30/jquery-ip-address-plugin/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 14:31:24 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=113</guid>
		<description><![CDATA[It&#8217;s pretty tough to handle IP addresses in HTML forms. Sure, you can do have 4 separate inputs and have users tab between them.  You could setup one input and validate it on the server side.  You could even use a tool like Masked Input to force users to match a specific format.
But [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s pretty tough to handle IP addresses in HTML forms. Sure, you can do have 4 separate inputs and have users tab between them.  You could setup one input and validate it on the server side.  You could even use a tool like <a title="jQuery Masked Input plugin" href="http://digitalbush.com/projects/masked-input-plugin/" target="_blank">Masked Input</a> to force users to match a specific format.</p>
<p>But all of those solutions fall short in one way or another.<span id="more-113"></span></p>
<p>So I decided to write a jQuery plugin that would turn a plain old text input into an IP address form widget that operates like one you&#8217;d find in your operating system.</p>
<p>The plugin parses out existing text in the input (in case the user is editing an existing IP) and creates 4 new text inputs.  As the user types the IP address, the cursor jumps to the next box.  But if I left it there, it would be pretty lame, right?</p>
<p>The plugin also adds event handlers to the inputs to limit keys.  There are also event handlers that will allow right and left arrows to jump between inputs and backspace will continue across inputs as well.</p>
<p>I had to write another plugin to get the cursor handling working correctly, so it&#8217;s required for the IP address plugin.</p>
<p>As the user is typing, if all 4 IP octets have a value, the original IP address will be repopulated with the IP address from the widget.</p>
<p><a title="jQuery IP address plugin demo" href="http://mlntn.com/demos/jquery-ipaddress/">Check out the demo</a> or <a title="jQuery IP address plugin download" href="http://mlntn.com/downloads/jquery.ipaddress.zip">download the scripts and styles</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2009/12/30/jquery-ip-address-plugin/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>jQuery undo plugin</title>
		<link>http://mlntn.com/2009/03/09/javascript-jquery-undo-plugin/</link>
		<comments>http://mlntn.com/2009/03/09/javascript-jquery-undo-plugin/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 02:14:57 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery plugins]]></category>
		<category><![CDATA[undo]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=46</guid>
		<description><![CDATA[I got a crazy idea when I was working with some AJAX interfaces &#8211; a quick and painless jQuery undo plugin.
What does that mean?  I&#8217;m not entirely sure.
(Demo)
Here&#8217;s the goal&#8230;
I want to be easily able to add a function to the undo queue.  When I press the undo key combination, that function will run [...]]]></description>
			<content:encoded><![CDATA[<p>I got a crazy idea when I was working with some AJAX interfaces &#8211; a quick and painless jQuery undo plugin.</p>
<p>What does that mean?  I&#8217;m not entirely sure.<span id="more-46"></span></p>
<p>(<a href="http://design.mellentine.com/demos/jquery-undoable/">Demo</a>)</p>
<p>Here&#8217;s the goal&#8230;</p>
<p>I want to be easily able to add a function to the undo queue.  When I press the undo key combination, that function will run again and it will be placed in the redo queue:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">undoable</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;">'#sample'</span> <span style="color: #339933;">+</span> n<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</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></div></div>
<p>This is really not ideal.  While some functions toggle correctly, not all do.</p>
<p>What I really is to be able to define a function for undo and a function for redo:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">undoable</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;">'#sample'</span> <span style="color: #339933;">+</span> n<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</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: #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;">'#sample'</span> <span style="color: #339933;">+</span> n<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</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></div></div>
<p>I really think I can get away with both of them.</p>
<p><a href="http://design.mellentine.com/demos/jquery-undoable/">Check out the demo</a> and let me know what you think. (Hint: Open the Firebug console)</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2009/03/09/javascript-jquery-undo-plugin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery zoom event plugin</title>
		<link>http://mlntn.com/2008/12/11/javascript-jquery-zoom-event-plugin/</link>
		<comments>http://mlntn.com/2008/12/11/javascript-jquery-zoom-event-plugin/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 23:35:08 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery plugins]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=34</guid>
		<description><![CDATA[I don&#8217;t normally write code that I&#8217;m not ready to use, but I made an exception for this jQuery plugin.  It doesn&#8217;t really do all that I wanted it to do, but it&#8217;s good enough for a release.

Before I lose the ADD/ADHD folks: Demo.
The goal for this plugin was to create a event handler for [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t normally write code that I&#8217;m not ready to use, but I made an exception for this jQuery plugin.  It doesn&#8217;t really do all that I wanted it to do, but it&#8217;s good enough for a release.<br />
<span id="more-34"></span><br />
Before I lose the ADD/ADHD folks: <a title="jQuery zoom() event demo" href="/demos/jquery-zoom/">Demo</a>.</p>
<p>The goal for this plugin was to create a event handler for text and page zooming in a browser window.  While this can&#8217;t be accomplished entirely without some better event support in the browsers, I can watch for keyboard and mouse shortcuts that result in zooming.</p>
<p>This plugin itself doesn&#8217;t care if you zoomed in or out.  It just runs the callback function when you zoom.  Maybe at a later point, I&#8217;ll add that feature.</p>
<p>Without any further ado, here&#8217;s the plugin code:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">zoom</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keydown</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><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> jQuery.<span style="color: #660066;">browser</span>.<span style="color: #660066;">mozilla</span> <span style="color: #339933;">||</span> jQuery.<span style="color: #660066;">browser</span>.<span style="color: #660066;">msie</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">ctrlKey</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">187</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">189</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">107</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">109</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">96</span> &nbsp;<span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">48</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> jQuery.<span style="color: #660066;">browser</span>.<span style="color: #660066;">opera</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">43</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">45</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">42</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">ctrlKey</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">48</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span> fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> jQuery.<span style="color: #660066;">browser</span>.<span style="color: #660066;">safari</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">metaKey</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">charCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">43</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">charCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">45</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; jQuery<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;">'mousewheel'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">ctrlKey</span><span style="color: #009900;">&#41;</span> fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; jQuery<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;">'DOMMouseScroll'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">ctrlKey</span><span style="color: #009900;">&#41;</span> fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Because we&#8217;re dealing with browser implementations of keyboard shortcuts, I decided to use jQuery.browser (which uses navigator.useragent) to deal with the browsers individually.  Much to my surprise (and delight), Internet Explorer and Firefox handle zooming identically.</p>
<p>Lines 2 &#8211; 30 deal with zooming through keyboard shortcuts.  The character codes are as follows:<br />
<strong>42</strong>: * (numpad)<br />
<strong>43</strong>: + (numpad)<br />
<strong>45</strong>: &#8211; (numpad)<br />
<strong>48</strong>: 0 (numpad in FF and IE)<br />
<strong>96</strong>: 0<br />
<strong>107</strong>: + (numpad in FF and IE)<br />
<strong>109</strong>: &#8211; (numpad in FF and IE)<br />
<strong>187</strong>: +<br />
<strong>189</strong>: -</p>
<p>Lines 32-38 deal with scrollwheel zooming in Firefox and IE.  Opera doesn&#8217;t run the callback function for the scrollwheel when the CTRL key is pressed (which is how scrollwheel zooming is done in Opera).  Safari 2 (which is what I have) doesn&#8217;t support scrollwheel zooming at all.</p>
<p>Further testing needs to be done in Safari 3, but this plugin works pretty well as is.  Here&#8217;s how you&#8217;d use it:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">zoom</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><br />
&nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'I zoomed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Here&#8217;s the <a title="jQuery zoom() event plugin" href="/downloads/jquery.zoom.js">download</a> and the <a title="jQuery zoom() event demo" href="/demos/jquery-zoom/">demo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/12/11/javascript-jquery-zoom-event-plugin/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The start of a jQuery zoom event handler</title>
		<link>http://mlntn.com/2008/12/11/javascript-the-start-of-a-jquery-zoom-event-handler/</link>
		<comments>http://mlntn.com/2008/12/11/javascript-the-start-of-a-jquery-zoom-event-handler/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 05:40:31 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=31</guid>
		<description><![CDATA[After much research and very little code, I&#8217;ve come up with a basic start to a jQuery zoom event handler.  Don&#8217;t laugh.  This is very basic, but it handles zoom events based on keyboard shortcuts and fires the handler.

UPDATE: You can find the latest version of the plugin here.
jQuery.fn.zoom = function&#40;fn&#41; &#123;
&#160; &#160; jQuery&#40;document&#41;.keypress&#40;function&#40;e&#41;&#123;
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>After much research and very little code, I&#8217;ve come up with a basic start to a jQuery zoom event handler.  Don&#8217;t laugh.  This is very basic, but it handles zoom events based on keyboard shortcuts and fires the handler.<br />
<span id="more-31"></span></p>
<p><strong>UPDATE:</strong> You can find the latest version of the plugin <a href="http://design.mellentine.com/2008/12/11/javascript-jquery-zoom-event-plugin/">here</a>.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">zoom</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">ctrlKey</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">61</span> <span style="color: #339933;">||</span> e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">45</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>You could then assign a handler like so:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">zoom</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><br />
&nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'zoom, zoom'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>So much more needs to be added to this and I&#8217;m not even sure I&#8217;m headed the right direction.  For example, I can address another way of zooming (CTRL-scrollwheel), but I doubt I can capture use of the menu in the browser.  Also, I could probably assign different keys to zoom.  I might have to come up with a different solution entirely.</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/12/11/javascript-the-start-of-a-jquery-zoom-event-handler/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple date conversion from SQL to readable</title>
		<link>http://mlntn.com/2008/12/02/javascript-simple-date-conversion-from-sql-to-readable/</link>
		<comments>http://mlntn.com/2008/12/02/javascript-simple-date-conversion-from-sql-to-readable/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 00:10:21 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=29</guid>
		<description><![CDATA[I needed a simple date converter function from SQL dates (YYYY-MM-DD) to something readable (M/D/YYYY).  So I wrote this little function.  Maybe someone else will have some use for it.
var makeSQLDatePretty = function&#40;d&#41; &#123;
&#160; return d.replace&#40;/([0-9]{4})-0?([0-9]{1,2})-0?([0-9]{1,2})/, '$2/$3/$1'&#41;;
&#125;;
Pass in a SQL date and the function trims leading zeros and returns a reformatted date.
var sqlDate = '2008-12-02';
alert&#40;makeSQLDatePretty&#40;sqlDate&#41;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a simple date converter function from SQL dates (YYYY-MM-DD) to something readable (M/D/YYYY).  So I wrote this little function.  Maybe someone else will have some use for it.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> makeSQLDatePretty <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> d.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/([0-9]{4})-0?([0-9]{1,2})-0?([0-9]{1,2})/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'$2/$3/$1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Pass in a SQL date and the function trims leading zeros and returns a reformatted date.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> sqlDate <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2008-12-02'</span><span style="color: #339933;">;</span><br />
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>makeSQLDatePretty<span style="color: #009900;">&#40;</span>sqlDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// should alert '12/2/2008'</span></div></div>
<p>Crazy Europeans may want to change the replace regex with &#8216;$3/$2/$1&#8242;.</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/12/02/javascript-simple-date-conversion-from-sql-to-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smart, unobtrusive AJAX form submission with jQuery</title>
		<link>http://mlntn.com/2008/09/22/javascript-smart-unobtrusive-ajax-form-submission-with-jquery/</link>
		<comments>http://mlntn.com/2008/09/22/javascript-smart-unobtrusive-ajax-form-submission-with-jquery/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 23:44:22 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery plugins]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=19</guid>
		<description><![CDATA[A former coworker was telling me recently about MooTools and how the AJAX functions are so easy to use.  He sent me the following example:

function attachedToOnSubmitOfAForm&#40;&#41;&#123;
&#160; &#160; &#160; &#160; sp = $&#40;&#34;someForm&#34;&#41;;
&#160; &#160; &#160; &#160; sp.addClass&#40;'loader'&#41;;

&#160; &#160; &#160; &#160; sp.send&#40;&#123;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; onComplete: function&#40;json&#41; &#123;
&#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>A former coworker was telling me recently about MooTools and how the AJAX functions are so easy to use.  He sent me the following example:<br />
<span id="more-19"></span></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> attachedToOnSubmitOfAForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sp <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someForm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sp.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'loader'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; sp.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ....<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onFailure<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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ....<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Great concept, right?  No need to tell it where to send the data or what fields to send, since the form already contains that info.  It&#8217;s mostly unobtrusive, so any browser that doesn&#8217;t have JS enabled would still be able to submit.</p>
<p>So my immediate response was, &#8220;Very cool.  I&#8217;m sure a jQuery plugin either exists or could be created pretty easily to handle that.&#8221;</p>
<p>I built the following plugin in maybe 30 minutes, and had it tested in another 15.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">send</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submit'</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type &nbsp; &nbsp; <span style="color: #339933;">:</span> form.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'method'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">'get'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url &nbsp; &nbsp; &nbsp;<span style="color: #339933;">:</span> form.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'action'</span><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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data &nbsp; &nbsp; <span style="color: #339933;">:</span> form.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>There are some of you that are wondering, &#8220;Why is this necessary? I can write my own jQuery.ajax() code every time.&#8221;  Yep, and that&#8217;s part the problem I&#8217;m trying to solve. The plugin is a shortcut for most of the jQuery.ajax() code you&#8217;d normally write and it makes unobtrusive form submission dead simple:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jQuery<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><br />
&nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; complete <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The function takes one parameter, an object that extends the <a href="http://docs.jquery.com/Ajax/jQuery.ajax#options">jQuery.ajax method</a>.</p>
<p>Here&#8217;s a <a href="/demos/jquery-send">demo</a> and the <a href="/downloads/jquery.send.js">download</a>.</p>
<p><strong>2008-09-23</strong>: Updated <em>window.location</em> to <em>window.location.href</em> (Thanks Samori)</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/09/22/javascript-smart-unobtrusive-ajax-form-submission-with-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Iterating through objects, the easy way</title>
		<link>http://mlntn.com/2008/05/08/javascript-iterating-through-objects-the-easy-way/</link>
		<comments>http://mlntn.com/2008/05/08/javascript-iterating-through-objects-the-easy-way/#comments</comments>
		<pubDate>Thu, 08 May 2008 05:04:24 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[objects]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=18</guid>
		<description><![CDATA[Objects in any ECMAScript language (Javascript, Actionscript, Jscript, etc) are very powerful for &#8211; among other things &#8211; storing data.  However, getting some information out of these objects can be a hassle sometimes.
I ran into this issue recently and wrote some cool little handlers for objects.  The following methods can be used to [...]]]></description>
			<content:encoded><![CDATA[<p>Objects in any ECMAScript language (Javascript, Actionscript, Jscript, etc) are very powerful for &#8211; among other things &#8211; storing data.  However, getting some information out of these objects can be a hassle sometimes.</p>
<p>I ran into this issue recently and wrote some cool little handlers for objects.  The following methods can be used to iterate through an array manually.  I wrote them for Actionscript, but they certainly work for Javascript &#8211; although I&#8217;d suggest one small modification (shown later).<span id="more-18"></span></p>
<p>Let&#8217;s get to the code.</p>
<p>In each of the &#8220;for in&#8221; loops below, I make sure to exclude any prototyped methods.  If I don&#8217;t, they&#8217;ll show up in the loop &#8211; which can be pretty bad.</p>
<p>The &#8216;first&#8217; method starts looping through the array, but returns the first item in the object as soon as it gets to it.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">first</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><br />
&nbsp; &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> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The &#8216;last&#8217; method loops through the object, storing the current item in a variable.  When it finishes the loop, it returns that variable &#8211; the last item in the object.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">last</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><br />
&nbsp; &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> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> l<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The &#8216;previous&#8217; method loops through the object looking for the &#8217;subject&#8217; variable.  If it matches the variable with the current item, it returns the previous item &#8211; set after the if statement below (in the previous time through the loop).  If it never matches the variable or the previous item is never set, it will return the last item of the object.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">previous</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &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> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> subject <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> l <span style="color: #339933;">!==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> l<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The &#8216;next&#8217; method loops through the object looking for the &#8217;subject&#8217; variable, as well.  However, when it matches the variable it sets a &#8216;found&#8217; flag.  The next time through the loop, the current item will be returned.  If it never matches the variable or the matched item is the last in the object, the first item will be returned.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">next</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &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> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>So let&#8217;s do some examples.  My test object has 3 items &#8211; one of which contains an array:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; apple &nbsp;<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'fuji'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'gala'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'red delicious'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; banana <span style="color: #339933;">:</span> <span style="color: #3366CC;">'chiquita'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; orange <span style="color: #339933;">:</span> <span style="color: #3366CC;">'navel'</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
myObject.<span style="color: #660066;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 'apple'</span><br />
myObject.<span style="color: #660066;">last</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;">// returns 'orange'</span><br />
myObject.<span style="color: #660066;">previous</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 'orange'</span><br />
myObject.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// returns 'banana'</span><br />
myObject.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'banana'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// returns 'orange'</span><br />
myObject.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// returns 'apple'</span></div></div>
<p>Simple as that.</p>
<p>The one change that I would recommend looking into for Javascript is better checking for the &#8220;for in&#8221; loops.</p>
<p>Instead of:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></div>
<p>The hasOwnProperty property is recommended:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</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;">hasOwnProperty</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/05/08/javascript-iterating-through-objects-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Image onerror magic</title>
		<link>http://mlntn.com/2008/03/26/javascript-onerror-image-magic/</link>
		<comments>http://mlntn.com/2008/03/26/javascript-onerror-image-magic/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 14:44:47 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[img]]></category>

		<guid isPermaLink="false">http://mlntn.com/2008/03/26/javascript-onerror-image-magic/</guid>
		<description><![CDATA[HTML has a little-known event handler for &#60;img&#62; and &#60;body&#62; tags.  I&#8217;m going to focus on &#60;img&#62; tags and how you can use this to clean up some 404s.
First, just a quick note about this event handler.  You won&#8217;t find it in any W3C spec because it just isn&#8217;t there.  Why?  [...]]]></description>
			<content:encoded><![CDATA[<p>HTML has a little-known event handler for &lt;img&gt; and &lt;body&gt; tags.  I&#8217;m going to focus on &lt;img&gt; tags and how you can use this to clean up some 404s.<span id="more-17"></span></p>
<p>First, just a quick note about this event handler.  You won&#8217;t find it in any W3C spec because it just isn&#8217;t there.  Why?  I don&#8217;t know, but I do know that it&#8217;s supported by Firefox and Internet Explorer 6/7 &#8211; which is ~90% of the browser usage and possibly more (read: I need to fire up my Mac and test it).  Apparently, this event handler has been around for a long time &#8211; see this <a href="http://lists.w3.org/Archives/Public/www-html/2001Sep/0084.html" target="_blank">brief e-mail exchange</a> on the HTML users group.</p>
<p>Okay, let&#8217;s get started.  Recently, I was working with some external data where I didn&#8217;t know whether I would have an image with each item the data or not.  This is a simple problem that can be solved using server side code (since the data feed needed to be parsed into a database table anyway).  However, I found that the links to some of the images returned 404 errors.  I&#8217;m not a usability expert, but have broken images looks pretty bad, even if you use alt attributes.  The onerror event handler is perfect for this situation.  Here&#8217;s how it works:</p>
<p>You assign the attribute to the image tag with some sort of error handler:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;badimage.jpg&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span></div></div>
<p>Then, create your error handler function.  You could replace the image with a &#8220;No image&#8221; image (like I did):</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> fixImage<span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'noimage.gif'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Or you could just remove the image altogether:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> fixImage<span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; img.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>If you want to read more about using onerror with &lt;body&gt; tags, <a href="http://developer.mozilla.org/" target="_blank">MDC</a> has some <a href="http://developer.mozilla.org/en/docs/DOM:window.onerror" target="_blank">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/03/26/javascript-onerror-image-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Internet Explorer&#8217;s attachEvent breaks &#8216;this&#8217;</title>
		<link>http://mlntn.com/2008/02/25/javascript-internet-explorers-attachevent-breaks-this/</link>
		<comments>http://mlntn.com/2008/02/25/javascript-internet-explorers-attachevent-breaks-this/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 18:27:47 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://mlntn.com/2008/02/25/javascript-internet-explorers-attachevent-breaks-this/</guid>
		<description><![CDATA[First of all, let me just say that I love jQuery.  It makes Javascript development so much faster.  I&#8217;m sure that Prototype is just as easy for those that use that daily, but I prefer jQuery.  And before you ask&#8230; Yes, I learned both.
That having been said, there are some applications where [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, let me just say that I love <a href="http://jquery.com/" target="_blank">jQuery</a>.  It makes Javascript development so much faster.  I&#8217;m sure that <a href="http://www.prototypejs.org/" target="_blank">Prototype</a> is just as easy for those that use that daily, but I prefer jQuery.  And before you ask&#8230; Yes, I learned both.</p>
<p>That having been said, there are some applications where I cannot use a Javascript library and I need to use the (longer) Javascript methods.  There&#8217;s nothing wrong with this &#8211; in fact, I find that I always learn something new.  Such was the case today.<span id="more-13"></span></p>
<p>I decided that I would attach some events using the addEventListener function.  I had always either used the <a href="http://www.w3.org/TR/DOM-Level-2-Events/" target="_blank">older method</a> of attaching events or some helper library.  So I checked out <a href="http://developer.mozilla.org/en/docs/DOM:element.addEventListener" target="_blank">the spec</a> on <a href="http://developer.mozilla.org/" target="_blank">MDC</a> and saw that Internet Explorer had its own version of this function called <a href="http://msdn2.microsoft.com/en-us/library/ms536343.aspx" target="_blank">attachEvent</a>.  I hate how Internet Explorer does things like this.  There are standards for a reason, you know.</p>
<p>Anyway, I wrote a simple function that would apply the event, but it was not working in Internet Explorer.  So I started debugging the issue.</p>
<p>&#8220;Is the event being attached?&#8221; &#8211; Yes</p>
<p>Is the event being attached to the right object?&#8221; &#8211; Yes</p>
<p>Can I alert some text in the event function?&#8221; &#8211; Yes&#8230; odd.</p>
<p>Here&#8217;s my function:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">addEvent<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">container</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'mouseover'</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'over'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
addEvent <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #339933;">,</span> event<span style="color: #339933;">,</span> func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> func<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>el.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'on'</span><span style="color: #339933;">+</span>event<span style="color: #339933;">,</span> func<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'on'</span><span style="color: #339933;">+</span>event<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>It turns out that Internet Explorer doesn&#8217;t pass the &#8216;this&#8217; object to the event function.  &#8216;this&#8217; is this instance should be the object that had the event attached.</p>
<p>It&#8217;s no shock to people that know me that I hate Internet Explorer.  I even sell <a href="http://19352.spreadshirt.com/-/-/Shop/" target="_blank">T-shirts</a> declaring that fact.  But this is just stupid.</p>
<p>Why did Microsoft have to implement their own version of addEventListener?  Why did they have to implement it so horribly that it doesn&#8217;t return &#8216;this&#8217;?</p>
<p>I ended up writing a workaround that just used the older method of applying the event to the object.  Here&#8217;s the finished version:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">addEvent <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #339933;">,</span> event<span style="color: #339933;">,</span> func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> func<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'on'</span><span style="color: #339933;">+</span>event<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>It&#8217;s really just adding insult to injury, since the only reason I had to write this function is because Internet Explorer doesn&#8217;t handle the CSS :hover pseudo-class for DIV elements.</p>
<p>Anyone want a <a href="http://19352.spreadshirt.com/-/-/Shop/" target="_blank">T-shirt</a>?</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/02/25/javascript-internet-explorers-attachevent-breaks-this/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Method overloading</title>
		<link>http://mlntn.com/2007/12/04/javascript-method-overloading/</link>
		<comments>http://mlntn.com/2007/12/04/javascript-method-overloading/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 04:20:08 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://mlntn.com/2007/12/04/javascript-method-overloading/</guid>
		<description><![CDATA[John Resig, one of my heroes and creator of the jQuery JavaScript library, wrote a great piece about overloading methods.  He created a dead simple function that shoulders most of the work.  Check it out:
function addMethod(object, name, fn){
  var old = object[ name ];
  object[ name ] = function(){
   [...]]]></description>
			<content:encoded><![CDATA[<p><a title="John Resig" href="http://ejohn.org/" target="_blank">John Resig</a>, one of my heroes and creator of the <a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a> JavaScript library, wrote a <a title="JavaScript method overloading" href="http://ejohn.org/blog/javascript-method-overloading/" target="_blank">great piece</a> about overloading methods.  He created a dead simple function that shoulders most of the work.  Check it out:</p>
<pre>function addMethod(object, name, fn){
  var old = object[ name ];
  object[ name ] = function(){
    if ( fn.length == arguments.length )
      return fn.apply( this, arguments );
    else if ( typeof old == 'function' )
      return old.apply( this, arguments );
  };
}</pre>
<p>Here&#8217;s how you could use it to extend the User object that we <a href="http://design.mellentine.com/2007/12/01/javascript-creating-objects-and-methods/">created earlier</a>.</p>
<pre>addMethod(User.prototype, "find", function(){
  // Find all users...
});
addMethod(User.prototype, "find", function(name){
  // Find a user by name
});
addMethod(User.prototype, "find", function(first, last){
  // Find a user by first and last name
});</pre>
<p>You can read more from John&#8217;s blog <a title="John Resig blog" href="http://ejohn.org/blog/" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2007/12/04/javascript-method-overloading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
