<?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>I does Javascript!</title>
	<atom:link href="http://blog.stchur.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stchur.com</link>
	<description>If you don&#039;t expect too much from me, you might not be let down.</description>
	<lastBuildDate>Sat, 10 Nov 2012 17:55:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Dealing with CSS Vendor Prefixes</title>
		<link>http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/</link>
		<comments>http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 06:03:56 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=194</guid>
		<description><![CDATA[The IE team recently posted what they're calling a best practice for dealing with vendor prefixes in modern browsers. After reading it, I thought I would post the solution used in Gimme, which I implemented as soon as I had to start dealing with vendor prefixes. I've yanked it out and posted it here as [...]]]></description>
				<content:encoded><![CDATA[	<p>The IE team recently posted what they're calling <a href="http://blogs.msdn.com/b/ie/archive/2011/10/28/a-best-practice-for-programming-with-vendor-prefixes.aspx" title="A Best Practice for Programming with Vendor Prefixes">a best practice for dealing with vendor prefixes in modern browsers</a>.</p>
	<p>After reading it, I thought I would post the solution used in Gimme, which I implemented as soon as I had to start dealing with vendor prefixes.</p>
	<p>I've yanked it out and posted it here as a stand-alone function called getStyleName().  You can use it when you want to set some element's style in Javascript.  You always pass it the "standard" name (without any prefix) and it will do (or try to do) the right thing in the right browser.</p>
	<p>For instance:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> someElem = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'foo'</span><span class="br0">&#41;</span>;</p>
	<p><span class="co1">// MozTransform in Mozilla, webkitTransform in webkit, etc...</span><br />
someElem.<span class="me1">style</span><span class="br0">&#91;</span>getStyleName<span class="br0">&#40;</span><span class="st0">'transform'</span><span class="br0">&#41;</span><span class="br0">&#93;</span> = <span class="st0">'rotate(90deg)'</span>;</div>
	<p>Here is the getStyleName function in full:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> getStyleName = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> styleHash = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> vendorPrefixes = <span class="br0">&#91;</span> <span class="st0">'O'</span>, <span class="st0">'ms'</span>, <span class="st0">'Moz'</span>, <span class="st0">'Webkit'</span> <span class="br0">&#93;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">function</span><span class="br0">&#40;</span>styleName, elem<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;elem = elem || document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> s = styleHash<span class="br0">&#91;</span>styleName<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!s<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// hope the browser supports the &quot;official&quot; (non-vendor-prefixed) style first</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> camStyle = camelize<span class="br0">&#40;</span>styleName<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// if not, then ask getVendorStyle for help -- it will try the various vendor prefixes seeking</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// one that works (if it doesn't find one, it will return an empty string, in which case we</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// revert back to originally passed in styleName)</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>elem.<span class="me1">style</span><span class="br0">&#91;</span>camStyle<span class="br0">&#93;</span> === undefined<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;camStyle = getVendorStyle<span class="br0">&#40;</span>camStyle, elem<span class="br0">&#41;</span> || styleName;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// finally, store the camelized &amp; vendor-specific (if applicable) value in the styleHash where key = styleName that was passed in</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;s = styleHash<span class="br0">&#91;</span>styleName<span class="br0">&#93;</span> = camStyle;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> s;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> getVendorStyle<span class="br0">&#40;</span>style, elem<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> s = camelize<span class="br0">&#40;</span><span class="st0">'-'</span> + style<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> i = vendorPrefixes.<span class="me1">length</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>i--<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;style = vendorPrefixes<span class="br0">&#91;</span>i<span class="br0">&#93;</span> + s;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>elem.<span class="me1">style</span><span class="br0">&#91;</span>style<span class="br0">&#93;</span> !== undefined<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> style;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> <span class="st0">''</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> camelize<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> s.<span class="me1">replace</span><span class="br0">&#40;</span><span class="re0">/-<span class="br0">&#40;</span>\w<span class="br0">&#41;</span>/g</span>, <span class="kw2">function</span><span class="br0">&#40;</span>m, c<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> c.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>True private variables in Javascript with prototype</title>
		<link>http://blog.stchur.com/2011/09/26/true-private-variables-in-javascript-with-prototype/</link>
		<comments>http://blog.stchur.com/2011/09/26/true-private-variables-in-javascript-with-prototype/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 05:09:49 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=179</guid>
		<description><![CDATA[Most folks know how to mimic public, private, and "privileged" variables in Javascript, but I think at some point, when getting familiar with prototypal inheritance, most scripters wonder if there is a way to access the private members (the ones created in a closure) of an instance within a function defined using prototype. There is [...]]]></description>
				<content:encoded><![CDATA[	<p>Most folks know how to mimic public, private, and "privileged" variables in Javascript, but I think at some point, when getting familiar with prototypal inheritance, most scripters wonder if there is a way to access the private members (the ones created in a closure) of an instance within a function defined using prototype.</p>
	<p>There is really no good way to do this.  Here's a simple prototype example:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="kw3">name</span> = <span class="kw3">name</span>;<br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">age</span> = age;<br />
<span class="br0">&#125;</span><br />
Cat.<span class="me1">prototype</span>.<span class="me1">introduce</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'I am a cat. My name is '</span> + <span class="kw1">this</span>.<span class="kw3">name</span> + <span class="st0">', and I am '</span> + <span class="kw1">this</span>.<span class="me1">age</span> + <span class="st0">' year(s) old.'</span>;<br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
c.<span class="me1">introduce</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
	<p>The <code>introduce</code> function was defined with <code>.prototype</code> so the <code>this</code> keyword in that context refers to the Cat instance in question.  Using it, we can get at the <code>.name</code> and <code>.age</code> properties. But this also means that the <code>introduce</code> function isn't the only thing that has access to those properties.  In fact, the whole world has access.  They are, essentially, public properties.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="kw3">name</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;<span class="co1">// directly accessible</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">age</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// directly accessible</span><br />
&nbsp;</div>
	<p>What if you don't want that?  What if you want to take the name that is passed into the constructor, modify it in some way (let's say, convert it to uppercase) and then offer read only access to it?</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="kw3">name</span> = <span class="kw3">name</span>.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
Cat.<span class="me1">prototype</span>.<span class="me1">getName</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw1">this</span>.<span class="kw3">name</span>;<br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// GARFIELD</span><br />
c.<span class="kw3">name</span> = <span class="st0">'STEPHEN'</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// oops, should this be allowed?</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// STEPHEN, clearly not the kind of encapsulation we're going for</span><br />
&nbsp;</div>
	<p>So how can we allow <code>getName</code> to have access to <code>.name</code>, but at the same time disallow access to <code>.name</code> outside of prototype defined functions?  Well, as I said, you really can't.  I mean, there's no <strong>good</strong> way.  But there is a sort-of way.</p>
	<div class = "note"><strong>DISCLAIMER: I am NOT recommending that you actually do what I'm about to demonstrate.  At the same time, I am not telling you NOT to do it either.  From a performance perspective, this is completely unvalidated (at least by me).  So if you want to do something like this, best do your homework and figure out if it will offer you an acceptable level of performance -- I am not attempting to answer that for you.</strong></div>
	<p>Alright.  With the disclaimer out of the way, here we go:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> Cat = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> guid = <span class="nu0">0</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> privates = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">this</span>._$guid = guid++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> =<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw3">name</span>: <span class="kw3">name</span>,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;age: age<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getName</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="kw3">name</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getAge</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="me1">age</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> cat;<br />
<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
	<p><span class="kw2">var</span> cat1 = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> cat2 = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Fluffy'</span>, <span class="nu0">13</span><span class="br0">&#41;</span>;</p>
	<p><span class="kw3">alert</span><span class="br0">&#40;</span>cat1.<span class="me1">getName</span> === cat2.<span class="me1">getName</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// true</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat1.<span class="kw3">name</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// oops, no dice (which is good)</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat1.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Garfield</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat2.<span class="me1">age</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// oops, no dice</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat2.<span class="me1">getAge</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// 13</span><br />
&nbsp;</div>
	<p>First, I alert cat1.getName === cat2.getName to illustrate that prototype really is being used here.  The instances cat1 and cat2 share the implementation of the getName function (they don't each get their own -- something that would have been true had we used the closure model exclusively).</p>
	<p>Second, you'll notice that I do not have direct access to <code>.name</code> or <code>.age</code>.  So this seems to do what we want.</p>
	<p>Now, more alert readers will no doubt have noticed that I cheated.  I'm still using the <code>this</code> keyword from within my prototype defined functions, <code>getName</code> and <code>getAge</code>.  This is so that I can access the instances GUID (<code>._$guid</code>).  The GUID is, of course, nothing more than counter, but it serves as a way to track the created instances.  Using it, we can get access to property bag in the <code>privates</code> hash that "belongs to" the particular Cat instance we're interested in.</p>
	<p>But there is a flaw here, of course.  The <code>._$guid</code>, despite its marginally cryptic name, is completely unprotected.  That is, the whole world has access to it.  So one could really hork things badly if he decided to do something like:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> garfield = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> fluffy = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Fluffy'</span>, <span class="nu0">10</span><span class="br0">&#41;</span>;</p>
	<p>fluffy._$guid = <span class="nu0">0</span>;<br />
<span class="kw3">alert</span><span class="br0">&#40;</span>fluffy.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Garfield, oops! &nbsp;this is NOT good</span></div>
	<p>Now, in this example, I chose a valid GUID (that of Garfield), but I could have just as easily chosen an invalid GUID, which would have caused a script error. Of course, the script error is easily preventable by ensuring that the <code>._$guid</code> actually exists as a key in the <code>privates</code> hash before attempting to access it, but this does nothing to ensure the integrity of the data, and that's a problem.</p>
	<p>Turns out though, that we can add a bit more code.  A bit of code that involves a check prior to accessing data from the <code>privates</code> hash to ensure that the instance's <code>_$guid</code> hasn't been tampered with.  If it has, we throw an exception and refuse to go any further.</p>
	<p>Here's the revision:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> Cat = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> guid = <span class="nu0">0</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> privates = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> check = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">this</span>._$guid = guid++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;check<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> = <span class="kw1">this</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> =<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw3">name</span>: <span class="kw3">name</span>,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;age: age<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getName</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>check<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> !== <span class="kw1">this</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">throw</span> <span class="st0">'Oops, looks like this instances has been tampered with.'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="kw3">name</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getAge</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>check<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> !== <span class="kw1">this</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">throw</span> <span class="st0">'Oops, looks like this instances has been tampered with.'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="me1">age</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> cat;<br />
<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
	<p>
<span class="kw2">var</span> fluffy = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Fluffy'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> snowball = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Snowball'</span>, <span class="nu0">12</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> garfield = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">9</span><span class="br0">&#41;</span>;</p>
	<p><span class="kw3">alert</span><span class="br0">&#40;</span>fluffy.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Fluffy</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>snowball.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;<span class="co1">// Snowball</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>garfield.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;<span class="co1">// Garfield</span><br />
fluffy._$guid = <span class="nu0">2</span>;<br />
<span class="kw3">alert</span><span class="br0">&#40;</span>fluffy.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Throws an exception, b/c fluffy's _$guid was modified</span></div>
	<p>I even created a nice illustration to help you visualize this <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
	<p><a href="http://blog.stchur.com/wp-content/uploads/2011/09/cats.jpg"><img src="http://blog.stchur.com/wp-content/uploads/2011/09/cats-300x225.jpg" alt="Visualization of private variables using prototype in Javascript" title="Visualization of private variables using prototype in Javascript" width="300" height="225" class="alignnone size-medium wp-image-187" /></a></p>
	<p>As you can plainly see, the keys in the <code>privates</code> hash correspond to the <code>._$guid</code> property of Cat instance.  In other words, if you match up an instance's <code>._$guid</code> with a key in the <code>privates</code> hash, you'll have access to that instance's collection of member variables.</p>
	<p>You'll also notice that the keys in the <code>check</code> hash match the keys in the <code>privates</code> hash.  However, they don't point to a property bag, they point to an actual Cat instance.  In the <code>getName</code> function, we have access to the instance (through the use of the <code>this</code> keyword), and we can use that to perform a check.  If an instance's public <code>._$guid</code> has been tampered with, then looking in the <code>check</code> hash for that <code>$_guid</code> will result in accessing a Cat instance that isn't equal (identical) to <code>this</code>.  In that case, we can throw an exception.  Otherwise, we go ahead and access and entry from the <code>privates</code> hash, and this gives us access to the current Cat's members.</p>
	<p>Before closing, I want to once again make clear that I am most definitely NOT recommending that you use this model, but neither am I discouraging the use of it (I'm a little bit annoying that way, I know).  It's simply untested in terms of memory and speed performance.  Whether or not this yields anything that would constitute acceptable performance, I do not know and have not attempted to determine.  I imagine there could be some scenarios in which it is probably okay.  And I imagine there are other scenarios in which it might be a nightmare, but I'm only guessing on both counts.</p>
	<p>If you decide to experiment with this, definitely report back here with your findings for all to see.</p>
	<p>Happy scripting!
</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/09/26/true-private-variables-in-javascript-with-prototype/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Dependency Injection / Dependency Management (part 1)</title>
		<link>http://blog.stchur.com/2011/08/05/dependency-injection-dependency-management-part-1/</link>
		<comments>http://blog.stchur.com/2011/08/05/dependency-injection-dependency-management-part-1/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 01:19:31 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=151</guid>
		<description><![CDATA[I'm not an expert when it comes to dependency injection, but I do know a little. My team uses Unity in one of its projects, so I have some exposure. Recently, I started work on a project (hoping to eventually release it as OSS) on a Dependency Injection (like) / Dependency Management framework for the [...]]]></description>
				<content:encoded><![CDATA[	<p>I'm not an expert when it comes to dependency injection, but I do know a little.  My team uses <a href = "http://unity.codeplex.com/" title = "Unity">Unity</a> in one of its projects, so I have some exposure.</p>
	<p>Recently, I started work on a project (hoping to eventually release it as OSS) on a Dependency Injection (like) / Dependency Management framework for the client (that is, written in Javascript).</p>
	<p>Since it was (is) a fun project for me, I thought I might blog about some of the more interesting parts of what I've done.</p>
	<p>Dependency Resolution:<br />
One of the key parts of Dependency Injection is dependency resolution.  That is, if I need an instance of something, say, a Foo, and if creating a Foo requires an instance of say, a Bar, then my DI framework ought to resolve the Bar for me on its way towards creating my Foo.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> registration =<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;Foo: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Foo'</span>, params: <span class="br0">&#91;</span> <span class="st0">'Bar'</span> <span class="br0">&#93;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp;Bar: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Bar'</span> <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
	<p><span class="co1">// obviously, code for Container, not show here (yet)</span><br />
<span class="kw2">var</span> c = <span class="kw2">new</span> Container<span class="br0">&#40;</span>registration<span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> f = c.<span class="me1">instance</span><span class="br0">&#40;</span><span class="st0">'Foo'</span><span class="br0">&#41;</span>;&nbsp;<span class="co1">// container &quot;knows&quot; Foo needs Bar; it takes care of it for us</span></div>
	<p>So what I'm focusing on in this blog post is the comment above: "container 'knows' Foo need Bar; it takes care of it for us.</p>
	<p>In a future post, I'll be talking more about the management part (involving not only resolving types on demand, but also downloading dependencies and groups of dependencies prior to resolution).</p>
	<p>But for now, let's just assume that all dependencies needed to create anything/everything have already been preloaded.</p>
	<p>So how does the container know that Foo needs a Bar?  Well, it's not too hard to figure out if you look at the registration.  It's just a dictionary of key/value pairs, where the value is a simple object literal containing a couple properties:</p>
	<ol>
	<li>className - required, the fully qualified name of the thing that the container will eventually be creating instances of</li>
	<li>params - optional, an array of string keys that map to other values in the registration dictionary</li>
	</ol>
	<p>Container knows that Foo needs a Bar because that was explicitly spelled out in the registration when we defined:</p>
	<div class="dean_ch" style="white-space: nowrap;">Foo: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Foo'</span>, params: <span class="br0">&#91;</span> <span class="st0">'Bar'</span> <span class="br0">&#93;</span> <span class="br0">&#125;</span></div>
	<p>The params array tells the container everything it needs to know.  So what does the code that actually implements this type resolution look like? Glad you asked.  Code below, explanation to follow:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Container<span class="br0">&#40;</span>reg<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">function</span> instance<span class="br0">&#40;</span>factoryName<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> params = reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">params</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> i = params.<span class="me1">length</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>i--<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> f = reg<span class="br0">&#91;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>f<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = instance<span class="br0">&#40;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> create<span class="br0">&#40;</span>reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">className</span>, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">instance</span> = instance;</p>
	<p>&nbsp; &nbsp; <span class="kw2">function</span> create<span class="br0">&#40;</span>type, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> inst = <span class="kw2">new</span> constructor<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; inst.<span class="me1">init</span> &amp;&amp; inst.<span class="me1">init</span>.<span class="me1">apply</span><span class="br0">&#40;</span>inst, params<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> inst;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Container<span class="br0">&#40;</span>registration<span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> f = c.<span class="me1">instance</span><span class="br0">&#40;</span><span class="st0">'Foo'</span><span class="br0">&#41;</span>;</div>
	<p>Now this is not actually the code used in the project I'm working on.  It's actually rather different, but this will suffice for now.  One thing to note is that I'm really not performing any sort of safety checks here.  If you ask the container for an instance of something that hasn't been registered, it will not fail gracefully (and it really should).  But that's an easy change and not really the focus here.</p>
	<p>There isn't a ton code that requires explanation here, except for a couple pieces.  The first piece that I suspect will raise some eyebrows is:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
	<p>Now let me say right off the bat that I'm not claiming this is the best way to do it (certainly it's not the only way).  Essentially, this is just a confusing way of doing:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = <span class="kw1">eval</span><span class="br0">&#40;</span>type<span class="br0">&#41;</span>;</div>
	<p>So why not just do that?  Well, I'm not saying you can't, but a lot of people have a hard-line stance against using eval.  Also, in some browsers, eval was <strong>much</strong> slower than using new Function(..).  However, in other browsers, eval was faster (sometime nominally, sometimes significantly).</p>
	<p>Finally, there is another option in this case: you can split the type string on the dot, and actually attempt to access the parts as properties.  It would ultimately translate to something like:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = window<span class="br0">&#91;</span><span class="st0">'Stephen'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">'Stchur'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">'Foo];</span></div>
	<p>Of course, you wouldn't hard code it like that, you'd split the string and loop, but I trust you get the idea.  For simple cases, this actually turns out to be quite fast, so it's something to consider.  But also worth considering is that fact that, for small numbers of repeated executions, it matters very little which method you choose.  And if you cache the resolved constructor once you get it, it matters even less.</p>
	<p>Anyway, I used new Function(..) in this post, because I think it's an interesting way to accomplish the task (not necessarily the best, but chances are you haven't seen it before, so you learned something, and that's what this blog is all about -- spreading knowledge and learning).</p>
	<p>If you're unfamiliar with Function (capital F) in Javascript, allow me to explain.</p>
	<p>Function in Javascript is a constructor that takes a string and returns you a function whose logic is whatever string-code you passed into it.  For example:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> sayHello = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'alert(&quot;hello there&quot;);'</span><span class="br0">&#41;</span>;<br />
sayHello<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; <span class="co1">// alerts &quot;hello there&quot;</span></div>
	<p>It's not too hard then to see how we've leveraged Function in our case.  We created a function whose job it is to return a reference to function named, type.  That is, if type = "Foo" then this code:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span></div>
	<p>Will result in the variable <code>constructor</code>, pointing to the function, <code>Foo</code> (so it essentially <em>is</em> Foo).  Another way of thinking about it is that the implementation of the function we've create is:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">return</span> Foo;</div>
	<p>And when you execute that function, what you get back is a reference to the function Foo, which is the constructor you care about.</p>
	<p>Great! Now what?  Well, now we should invoke the constructor.  Unfortunately, things fall a part a little bit here (but only a little).  There isn't really a great way to invoke a constructor with the <code>new</code> keyword and at the same time pass in an array of params to be used as the individual arguments to that constructor.  More clever Javascript Ninjas will give you creative solutions.  You can create an empty object, and then call <code>.apply</code> on the constructor, passing in the object you created as the <code>this</code> object and the items in whatever array you pass in will becomes the individual arguments to the constructor.  This however, won't result in an object that actually <em>is</em> an instance of your constructor.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">description</span> = <span class="st0">'furry'</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="kw3">name</span> = <span class="kw3">name</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">age</span> = age;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">var</span> c = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
Cat.<span class="me1">apply</span><span class="br0">&#40;</span>c, <span class="br0">&#91;</span> <span class="st0">'Garfield'</span>, <span class="nu0">10</span> <span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
c <span class="kw1">instanceof</span> Cat; <span class="co1">// false <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' />  </span></div>
	<p>You can carry this further though.  You can set the native prototype of your <code>c</code> object to be Cat's prototype:</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="kw2">var</span> c = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
c.__proto__ = Cat.<span class="me1">prototype</span>;<br />
c <span class="kw1">instanceof</span> Cat; <span class="co1">// true <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </span></div>
	<p>However, support for <code>__proto__</code> is not so good in several older versions of a certain, still-fairly-popular-but-I-have-no-idea-why browser (I'm a Firefox guy myself).</p>
	<p>It's worth pointing out here that your temptation to try <code>c.prototype = Cat.prototype</code> will not work, but I'm not going to get into why.  This isn't a post about prototype in Javascript.</p>
	<p>If you care to, you could iterate Cat's prototype and manually copy over all the properties.  Then your object would satisfy as a Cat if you're okay with duck typing.  But it still wouldn't actually <em>be</em> a Cat.</p>
	<p>Well, if you're only targeting browsers that support <code>__proto__</code>, then that's an option.  If you don't care about your object actually <em>being</em> a true instance of the thing you're creating, then you can use <code>.apply</code> and copy over the prototype.  Or, if you have some magic pixie dust that I don't have, then I suppose that's an option too (please share).</p>
	<p>Short of any of those things, your other option is to mandate some sort of a convention.  Two conventions come to mind that are simply and work well.</p>
	<ol>
	<li>Mandate that all constructors be able to successfully execute, parameterless. And then automatically call (if it exists) some special initialization function (I like <code>.init</code>) that take all the params that really matter.</li>
	<li>Mandate that all constructors take only a single param - an array of values needed to construct the instance.</li>
	</ol>
	<p>I chose option 1 for the sample code in this post.  I like it better than option 2 because I have a Chocolate Lab named Guinness (that didn't make any sense).</p>
	<p>Moving on...</p>
	<p>With the instantiation out of the way, the only remaining bit of code might be the resolution of the params.</p>
	<p>First we grab a reference to the array of params, as defined by this factory's definition in the registration:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> params = reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">params</span>;</div>
	<p>We then loop through it (backwards, because that's how I roll), and pull out each value, assigning it to the variable, <code>f</code>.  We assume that f points to a valid, registered, factory.  If not, we leave the value alone (this isn't particular useful right now, but in a future blog post, you'll see the code reworked a bit so that the <code>.instance</code> function can accept arbitrary values to be used as other arguments in constructing your object).</p>
	<p>Now if <code>f</code> <em>does</em> point to a valid, registered factory, then we go ahead and resolve it.  Resursively.  By calling <code>.instanace</code> again.  This is important, in case of the params, are factories that take other params that are factories.</p>
	<p>Once we've resolved a param though, we can actually just have the result "take over" what was in the original params array at the index originally.</p>
	<div class="dean_ch" style="white-space: nowrap;">params<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = instance<span class="br0">&#40;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
	<p>In this way, the right value will be in the right place in the params array when it finally gets plumbed through to the call to <code>inst.init.apply(inst, params)</code></p>
	<p>Makes perfect sense right?</p>
	<p>Honestly, I think it's harder to speak/write about than it is to just understand.  It's not super complicated.  It will get more complicated though.  But that's for a future blog post.  In the mean time, here's a full, copy/pastable bit of code that actually works.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> Stephen =<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Stchur:<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Foo: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">desc</span> = <span class="st0">'foo!'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; Bar: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">desc</span> = <span class="st0">'bar!'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">init</span> = <span class="kw2">function</span><span class="br0">&#40;</span>f<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">this</span>.<span class="me1">myFoo</span> = f; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; Baz: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">desc</span> = <span class="st0">'baz!'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">init</span> = <span class="kw2">function</span><span class="br0">&#40;</span>f, b<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">myFoo</span> = f;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">myBar</span> = b;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">var</span> registration =<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Foo: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Foo'</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; Bar: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Bar'</span>, params: <span class="br0">&#91;</span><span class="st0">'Foo'</span><span class="br0">&#93;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; Baz: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Baz'</span>, params: <span class="br0">&#91;</span><span class="st0">'Foo'</span>,<span class="st0">'Bar'</span><span class="br0">&#93;</span> <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">function</span> Container<span class="br0">&#40;</span>reg<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">function</span> instance<span class="br0">&#40;</span>factoryName<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> params = reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">params</span> || <span class="br0">&#91;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> i = params.<span class="me1">length</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>i--<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> f = reg<span class="br0">&#91;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>f<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = instance<span class="br0">&#40;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> create<span class="br0">&#40;</span>reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">className</span>, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">instance</span> = instance;</p>
	<p>&nbsp; &nbsp; <span class="kw2">function</span> create<span class="br0">&#40;</span>type, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> inst = <span class="kw2">new</span> constructor<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; inst.<span class="me1">init</span> &amp;&amp; inst.<span class="me1">init</span>.<span class="me1">apply</span><span class="br0">&#40;</span>inst, params<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> inst;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Container<span class="br0">&#40;</span>registration<span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> f = c.<span class="me1">instance</span><span class="br0">&#40;</span><span class="st0">'Baz'</span><span class="br0">&#41;</span>;</div>
	<p>Look for part 2 coming soon.</p>
	<p>Happy scripting!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/08/05/dependency-injection-dependency-management-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Quiz</title>
		<link>http://blog.stchur.com/2011/06/10/javascript-quiz/</link>
		<comments>http://blog.stchur.com/2011/06/10/javascript-quiz/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 18:45:25 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=146</guid>
		<description><![CDATA[Inspired by some Javascript questions I was asked recently by a few co-workers, I decided to put together what started out a little reference/cheat-sheet, but which ultimately took the form of a Javascript Quiz. It's meant to be fun and informative. You keep track of your own answers (if you want to); no one ever [...]]]></description>
				<content:encoded><![CDATA[	<p>Inspired by some Javascript questions I was asked recently by a few co-workers, I decided to put together what started out a little reference/cheat-sheet, but which ultimately took the form of a Javascript Quiz.</p>
	<p>It's meant to be fun and informative.  You keep track of your own answers (if you want to); no one ever has to know what you got right or wrong.</p>
	<p>I should mentioned that I did NOT test this in all browsers.  I'm a Firefox guy, so that's where I do most of my testing and development for little one-off personal projects like this.  I did take a quick pass with IE8 and Chrome, but I did NOT test with IE6 or IE7 or Safari.  If something doesn't work in one of those browsers, feel free to let me know, but bear in mind that there is a strong possibility that I will not care in the least <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
	<p>And of course, there's always multiple ways to solve a problem.  I do not claim any sort of authority with any of the answers I've provided.  If you dislike an answer of feel you have a better one, good for you <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
	<p>Anyway, here it is: <a href="http://blog.stchur.com/blogcode/jsquiz">http://blog.stchur.com/blogcode/jsquiz</a></p>
	<p>Enjoy!
</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/06/10/javascript-quiz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programmatically clicking a link in Javascript</title>
		<link>http://blog.stchur.com/2010/01/15/programmatically-clicking-a-link-in-javascript/</link>
		<comments>http://blog.stchur.com/2010/01/15/programmatically-clicking-a-link-in-javascript/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 01:09:03 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Advanced Javascript]]></category>
		<category><![CDATA[Cross-Browser]]></category>
		<category><![CDATA[Useful Functions]]></category>
		<category><![CDATA[actuate]]></category>
		<category><![CDATA[click]]></category>
		<category><![CDATA[dispatch]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[prevent default action]]></category>
		<category><![CDATA[programmatically clicking]]></category>
		<category><![CDATA[simulate]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=129</guid>
		<description><![CDATA[More accurately, this would be called actuating a link (i.e. the process of setting the link's behavior into motion, if you will.) In other words, you have a link: &#60;a id = 'bingLink' href = &#34;http://www.bing.com&#34;&#62;Bing&#60;/a&#62; And you want to programmatically make that link do its thing. This usually means having the browser navigate to [...]]]></description>
				<content:encoded><![CDATA[	<p>More accurately, this would be called actuating a link (i.e. the process of setting the link's behavior into motion, if you will.)  In other words, you have a link:</p>
	<div class="dean_ch" style="white-space: nowrap;">&lt;a id = 'bingLink' href = &quot;http://www.bing.com&quot;&gt;Bing&lt;/a&gt;</div>
	<p>And you want to programmatically make that link do its thing.  This usually means having the browser navigate to the URL specified link's href attribute (though, it can also mean executing some click handler if one happens to be specified on said link.)</p>
	<p>To be honest, I don't think there are a ton of real-world uses for this, but there are a few.  And depending on your needs, it may not be trivial to accomplish this in a cross-browser fashion.  For example:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> myLink = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'myLink'</span><span class="br0">&#41;</span>;<br />
myLink.<span class="me1">click</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
	<p>The above will do the trick in IE, because IE supports <code>.click()</code> on links.  Most other browsers don't though, so that's not an all-encompassing solution.</p>
	<p>One (partial) solution in non-IE browsers might be to read the <code>href</code> attribute from a given link and then use Javascript to make the browser navigate to to the URL specified by <code>href</code> that way:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> actuateLink<span class="br0">&#40;</span>link<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;window.<span class="me1">location</span>.<span class="me1">href</span> = link.<span class="me1">href</span>;<br />
<span class="br0">&#125;</span></div>
	<p>This is not a complete solution for a couple of reasons:</p>
	<ol>
	<li>Navigating by setting <code>window.location.href</code> blows away the <code>document.referrer</code> property.</li>
	<li>Any click event handler wired to the link will not execute.</li>
	</ol>
	<p>For anyone who has read my post about <a href = "http://blog.stchur.com/2007/11/16/re-routing-events-in-javscript/" title = "Re-routing events in Javascript">Re-routing events in Javascript</a> post, you'll know that it is possible to simulate an event though the use of <code>.dispatchEvent(..)</code>.</p>
	<p>Here's a brief refresher of the part most relevant to this discussion:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> myEvt = document.<span class="me1">createEvent</span><span class="br0">&#40;</span><span class="st0">'MouseEvents'</span><span class="br0">&#41;</span>;<br />
myEvt.<span class="me1">initEvent</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp;<span class="st0">'click'</span> &nbsp; &nbsp; &nbsp;<span class="co1">// event type</span><br />
&nbsp; &nbsp;,<span class="kw2">true</span> &nbsp; &nbsp; &nbsp;<span class="co1">// can bubble?</span><br />
&nbsp; &nbsp;,<span class="kw2">true</span> &nbsp; &nbsp; &nbsp;<span class="co1">// cancelable?</span><br />
<span class="br0">&#41;</span>;<br />
document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'myLink'</span><span class="br0">&#41;</span>.<span class="me1">dispatchEvent</span><span class="br0">&#40;</span>myEvt<span class="br0">&#41;</span>;</div>
	<p>This approach will ensure that any click event handlers will fire as desired (though, it's worth nothing that you'd have to handle <code>mousedown, mouseup, keydown, keypress</code> etc... separately,) but it does NOT actuate the link!</p>
	<p>This might surprise you.  Dispatching a click event to a link doesn't actually cause the browser to navigate to the URL specified by that link's href property.  This actually isn't shocking to me, personally.  A little surprising, but not shocking.</p>
	<p>Anyway, one option is to dispatch the click event and then use <code>window.location.href</code>.  This should achieve (some of) the desired effect, but again, the <code>document.referrer</code> will not be preserved if you do this.  Also, if the event handler <a href = "http://blog.stchur.com/2006/06/23/preventing-the-default-action/" title = "Preventing the default action">prevents the default action</a>, this method will not honor the code that prevents the default action (i.e. the link will navigate despite the code that attempts to prevent it from doing so.)</p>
	<p>You can work around the <code>window.location.href</code> problem by creating a &lt;form&gt; element, settings its <code>action</code> attribute to that of the link's href attribute and then submitting the form through <code>myForm.submit()</code>, but the problem regarding preventing the default action remains.</p>
	<h2>A 95% Solution</h2>
	<p>Is there a 100% perfect solution to this problem?  Probably, but I'm not 100% perfect.  I'm only 95% perfect, so I give you my 95% solution:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> actuateLink<span class="br0">&#40;</span>link<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> allowDefaultAction = <span class="kw2">true</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>link.<span class="me1">click</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;link.<span class="me1">click</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>document.<span class="me1">createEvent</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> e = document.<span class="me1">createEvent</span><span class="br0">&#40;</span><span class="st0">'MouseEvents'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">initEvent</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; <span class="st0">'click'</span> &nbsp; &nbsp; <span class="co1">// event type</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; ,<span class="kw2">true</span> &nbsp; &nbsp; &nbsp;<span class="co1">// can bubble?</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; ,<span class="kw2">true</span> &nbsp; &nbsp; &nbsp;<span class="co1">// cancelable?</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;allowDefaultAction = link.<span class="me1">dispatchEvent</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>allowDefaultAction<span class="br0">&#41;</span>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> f = document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'form'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;f.<span class="me1">action</span> = link.<span class="me1">href</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;document.<span class="me1">body</span>.<span class="me1">appendChild</span><span class="br0">&#40;</span>f<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;f.<span class="me1">submit</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
	<p>To use it:</p>
	<div class="dean_ch" style="white-space: nowrap;">actuateLink<span class="br0">&#40;</span>document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'bingLink'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
	<p>This code is self explanatory, right?</p>
	<p>If <code>.click()</code> is available, let's use that.  If not, let's try to do a W3C-style create and dispatch event.  Then, in order to maintain the <code>document.referrer</code> property, we'll use a form submission, but we only want to submit that form if the event handler (if there is one) for the <code>click</code> event doesn't explicitly prevent the default action.</p>
	<p>This isn't a 100% perfect solution because, actuating a link in this manner is only going to invoke click handlers and navigate to the URL of the link's href attribute (if appropriate to do so.)  It isn't going to invoke handlers for any other types of events.  And I'm not about to create and dispatch every possible kind of event.  Besides, I think we can all agree that <code>click</code> is by far the most common scenario.</p>
	<p>So there ya go.  Programmatically clicking (or more accurately, actuating) a link with Javascript.  Enjoy!</p>
	<p>(Tested: Firefox 3.5.7, IE8, Opera 10.01, Safari/Win 4.0.3, Chrome 3)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2010/01/15/programmatically-clicking-a-link-in-javascript/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Fetch (a stand-alone CSS querying engine)</title>
		<link>http://blog.stchur.com/2009/03/06/fetch-a-stand-alone-css-querying-engine/</link>
		<comments>http://blog.stchur.com/2009/03/06/fetch-a-stand-alone-css-querying-engine/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 06:00:16 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Advanced Javascript]]></category>
		<category><![CDATA[Cross-Browser]]></category>
		<category><![CDATA[CSS Related]]></category>
		<category><![CDATA[Fetch]]></category>
		<category><![CDATA[Gimme]]></category>
		<category><![CDATA[CSS Querying]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=124</guid>
		<description><![CDATA[I've started work on a new side-project called Fetch. It's a stand-alone CSS querying engine, designed to be super lightweight and offer high performance across all modern browsers. It's still very much in development and the source is a bit sloppy, but it currently weighs in at only 2.5k compressed and gzipped and (in Firefox3) [...]]]></description>
				<content:encoded><![CDATA[	<p>I've started work on a new side-project called Fetch.  It's a stand-alone CSS querying engine, designed to be super lightweight and offer high performance across all modern browsers.</p>
	<p>It's still very much in development and the source is a bit sloppy, but it currently weighs in at only 2.5k compressed and gzipped and (in Firefox3) either outperforms or is on par with all other major Javascript libraries out there*.</p>
	<p>It's definitely not ready for production use, but when it is, it will become the new CSS querying engine for Gimme and will also be available as a stand-alone library, which will be extensible (so you can add your own rules) and which will also be guaranteed to play nicely with other libraries by not adding to the prototype of any native objects and only introducing one single global variable (fetch).</p>
	<p>For anyone interested, the current (i.e. sloppy) source is available on CodePlex: <a href = "http://gimme.codeplex.com/SourceControl/changeset/view/32017#399762">fetch.js</a>.</p>
	<p>As I said, it's not ready for prime time and the code is sloppy (and probably buggy).  Consider yourself warned.</p>
	<p>* <small>Fetch was not compared with libraries that use XPath or querySelectorAll, because it uses neither, but rather is a pure Javascript implementation.  Upon release, it will take advantage of querySelectorAll where appropriate, but will not use XPath.</small>
</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2009/03/06/fetch-a-stand-alone-css-querying-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My New Blog</title>
		<link>http://blog.stchur.com/2008/11/07/my-new-blog/</link>
		<comments>http://blog.stchur.com/2008/11/07/my-new-blog/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 20:12:47 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/11/07/my-new-blog/</guid>
		<description><![CDATA[This blog is mostly technical. I do, from time to time, post about non-technical things, but I try to keep this blog more or less about programming, the web, and javascript. But I'm a loud-mouth and I have lots to say. So rather than clutter up this blog with "too much" incoherent babble, I decided [...]]]></description>
				<content:encoded><![CDATA[	<p>This blog is mostly technical.  I do, from time to time, post about non-technical things, but I try to keep this blog more or less about programming, the web, and javascript.</p>
	<p>But I'm a loud-mouth and I have lots to say.  So rather than clutter up this blog with "too much" incoherent babble, I decided to start another.</p>
	<p>My new blog is called Stchur on Whatever (<a href = "http://whatever.stchur.com" title = "Stchur on Whatever">http://whatever.stchur.com</a>) and on it, I'll write about whatever the heck I feel like.</p>
	<p>So please check it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/11/07/my-new-blog/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hello Old Friend</title>
		<link>http://blog.stchur.com/2008/10/10/hello-old-friend/</link>
		<comments>http://blog.stchur.com/2008/10/10/hello-old-friend/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 12:59:13 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Beating IE into submission]]></category>
		<category><![CDATA[CSS Related]]></category>
		<category><![CDATA[MiniPosts]]></category>
		<category><![CDATA[Web-related]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[font-size]]></category>
		<category><![CDATA[IE6]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/10/10/hello-old-friend/</guid>
		<description><![CDATA[More evidence that I was right when I wrote this post. Won't be long now IE6...]]></description>
				<content:encoded><![CDATA[<p>More <a href = "http://orderedlist.com/articles/hello-old-friend" title = "font-size with pixels no longer considered harmful">evidence</a> that I was right when I wrote <a href = "http://blog.stchur.com/2008/01/15/ie5-is-dead-and-ie6-is-on-death-row/" title = "IE5 is Dead (and IE6 is on Death Row)">this post</a>.</p>

<p>Won't be long now IE6...</p>]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/10/10/hello-old-friend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Ultimate addEvent(..) function</title>
		<link>http://blog.stchur.com/2008/09/25/the-ultimate-addevent-function/</link>
		<comments>http://blog.stchur.com/2008/09/25/the-ultimate-addevent-function/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 18:32:17 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Advanced Javascript]]></category>
		<category><![CDATA[Beating IE into submission]]></category>
		<category><![CDATA[Cross-Browser]]></category>
		<category><![CDATA[Firefox Related]]></category>
		<category><![CDATA[Useful Functions]]></category>
		<category><![CDATA[Gimme]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Ultimate addEvent]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/09/25/the-ultimate-addevent-function/</guid>
		<description><![CDATA[Two articles in particular that I've written on this blog have garnered a lot of positive feedback. Fixing IE's attachEvent Failures mouseenter and mouseleave Events for Firefox (and other Non-IE Browsers) As time has gone on, I've seen mention of these articles pop up in programming forums here and there, and they still receive comments [...]]]></description>
				<content:encoded><![CDATA[	<p>Two articles in particular that I've written on this blog have garnered a lot of positive feedback.</p>
	<ol>
	<li><a href = "http://blog.stchur.com/2006/10/12/fixing-ies-attachevent-failures/" title = "Fixing IE's attachEvent Failures">Fixing IE's attachEvent Failures</a></li>
	<li><a href = "http://blog.stchur.com/2007/03/15/mouseenter-and-mouseleave-events-for-firefox-and-other-non-ie-browsers/" title = "mouseenter and mouseleave Events for Firefox (and other Non-IE Browsers)">mouseenter and mouseleave Events for Firefox (and other Non-IE Browsers)</a></li>
	</ol>
	<p>As time has gone on, I've seen mention of these articles pop up in programming forums here and there, and they still receive comments from time to time, which lets me know that people are still getting something out of them.</p>
	<p>Unfortunately, the code in them is a tad dated.  I've learned more and improved my code since I originally wrote them, and while these improvements have made their way into my <a href = "http://gimme.stchur.com" title = "The Gimme Javascript Library">Javascript Library</a>, I know that the majority of people are just grabbing the code directly from the blog entries.</p>
	<p>So I've decided to post an update: a sort of fusion between these two blog entries which offers functionality for addressing all of the issues discussed in the aforementioned entries (and then some).</p>
	<p>I consider this new function the "Ultimate addEvent Function" because I really believe it addresses the large majority of cross-browser issues that people face when dealing with events in Javascript.  And furthermore, this function goes beyond anything that any other Javascript library provides (at least as far as I know).</p>
	<p>So without further ado, I offer to you:</p>
	<h3>sstchur's Ultimate addEvent(..) function!</h3></p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> xb = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> GUIDCounter = <span class="nu0">0</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> evtHash = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> pseudoEvents =<br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="st0">'mouseenter'</span>: <span class="kw2">function</span><span class="br0">&#40;</span>_fn, _useCapture, _listening<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> f = mouseEnter<span class="br0">&#40;</span>_fn<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_listening ?<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;xb.<span class="me1">addEvent</span><span class="br0">&#40;</span><span class="kw1">this</span>, <span class="st0">'mouseover'</span>, f, _useCapture, <span class="kw2">false</span><span class="br0">&#41;</span> :<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;xb.<span class="me1">removeEvent</span><span class="br0">&#40;</span><span class="kw1">this</span>, <span class="st0">'mouseover'</span>, f, _useCapture, <span class="kw2">false</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;f = <span class="kw2">null</span>;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>,<br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="st0">'mouseleave'</span>: <span class="kw2">function</span><span class="br0">&#40;</span>_fn, _useCapture, _listening<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> f = mouseEnter<span class="br0">&#40;</span>_fn<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_listening ?<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;xb.<span class="me1">addEvent</span><span class="br0">&#40;</span><span class="kw1">this</span>, <span class="st0">'mouseout'</span>, f, _useCapture, <span class="kw2">false</span><span class="br0">&#41;</span> :<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;xb.<span class="me1">removeEvent</span><span class="br0">&#40;</span><span class="kw1">this</span>, <span class="st0">'mouseout'</span>, f, _useCapture, <span class="kw2">false</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;f = <span class="kw2">null</span>;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;xb.<span class="me1">Helper</span> =<br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;getObjectGUID: getObjectGUID,<br />
&nbsp; &nbsp;&nbsp; &nbsp;storeHandler: storeHandler,<br />
&nbsp; &nbsp;&nbsp; &nbsp;retrieveHandler: retrieveHandler,<br />
&nbsp; &nbsp;&nbsp; &nbsp;isAnAncestorOf: isAnAncestorOf,<br />
&nbsp; &nbsp;&nbsp; &nbsp;mouseEnter: mouseEnter<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;&nbsp;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;xb.<span class="me1">addEvent</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> document.<span class="me1">addEventListener</span> !== <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> w3c_addEvent;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> document.<span class="me1">attachEvent</span> !== <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> ie_addEvent;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// no modern event support I guess <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// (you could use DOM 0 here if you really wanted to)</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">function</span> w3c_addEvent<span class="br0">&#40;</span>_elem, _evtName, _fn, _useCapture, _directCall<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> eventFn = pseudoEvents<span class="br0">&#91;</span>_evtName<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> eventFn === <span class="st0">'function'</span> &amp;&amp; _directCall !== <span class="kw2">false</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;eventFn.<span class="me1">call</span><span class="br0">&#40;</span>_elem, _fn, _useCapture, <span class="kw2">true</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_elem.<span class="me1">addEventListener</span><span class="br0">&#40;</span>_evtName, _fn, _useCapture<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">function</span> ie_addEvent<span class="br0">&#40;</span>_elem, _evtName, _fn, _useCapture, _directCall<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> eventFn = pseudoEvents<span class="br0">&#91;</span>_evtName<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> eventFn === <span class="st0">'function'</span> &amp;&amp; _directCall !== <span class="kw2">false</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;eventFn.<span class="me1">call</span><span class="br0">&#40;</span>_elem, _fn, _useCapture, <span class="kw2">true</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// create a key to identify this element/event/function combination</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> key = generateHandlerKey<span class="br0">&#40;</span>_elem, _evtName, _fn<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// if this element/event/combo has already been wired up, just return</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> f = evtHash<span class="br0">&#91;</span>key<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> f !== <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// create a helper function to fix IE's lack of standards support</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;f = <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// map .target to .srcElement</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">target</span> = e.<span class="me1">srcElement</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// map .relatedTarget to either .toElement or .fromElement</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>_evtName == <span class="st0">'mouseover'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> e.<span class="me1">relatedTarget</span> = e.<span class="me1">fromElement</span>; <span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>_evtName == <span class="st0">'mouseout'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> e.<span class="me1">relatedTarget</span> = e.<span class="me1">toElement</span>; <span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">preventDefault</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> e.<span class="me1">returnValue</span> = <span class="kw2">false</span>; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">stopPropagation</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> e.<span class="me1">cancelBubble</span> = <span class="kw2">true</span>; <span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// call the actual function, using entity (the element) as the 'this' object</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_fn.<span class="me1">call</span><span class="br0">&#40;</span>_elem, e<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// null out these properties to prevent memory leaks</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">target</span> = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">relatedTarget</span> = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">preventDefault</span> = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.<span class="me1">stopPropagation</span> = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// add the helper function to the event hash</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;evtHash<span class="br0">&#91;</span>key<span class="br0">&#93;</span> = f;</p>
	<p>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// hook up the event (IE style)</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_elem.<span class="me1">attachEvent</span><span class="br0">&#40;</span><span class="st0">'on'</span> + _evtName, f<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;key = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;f = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
	<p>&nbsp; &nbsp;xb.<span class="me1">defineEvent</span> = <span class="kw2">function</span><span class="br0">&#40;</span>_evtName, _logicFn<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;pseudoEvents<span class="br0">&#91;</span>_evtName<span class="br0">&#93;</span> = _logicFn;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="co1">// Helper Functions</span><br />
&nbsp; &nbsp;<span class="kw2">function</span> storeHandler<span class="br0">&#40;</span>_key, _handler<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;evtHash<span class="br0">&#91;</span>_key<span class="br0">&#93;</span> = _handler;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw2">function</span> retrieveHandler<span class="br0">&#40;</span>_key<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> evtHash<span class="br0">&#91;</span>_key<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw2">function</span> generateHandlerKey<span class="br0">&#40;</span>_elem, _evtName, _handler<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> <span class="st0">'{'</span> + getObjectGUID<span class="br0">&#40;</span>_elem<span class="br0">&#41;</span> + <span class="st0">'/'</span> + _evtName + <span class="st0">'/'</span> + getObjectGUID<span class="br0">&#40;</span>_handler<span class="br0">&#41;</span> + <span class="st0">'}'</span>;&nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw2">function</span> isAnAncestorOf<span class="br0">&#40;</span>_ancestor, _descendant, _generation<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>_ancestor === _descendant<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw2">false</span>; <span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> gen = <span class="nu0">0</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>_descendant &amp;&amp; _descendant != _ancestor<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;gen++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_descendant = _descendant.<span class="me1">parentNode</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;_generation = _generation || gen;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> _descendant === _ancestor &amp;&amp; _generation === gen;&nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw2">function</span> mouseEnter<span class="br0">&#40;</span>_fn<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span>&nbsp; <br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> key = xb.<span class="me1">Helper</span>.<span class="me1">getObjectGUID</span><span class="br0">&#40;</span>_fn<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> f = evtHash<span class="br0">&#91;</span>key<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> f === <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;f = evtHash<span class="br0">&#91;</span>key<span class="br0">&#93;</span> = <span class="kw2">function</span><span class="br0">&#40;</span>_evt<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> relTarget = _evt.<span class="me1">relatedTarget</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">this</span> === relTarget || isAnAncestorOf<span class="br0">&#40;</span><span class="kw1">this</span>, relTarget<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span>; <span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_fn.<span class="me1">call</span><span class="br0">&#40;</span><span class="kw1">this</span>, _evt<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> f;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="kw2">function</span> getObjectGUID<span class="br0">&#40;</span>_elem<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>_elem === window<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> <span class="st0">'theWindow'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>_elem === document<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> <span class="st0">'theDocument'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> _elem.<span class="me1">uniqueID</span> !== <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> _elem.<span class="me1">uniqueID</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> ex = <span class="st0">'__$$GUID$$__'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> _elem<span class="br0">&#91;</span>ex<span class="br0">&#93;</span> === <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_elem<span class="br0">&#91;</span>ex<span class="br0">&#93;</span> = ex + GUIDCounter++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> _elem<span class="br0">&#91;</span>ex<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
	<div class = "note">The code above, as is, won't work "out of the box" because (for the sake of brevity) I have not included the <code>xb.removeEvent(..)</code> function.  Fear not however.  There will be a link to a download of the full source code, complete with both <code>xb.addEvent(..)</code> and <code>xb.removeEvent(..)</code> at the end of this post.</div>
	<h3>Commentary</h3>
	<p>So some of you may be wondering: "Why <em>another</em> addEvent function.  Haven't we been through this?"  We have, but I think you'll find that this version does more than any other addEvent function you've seen.  For example:</p>
	<ul>
	<li>Works in all browsers that matter</li>
	<li>Ensures <em>one</em> event wire up for any given element/event/handler combination (this is mostly for IE)</li>
	<li>Forces IE to honor the <code>this</code> keyword from within event handler functions</li>
	<li>Normalizes the wire up mechanism in all browsers (no need to include the "on" prefix for IE)</li>
	<li>Forces IE to recognize the following properties/methods on event objects: <code>.stopPropagation()</code>, <code>.preventDefault()</code>, <code>.target</code>, <code>.relatedTarget</code></li>
	<li>Enhances Non-IE browsers to support <code>mouseenter</code> and <code>mouseleave</code> events</li>
	<li>Provides an extension mechanism so developers can write their own custom events that plug right in ('mousewheel' or 'DOMContentReady' for example)</li>
	</ul>
	<p>Given all that the Ultimate addEvent function does, it's not surprising that it has a bit of length to it.  But it isn't super long, and I think it's well worth it.</p>
	<h3>Usage</h3>
	<p>Using the function(s) is just as easy as you'd expect:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> myDiv = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'myDiv'</span><span class="br0">&#41;</span>;<br />
xb.<span class="me1">addEvent</span><span class="br0">&#40;</span>myDiv, <span class="st0">'click'</span>, clickHandler<span class="br0">&#41;</span>;</p>
	<p><span class="kw2">function</span> clickHandler<span class="br0">&#40;</span>e<span class="br0">&#41;</span></p>
	<p>
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'The this keywords works (even in IE!): '</span> + <span class="kw1">this</span>.<span class="me1">id</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
	<h3>Extending Functionality</h3>
	<p>Since I mentioned that it was possible to extend the Ultimate addEvent function with custom events that plug right in, I figure I'd better offer an example.  Actually, there already is an example built right into it.  Both the <code>mouseenter</code> and <code>mouseleave</code> events utilize the extension mechanism internally, but for demonstration purposes, let's go ahead and add a <code>mousewheel<code> event:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// Extend the Ultimate addEvent function to recognize a &quot;mousewheel&quot; event</span><br />
xb.<span class="me1">defineEvent</span><span class="br0">&#40;</span><span class="st0">'mousewheel'</span>, <span class="kw2">function</span><span class="br0">&#40;</span>_fn, _useCapture, _listening<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="co1">// event name for IE, Opera and Safari</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> evtName = <span class="st0">'mousewheel'</span>;</p>
	<p>&nbsp; &nbsp;<span class="co1">// hander for IE, Opera, and Safari</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> f = _fn;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="co1">// if we're dealing with a Gecko browser, the event name</span><br />
&nbsp; &nbsp;<span class="co1">// and handler need some adjustment</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> ua = navigator.<span class="me1">userAgent</span>.<span class="me1">toLowerCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>ua.<span class="me1">indexOf</span><span class="br0">&#40;</span><span class="st0">'khtml'</span><span class="br0">&#41;</span> === <span class="nu0">-1</span> &amp;&amp; ua.<span class="me1">indexOf</span><span class="br0">&#40;</span><span class="st0">'gecko'</span><span class="br0">&#41;</span> !== <span class="nu0">-1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;evtName = <span class="st0">'DOMMouseScroll'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;f = mouseWheel<span class="br0">&#40;</span>_fn<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;<span class="co1">// _listening represents whether this is an event attachment or detachment</span><br />
&nbsp; &nbsp;<span class="co1">// that 5th parameter? &nbsp;Don't worry about it; just always use false</span><br />
&nbsp; &nbsp;_listening ? xb.<span class="me1">addEvent</span><span class="br0">&#40;</span><span class="kw1">this</span>, evtName, f, _useCapture, <span class="kw2">false</span><span class="br0">&#41;</span> : xb.<span class="me1">removeEvent</span><span class="br0">&#40;</span><span class="kw1">this</span>, evtName, f, _useCapture, <span class="kw2">false</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</p>
	<p><span class="co1">// Helper function for dealing with mousewheel in Gecko browsers</span><br />
<span class="kw2">function</span> mouseWheel<span class="br0">&#40;</span>_fn<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> key = xb.<span class="me1">Helper</span>.<span class="me1">getObjectGUID</span><span class="br0">&#40;</span>_fn<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> f = xb.<span class="me1">Helper</span>.<span class="me1">retrieveHandler</span><span class="br0">&#40;</span>key<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> f === <span class="st0">'undefined'</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;f = <span class="kw2">function</span><span class="br0">&#40;</span>_evt<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_evt.<span class="me1">wheelDelta</span> = -<span class="br0">&#40;</span>_evt.<span class="me1">detail</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_fn.<span class="me1">call</span><span class="br0">&#40;</span><span class="kw1">this</span>, _evt<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;_evt.<span class="me1">wheelDelta</span> = <span class="kw2">null</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;xb.<span class="me1">Helper</span>.<span class="me1">storeHandler</span><span class="br0">&#40;</span>key, f<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> f;<br />
<span class="br0">&#125;</span></div>
	<p>I'm glossing over some details here because I don't want this post to get bogged down in something that it isn't really about, but I trust you get the idea.</p>
	<p>Most of this code actually comes from the <a href = "http://gimme.stchur.com" title = "The Gimme Javascript Library">The Gimme Javascript Library</a>.  If you're using Gimme in any of your web pages, you already have all of this functionality for free.  The syntax is a touch different, but all the capabilities are the same.</p>
	<p>And if you happen to be the author of a Live Maps Mashup, you already have Gimme, as the latest version shipped with the most recent <a href = "http://dev.virtualearth.net" title = "Virtual Earth MapControl Developers">Virtual Earth MapControl</a>.</p>
	<p><a name="end"></a></p>
	<h3>Complete Source</h3>
	<p>As promised, here is the <a href = "http://blog.stchur.com/blogcode/ultimate_addevent/ultimate.zip" title = "The Ultimate addEvent function">complete source</a>, including both <code>xb.addEvent(..)</code> and <code>xb.removeEvent(..)</code></p>
	<p>Enjoy! And please don't hesitate to send your feedback, both good and bad (I'm very willing to address issues or try to make requested enhancements).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/09/25/the-ultimate-addevent-function/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Microsoft Releases New Version of Live Maps</title>
		<link>http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/</link>
		<comments>http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 02:40:34 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Gimme]]></category>
		<category><![CDATA[Live Maps]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/</guid>
		<description><![CDATA[Microsoft released a new version of Live Maps today. This version features multi-way point routing as well as a number of other nice little updates. Another cool thing about this release is that it features Gimme quite prominently. After getting directions, you'll notice the ability to expand/collapse the directions specific to each point along the [...]]]></description>
				<content:encoded><![CDATA[	<p>Microsoft released a new version of <a href = "http://maps.live.com" title = "Live Maps">Live Maps</a> today.  This version features multi-way point routing as well as a number of other nice little updates.</p>
	<p>Another cool thing about this release is that it features <a href = "http://codeplex.com/gimme" title = "The Gimme Javascript Library">Gimme</a> quite prominently.  After getting directions, you'll notice the ability to expand/collapse the directions specific to each point along the route.  That nice, smooth animation you see is Gimme doing what Gimme does best!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
