<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Reign Drops Fall...</title>
	
	<link>http://www.reigndropsfall.net</link>
	<description>Ramblings from a hacker in Iowa</description>
	<lastBuildDate>Wed, 03 Oct 2012 21:52:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/reigndropsfall" /><feedburner:info uri="reigndropsfall" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license><item>
		<title>Internet Explorer Event Handler Leaks</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/7m5pS2OAcsA/</link>
		<comments>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 20:30:26 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=13953</guid>
		<description><![CDATA[If you&#8217;ve been developing for the open web for long, you&#8217;ll know that Internet Explorer is the bane of any web developer&#8217;s existence. The number of CSS bugs are enough to give any web developer a headache, but then add to that JScript&#8217;s deviations from the ECMAScript 3 spec and you have yourself a pain [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been developing for the open web for long, you&#8217;ll know that Internet Explorer is the bane of any web developer&#8217;s existence.  The number of <a href="http://www.positioniseverything.net/explorer.html">CSS bugs</a> are enough to give any web developer a headache, but then add to that <a href="http://wiki.ecmascript.org/lib/exe/fetch.php?id=resources%3Aresources&#038;cache=cache&#038;media=resources:jscriptdeviationsfromes3.pdf">JScript&#8217;s deviations from the ECMAScript 3 spec</a> and you have yourself a pain in the neck as well.  Grab your favorite pain reliever, because there is another nuisance: memory leaks.</p>
<h2>Leaks, Circular References, and Host Objects</h2>
<p>There are two types of leaks in web development: single-page and cross-page.  A single-page leak happens when an object is not collected by the garbage collector (GC) while the page is running, but is cleaned up when the page is unloaded.  The cause of single-page leaks is usually an object being referenced unexpectedly.  A cross-page leak happens when an object is not collected by the GC when the page is unloaded.  We will be focusing on cross-page leaks in this blog post; from this point on, &#8220;leak&#8221; will refer to cross-page leaks.</p>
<p>The cause of cross-page leaks is usually circular references that trip up the GC.  A circular reference is formed when object A references object B which, in turn, references object A.  This seems to be pretty simple to avoid, but you can add more objects into the mix and still have a circular reference: <code>A -> B -> C -> D -> A</code>.  In garbage collected languages, such as ECMAScript (which includes JavaScript and JScript), circular references are (or should be) handled and cleaned up properly.  In fact, most implementations of ECMAScript do a good job of cleaning up circular references between native objects.  However, ECMAScript 3 defined something called &#8220;host objects&#8221; that don&#8217;t have to follow the rules that natvie objects do (see <a href="http://perfectionkills.com/whats-wrong-with-extending-the-dom/#host_objects_have_no_rules">What&#8217;s Wrong With Extending the DOM</a> by Juriy Zaytsev for a run-down of host object wackiness).  Circular references between host objects and native objects are what trips Internet Explorer up.  In fact, all versions of Internet Explorer &#8211; except patched versions of 6 and version 7 &#8211; leak when you leave a page that contains code that forms circular references between host objects and native objects (yes, that includes version 8).</p>
<p>Why do I mention host objects and why can&#8217;t we avoid them all together?  <strong>Internet Explorer implements DOM nodes and <code>ActiveXObject</code>s as host objects that wrap COM+ objects</strong> (the exception to the rule is version 8 where only ActiveXObjects are host objects; you can <a href="http://www.reigndropsfall.net/demos/ie_leak/leak_activexobject.html">run this test</a> to confirm the <code>ActiveXObject</code> leak).  This wouldn&#8217;t be so bad, except the interaction between the garbage collection in JScript and the reference counting that COM+ objects use cause problems.  When a native object (NO) is referenced, it is <a href="http://blogs.msdn.com/b/ericlippert/archive/2003/09/17/53038.aspx">put on the &#8220;scavenger&#8221; list</a> until it is no longer referenced; when a host object (HO) is referenced, the COM+ object&#8217;s reference count is increased and will only be destroyed when the reference count reaches 0.  When a NO and HO are in a circular reference, the HO&#8217;s COM+ object&#8217;s reference count will be decreased when the NO is garbage collected or stops referencing the HO; the NO will be taken off the &#8220;scavenger&#8221; list when nothing references it &#8211; including COM+ objects.  This means any DOM node or <code>ActiveXObject</code> that references native objects has the potential to leak when the page is unloaded.  But all is not lost!  The pattern is easy to identify and there is a fail-safe way to keep leaks from happening.</p>
<h2>Identifying the Pattern</h2>
<p>The basic pattern is this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DOM_Node.objectRef -&gt;
    Object -&gt;
    Object.nodeRef -&gt;
    DOM_Node</pre></div></div>

<p>This translates to the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
elem.<span style="color: #660066;">someObject</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
obj.<span style="color: #660066;">someElement</span> <span style="color: #339933;">=</span> elem<span style="color: #339933;">;</span></pre></div></div>

<p>This is pretty easy to spot (and is a reminder of why not to use expando properties that are non-primitive), but remember that you can add multiple objects into the mix and still have a circular reference:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    obj1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> obj2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
elem.<span style="color: #660066;">someObject</span> <span style="color: #339933;">=</span> obj1<span style="color: #339933;">;</span>
obj1.<span style="color: #660066;">someObject</span> <span style="color: #339933;">=</span> obj2<span style="color: #339933;">;</span>
obj2.<span style="color: #660066;">someElement</span> <span style="color: #339933;">=</span> elem<span style="color: #339933;">;</span></pre></div></div>

<p>That seems easy enough, but there&#8217;s a much more elusive version of this pattern (adapted from <a href="http://www.jibbering.com/faq/notes/closures/#clMem">the comp.lang.javascript FAQ on closures</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DOM_Node.onevent -&gt;
    inner_function_object -&gt;
    inner_function_object.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.nodeRef -&gt;
    DOM_Node</pre></div></div>

<p>This translates into the following (note the surrounding closure):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elem.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        elem.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m not going to explain closures (for a good run-down, see the aforementioned FAQ), but the basic gist of what is happening here is a circular reference is formed through the <a href="http://bclary.com/2004/11/07/#a-10.1.6">activation object</a> of the outer function when accessed through the <a href="http://bclary.com/2004/11/07/#a-10.1.4">scope chain</a> of the event handler.  In pseudo-code, the ES3 internals would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">outer_activation_object.<span style="color: #660066;">elem</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// The scope chain is formed by prepending inner_func's activation</span>
<span style="color: #006600; font-style: italic;">// object to a copy of inner_func's [[Scope]], which is a copy of</span>
<span style="color: #006600; font-style: italic;">// outer_func's scope chain.</span>
inner_func.<span style="color: #660066;">scope_chain</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    inner_activation_object<span style="color: #339933;">,</span>
    outer_activation_object<span style="color: #339933;">,</span>
    global_object
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
elem.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> anonfunc<span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, the reference chain looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onclick -&gt;
    inner_func -&gt;
    inner_func.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>Visiting one or two pages that leak won&#8217;t be too bad (unless they&#8217;re using an inordinate amount of event handlers).  However, if you visit several pages that have circular references on them you will notice IE consuming more and more memory.  I&#8217;ve put together a <a href="http://www.reigndropsfall.net/demos/ie_leak/leak.html">page that will leak in IE6</a> and refresh itself every 2 seconds to demonstrate the leak.  You can use the system monitor to monitor overall system memory usage or a tool like <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a> to monitor the memory usage of individual processes.  This example will be our starting point and we&#8217;ll look at a couple ways to keep a page like this from leaking.  All of the examples in this article can also be downloaded in a <a href="http://www.reigndropsfall.net/demos/ie_leak.tar.bz2">tarball</a> or a <a href="http://www.reigndropsfall.net/demos/ie_leak.zip">zip file</a>.</p>
<p>If you look at the source of this leaky page, you will notice I include <a href="http://www.reigndropsfall.net/demos/ie_leak/helper.js"><code>helper.js</code></a>.  As the name suggests, this file has some helper functions:</p>
<ul>
<li><code>isHostType</code>: A function to safely check for the presence of properties on a host object.  If you&#8217;re not familiar with the reasoning behind this function or the gotchas of host objects, I suggest you read Peter Michaux&#8217;s article on <a href="http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting">feature detection</a>.  If you&#8217;re still looking at <code>navigator.userAgent</code> to know if you should use <code>addEventListener</code>, <code>attachEvent</code>, or <code>elem.onevent = function(){}</code> (DOM0), you need to read this article.  The version of this function I&#8217;m using is copied from <a href="http://github.com/phiggins42/has.js">has.js</a>.</li>
<li><code>_listen</code> and <code>_stopListening</code>: Wrapper functions around the event registration mechanism supported by the current browser.</li>
<li><code>normalizeEventName</code>: A function that makes sure the event name passed in is correct for the current browser.</li>
</ul>
<p>As we look at the different ways to break the leak pattern, I will be modifying the <code>listen</code> and <code>stopListening</code> functions within source of the page; I won&#8217;t have to modify anything in <code>helpers.js</code>.  Now that we have all of that out of the way, let&#8217;s start exploring ways to prevent event handler leaks.</p>
<h2>Fixing It, the Microsoft Way</h2>
<p>Microsoft is well aware of this problem; in fact, they have an <a href="http://msdn.microsoft.com/en-us/library/bb250448(VS.85).aspx">MSDN article on leak patterns in IE</a>, <a href="http://siteexperts.spaces.live.com/Blog/cns!1pNcL8JwTfkkjv4gg6LkVCpw!338.entry">an article from Scott Isaacs</a>, and a <a href="http://support.microsoft.com/default.aspx?scid=KB;EN-US;830555">Knowledge Base article</a> as well.  Let&#8217;s take a look at the ways they recommend to fix the problem.</p>
<h3>Detach on Unload</h3>
<p>The first way that Microsoft recommends is to call <code>elem.detachEvent(eventName, handler)</code> in an unload handler.  The example in the MSDN article mentioned earlier recommends storing the handler in an expando property of the node, however I won&#8217;t show that example here because storing things in an expando property of a node is a no-no in IE for a few reasons:</p>
<ul>
<li>Changing an expando causes the node to re-render</li>
<li>Expandos with primitive values are cloned when you clone a node</li>
<li>Expandos that store non-primitive values can contribute to circular references</li>
</ul>
<p>The third reason is the one that should make you wary of their solution; as we saw earlier, expandos are an easy way to make circular references and their solution is no exception.  Suppose the node with the expando on it is destroyed via <code>.innerHTML</code>: unless we keep a reference to that node around, we&#8217;ve lost it and cannot detach the handler and we now have a leak.</p>
<p>A slightly better solution that runs in the same vein as Microsoft&#8217;s would be to register an unload handler, which forms a closure around the element, for each event registered:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> normalizeEventName<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// unload every attached event</span>
    window.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onunload&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        obj.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This breaks the circular reference manually by removing the reference to <code>handler</code> from the element.  You can see for yourself in <a href="http://www.reigndropsfall.net/demos/ie_leak/no_leak_ms_unload_1.html">this example</a>.  Although this works, it could potentially be registering hundreds, if not thousands, of unload handlers (depending on its use).  A better solution to this would be to use one unload handler with a cache of the arguments used to register each handler:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>global<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _evtData <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> _emptyObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> _nextId <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        unloadAttached <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> attachUnload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>isHostType<span style="color: #009900;">&#40;</span>document<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;attachEvent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// one onunload handler to rule them all</span>
            global.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onunload&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> _evtData<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> _emptyObject<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        stopListening<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        unloadAttached <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> _nextId<span style="color: #339933;">++,</span>
            e <span style="color: #339933;">=</span> normalizeEventName<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _evtData<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>obj<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>unloadAttached<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            attachUnload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003366; font-weight: bold;">function</span> stopListening<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _evtData<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> _evtData<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            _stopListening.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">delete</span> _evtData<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    global.<span style="color: #660066;">listen</span> <span style="color: #339933;">=</span> listen<span style="color: #339933;">;</span>
    global.<span style="color: #660066;">stopListening</span> <span style="color: #339933;">=</span> stopListening<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The API changes slightly: <code>listen</code> now returns the ID of the cached arguments, and <code>stopListening</code> accepts that ID.  It seems that this is a simple solution to a big problem!</p>
<p>This simple solution, however, introduces a new problem: adding an unload handler breaks the &#8220;back/forward&#8221; cache (or, bfcache) in <a href="https://developer.mozilla.org/En/Using_Firefox_1.5_caching">modern</a> <a href="http://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/">browsers</a>.  Although we added a check for <code>attachEvent</code> in our <code>attachUnload</code> function to weed out Firefox, Safari, and Chrome, this would break bfcache in Opera (since Opera has <code>attachEvent</code>); it also could match IE10+ where bfcache might be implemented.  Since there&#8217;s no reliable way to detect if a browser has bfcache, it&#8217;s best to avoid possibly breaking it.</p>
<h3>Define Handlers in Another Scope</h3>
<p>Another solution Microsoft suggests is to define all event handlers in a separate scope so the element isn&#8217;t in the handlers&#8217; scope chain; the code shown in their example uses the global scope, but having all of your event handlers in the global scope is impractical for larger applications.  For this solution, we&#8217;ll revert to <code>listen</code> and <code>stopListening</code> from the original leaking example and <a href="http://www.reigndropsfall.net/demos/ie_leak/no_leak_ms_scope.html">modify the loop</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>i <span style="color: #339933;">&lt;</span> l<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> hookupOnClick <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">function</span> handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            listen<span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span> handler<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> hookupOnClick<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// use stopListening(e, &quot;onclick&quot;, handler) to de-register</span>
        container.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But do we really want to do that every time we set up a handler and for each event type?  It also doesn&#8217;t look very elegant (IMHO).  This solution is starting down the right path, though: we need to create the function attached to the node in a different scope.  Care needs to be taken because it&#8217;s easy to just add another reference into the circular reference chain:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> createHandler<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">function</span> handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        func.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> handler<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    listen<span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> createHandler<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>At first glance, this looks like it should work; a trained eye would see that this is just adding another reference into the chain:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onevent -&gt;
    handler -&gt;
    handler.scope_chain -&gt;
    createHandler_activation_object.func -&gt;
    inner_func -&gt;
    inner_func.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>We need a way to break that chain.</p>
<h2>Fixing It By Global Reference</h2>
<p>As we&#8217;ve seen, accessing an element from a function through an activation object through the scope chain will cause a leak if it&#8217;s not properly cleaned up or worked around.  However, there&#8217;s an interesting behavior I haven&#8217;t mentioned: a circular reference through the global object through the scope chain will not leak!  This behavior was pointed out to me by <a href="http://allyoucanleet.com/">John David Dalton</a>.  By combining this fact with what we&#8217;ve learned so far, we can create a <a href="http://www.reigndropsfall.net/demos/ie_leak/no_leak_1.html">generic event handling system</a> that the browser will clean up after:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createWrapper<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> cid <span style="color: #339933;">=</span> id<span style="color: #339933;">++,</span>
            wrapper <span style="color: #339933;">=</span> createLookupHandler<span style="color: #009900;">&#40;</span>cid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _cache<span style="color: #009900;">&#91;</span>cid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> wrapper<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> createWrapper<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> listen<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> wrapper <span style="color: #339933;">=</span> createWrapper<span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    _listen<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> normalizeEventName<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> wrapper<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> wrapper<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> stopListening<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    _stopListening<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s analyze the reference chain created here:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onclick -&gt;
    createLookupHandler_inner -&gt;
    createLookupHandler_inner.scope_chain -&gt;
    global_object -&gt;
    global_object._cache -&gt;
    _cache -&gt;
    _cache[id] -&gt;
    LeakFunc -&gt;
    LeakFunc.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>As you can see, we have a circular reference but we&#8217;re going through the global object through a scope chain.  <code>_cache</code> <strong>MUST</strong> be a global variable or reached through the global scope.  Accessing <code>_cache</code> through an activation object through the scope chain will cause a leak.  For instance, the following <a href="http://www.reigndropsfall.net/demos/ie_leak/leak_cache_1.html">will leak</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Why?  <code>_cache</code> is now accessible via the outer function&#8217;s activation object:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onevent -&gt;
    createLookupHandler_inner -&gt;
    createLookupHandler_inner.scope_chain -&gt;
    createWrapper_inner_activation_object -&gt;
    createWrapper_inner_activation_object._cache -&gt;
    _cache -&gt;
    _cache[id] -&gt;
    LeakFunc -&gt;
    LeakFunc.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>The following <a href="http://www.reigndropsfall.net/demos/ie_leak/leak_cache_2.html">will also leak</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _myCache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>_cache<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>_myCache<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example, <code>_myCache</code> is added to the outer function&#8217;s activation object via the arguments (which are added to the activation object):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">elem.onevent -&gt;
    createLookupHandler_inner -&gt;
    createLookupHandler_inner.scope_chain -&gt;
    createWrapper_inner_activation_object -&gt;
    createWrapper_inner_activation_object._cache -&gt;
    _myCache -&gt;
    _myCache[id] -&gt;
    LeakFunc -&gt;
    LeakFunc.scope_chain -&gt;
    outer_activation_object -&gt;
    outer_activation_object.elem -&gt;
    elem</pre></div></div>

<p>The object used to store the function references <strong>MUST BE REFERENCED THROUGH THE GLOBAL SCOPE</strong>.  My best guess as to why is that somehow IE6 will increase the reference count of a COM+ object (in this case, a DOMNode) when it is referenced through an activation object through the scope chain.  However, it seems that looking up an object through the global scope (which means you&#8217;re ultimately going through the scope chain) doesn&#8217;t have this same effect.  If someone can shed some more light on this, I would be much obliged.  I have set up a <a href="https://gist.github.com/663298">gist to try to explain what is going on</a> in terms of the ES3 spec and I would appreciate any corrections there as well.</p>
<p>The following examples will not leak because they all ultimately look up functions through the global scope:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>global<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    global._cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// note that we're referencing _cache</span>
                <span style="color: #006600; font-style: italic;">// via the global scope</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    really<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        long<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                _cache<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> createWrapper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// note that we're not closing around</span>
            <span style="color: #006600; font-style: italic;">// any part of the namespace object</span>
            <span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> my.<span style="color: #660066;">really</span>.<span style="color: #660066;">long</span>.<span style="color: #003366; font-weight: bold;">namespace</span>._cache<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Event Handlers in Libraries</h2>
<p>I&#8217;ve taken the liberty of writing some leak tests for the latest versions (at the time of this article) of 6 of the most popular JavaScript libraries used today (full disclosure: I am a Dojo committer).  I have included the results of the tests on IE6 version 6.0.3790.3959 running on Windows 2003 (this version doesn&#8217;t have the patch applied to it to fix the circular reference leak).  Note that I have not included which method the library uses to prevent leaks, just if it leaks or not:</p>
<ul>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_dojo.html">Dojo 1.5</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_ext.html">Ext 3.3.1</a> &#8211; <strong>leaks</strong></li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_jquery.html">jQuery 1.4.4</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_mootools.html">MooTools 1.3.0</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_prototype.html">Prototype 1.7.0.0</a> &#8211; doesn&#8217;t leak</li>
<li><a href="http://www.reigndropsfall.net/demos/ie_leak/leak_test_yui.html">YUI 3.2.0</a> &#8211; <strong>leaks</strong></li>
</ul>
<h2>Conclusion</h2>
<p>From all that we&#8217;ve seen here, the following pattern (or one similar to it) should be used to prevent leaks in event handlers in Internet Explorer without having to use an unload handler:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _cache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> createHandler <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> createLookupHandler<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>id <span style="color: #000066; font-weight: bold;">in</span> _cache <span style="color: #339933;">&amp;&amp;</span> _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                _cache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> createHandler<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> cid <span style="color: #339933;">=</span> id<span style="color: #339933;">++,</span>
            handler <span style="color: #339933;">=</span> createLookupHandler<span style="color: #009900;">&#40;</span>cid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        _cache<span style="color: #009900;">&#91;</span>cid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> handler<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> createHandler<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
elem.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onclick&quot;</span><span style="color: #339933;">,</span> createHandler<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/" title="Adding Axis Titles to DojoX Charts">Adding Axis Titles to DojoX Charts</a></li><li><a href="http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/" title="Using Local Modules With a Cross-Domain Dojo Build">Using Local Modules With a Cross-Domain Dojo Build</a></li><li><a href="http://www.reigndropsfall.net/2010/06/15/monkey-patching/" title="Monkey patching">Monkey patching</a></li><li><a href="http://www.reigndropsfall.net/2005/09/13/catching-up/" title="Catching up">Catching up</a></li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/7m5pS2OAcsA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/</feedburner:origLink></item>
		<item>
		<title>Adding Axis Titles to DojoX Charts</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/BB59bFAzgWU/</link>
		<comments>http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 17:16:32 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=9191</guid>
		<description><![CDATA[DojoX charting is a very powerful 2D graphics charting solution that works cross-browser. One thing that is an often requested feature is axis titles. I&#8217;ve come up with a solution (inspired by a post on the old Dojo forums) that leverages the current API and renders correctly in all browsers. I&#8217;ll be using Dojo 1.5 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://dojotoolkit.org/reference-guide/dojox/charting.html">DojoX charting</a> is a very powerful 2D graphics charting solution that works cross-browser.  One thing that is an often requested feature is axis titles.  I&#8217;ve come up with a solution (inspired by a <a href="http://o.dojotoolkit.org/forum/dojo-foundation/general-discussion/how-add-name-axes-im-not-able">post on the old Dojo forums</a>) that leverages the current API and renders correctly in all browsers.  I&#8217;ll be using Dojo 1.5 from <a href="http://code.google.com/apis/libraries/devguide.html#dojo">Google&#8217;s CDN</a> plus a local module as I outlined in my <a href="http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/">local module with a CDN build tutorial</a>.</p>
<h4>my/Chart2D.js</h4>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my.Chart2D&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dojox.charting.Chart2D&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my.Chart2D&quot;</span><span style="color: #339933;">,</span> dojox.<span style="color: #660066;">charting</span>.<span style="color: #660066;">Chart2D</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
	render<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inherited</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> axes <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">axes</span><span style="color: #339933;">,</span>
			theme_tick <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">theme</span>.<span style="color: #660066;">axis</span>.<span style="color: #660066;">tick</span><span style="color: #339933;">,</span>
			theme_font <span style="color: #339933;">=</span> theme_tick.<span style="color: #660066;">font</span><span style="color: #339933;">,</span>
			theme_font_color <span style="color: #339933;">=</span> theme_tick.<span style="color: #660066;">fontColor</span><span style="color: #339933;">,</span>
			dim <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dim</span><span style="color: #339933;">,</span>
			offsets <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">offsets</span><span style="color: #339933;">,</span>
			x_middle <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>dim.<span style="color: #660066;">width</span> <span style="color: #009966; font-style: italic;">/ 2) + (offsets.l /</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			y_middle <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>dim.<span style="color: #660066;">height</span> <span style="color: #009966; font-style: italic;">/ 2) - (offsets.b /</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			m <span style="color: #339933;">=</span> dojox.<span style="color: #660066;">gfx</span>.<span style="color: #660066;">matrix</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// For each axis defined, loop through, check if there</span>
		<span style="color: #006600; font-style: italic;">// is a 'title' property defined.</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> axes<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> axis <span style="color: #339933;">=</span> axes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>axis.<span style="color: #660066;">opt</span>.<span style="color: #660066;">title</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span>
					rotate <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// If the axis is vertical, rotate it</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>axis.<span style="color: #660066;">vertical</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					rotate <span style="color: #339933;">=</span> <span style="color: #CC0000;">270</span><span style="color: #339933;">;</span>
					y <span style="color: #339933;">=</span> y_middle<span style="color: #339933;">;</span>
					x <span style="color: #339933;">=</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
					x <span style="color: #339933;">=</span> x_middle<span style="color: #339933;">;</span>
					y <span style="color: #339933;">=</span> dim.<span style="color: #660066;">height</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Render the text in the middle of the chart</span>
				<span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> axis.<span style="color: #660066;">group</span>.<span style="color: #660066;">createText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
					x<span style="color: #339933;">:</span> x_middle<span style="color: #339933;">,</span>
					y<span style="color: #339933;">:</span> y_middle<span style="color: #339933;">,</span>
					text<span style="color: #339933;">:</span> axis.<span style="color: #660066;">opt</span>.<span style="color: #660066;">title</span><span style="color: #339933;">,</span>
					align<span style="color: #339933;">:</span> <span style="color: #3366CC;">'middle'</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Set the font and font color</span>
				elem.<span style="color: #660066;">setFont</span><span style="color: #009900;">&#40;</span>
					axis.<span style="color: #660066;">opt</span>.<span style="color: #660066;">font</span> <span style="color: #339933;">||</span> theme_font
				<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setFill</span><span style="color: #009900;">&#40;</span>
					axis.<span style="color: #660066;">opt</span>.<span style="color: #660066;">fontColor</span> <span style="color: #339933;">||</span> theme_font_color
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// If the axis is vertical, rotate and move into position,</span>
				<span style="color: #006600; font-style: italic;">// otherwise just move into position.</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>rotate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					elem.<span style="color: #660066;">setTransform</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
						m.<span style="color: #660066;">rotategAt</span><span style="color: #009900;">&#40;</span>rotate<span style="color: #339933;">,</span> x_middle<span style="color: #339933;">,</span> y_middle<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
						m.<span style="color: #660066;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> x <span style="color: #339933;">-</span> x_middle<span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
					elem.<span style="color: #660066;">setTransform</span><span style="color: #009900;">&#40;</span>m.<span style="color: #660066;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y <span style="color: #339933;">-</span> y_middle<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is pretty straight-forward: the code figures out where the middle of the axes are and places the title there, rotating if necessary.  I&#8217;ll point out one important part: the text must initially be created in the middle of the chart and then translated into position.  If the text is created where it should be placed, WebKit-based browsers will hide the vertical axis&#8217; title because it initially renders partially off-screen.</p>
<p>You use this new class exactly like you would use <a href="http://dojotoolkit.org/api/dojox/charting/Chart2D.html">dojox.charting.Chart2D</a>, except that each axis can now be passed a <code>title</code> attribute:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> my.<span style="color: #660066;">Chart2D</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chartNode'</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">setTheme</span><span style="color: #009900;">&#40;</span>dojox.<span style="color: #660066;">charting</span>.<span style="color: #660066;">themes</span>.<span style="color: #660066;">PlotKit</span>.<span style="color: #660066;">cyan</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">addPlot</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;default&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Default&quot;</span><span style="color: #339933;">,</span>
		lines<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		markers<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		tension<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">addAxis</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;x&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'X Axis'</span><span style="color: #339933;">,</span>
		min<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
		max<span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>
		majorTick<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #339933;">,</span> length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		minorTick<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;gray&quot;</span><span style="color: #339933;">,</span> length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">addAxis</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;y&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Y Axis'</span><span style="color: #339933;">,</span>
		vertical<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		min<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
		max<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
		majorTick<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #339933;">,</span> length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		minorTick<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;gray&quot;</span><span style="color: #339933;">,</span> length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">addSeries</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Series A&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">1.5</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">1.5</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">9</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.3</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">addSeries</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Series B&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.3</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">8</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">5.5</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.
	<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.reigndropsfall.net/demos/charting_titles/charting_titles.html"><img src="http://www.reigndropsfall.net/wp-content/uploads/2010/08/chart-with-axis-titles.jpg" alt="Chart with axis titles" title="Chart with axis titles" class="alignnone size-medium wp-image-11238" /></a></p>
<p>I&#8217;ve posted a <a href="http://www.reigndropsfall.net/demos/charting_titles/charting_titles.html">live example</a>, a <a href="http://www.reigndropsfall.net/demos/charting_titles.tar.bz2">tarball</a>, and a <a href="http://www.reigndropsfall.net/demos/charting_titles.zip">zip file</a> for download.  As you can see, DojoX charting is quite powerful on its own but is easily extensible.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/" title="Using Local Modules With a Cross-Domain Dojo Build">Using Local Modules With a Cross-Domain Dojo Build</a></li><li><a href="http://www.reigndropsfall.net/2010/06/15/monkey-patching/" title="Monkey patching">Monkey patching</a></li><li><a href="http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/" title="Internet Explorer Event Handler Leaks">Internet Explorer Event Handler Leaks</a></li><li><a href="http://www.reigndropsfall.net/2006/10/16/165/" title="Update on life">Update on life</a></li><li><a href="http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/" title="And life keeps flying by">And life keeps flying by</a></li><li><a href="http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/" title="My current endeavors">My current endeavors</a></li><li><a href="http://www.reigndropsfall.net/2006/01/21/updates-2/" title="Updates">Updates</a></li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/BB59bFAzgWU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/</feedburner:origLink></item>
		<item>
		<title>Using Local Modules With a Cross-Domain Dojo Build</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/Fy0_VMVA8aE/</link>
		<comments>http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 18:58:30 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[cdn]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[xdomain]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=1941</guid>
		<description><![CDATA[Lately, I have been using Dojo from a CDN as much as possible. There are several reasons to use a cross-domain build of Dojo that are listed here: You get more connections in MSIE, since you can load from another domain. Faster loading. You get increased cacheability/startup if many of your applications use the same [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I have been using <a href="http://dojotoolkit.org">Dojo</a> from a <abbr title="Content Delivery Network"><a href="http://en.wikipedia.org/wiki/Content_Delivery_Network">CDN</a></abbr> as much as possible.  There are several reasons to use a cross-domain build of Dojo that are listed <a href="http://docs.dojocampus.org/quickstart/cross-domain">here</a>:</p>
<ol>
<li>You get more connections in MSIE, since you can load from another domain. Faster loading.</li>
<li>You get increased cacheability/startup if many of your applications use the same installation.</li>
<li>Resource loading does not block the rest of the page from filling in as with Dojo&#8217;s normal, synchronous loading.</li>
<li>With a local install, your ISP may otherwise charge you for all of those Dojo bits that you are serving.</li>
</ol>
<p>The one problem with using a CDN is that you don&#8217;t have control over the build, which means you can&#8217;t build custom code into it.  Building custom code into a build is what most people do with builds, so it would seem you can&#8217;t develop code using Dojo&#8217;s module system against a CDN.  This couldn&#8217;t be farther from the truth.</p>
<p>In this post, we&#8217;re going to be using Dojo from <a href="http://dev.aol.com/dojo">AOL&#8217;s CDN</a>.  This method can also be used against a custom cross-domain build.  It may not seem like that would be very useful, but you may need to develop a new custom module against your build and don&#8217;t want to rebuild to test each change you make.  Let&#8217;s look at the source:</p>
<h4>xdlocal.html</h4>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>XDomain Build With Local Modules<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
		@import &quot;http://o.aolcdn.com/dojo/1.4.3/dijit/themes/tundra/tundra.css&quot;;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
		var djConfig = {
			isDebug: true,
			parseOnLoad: true,
			baseUrl: './',
			modulePaths: {
				'my': 'my'
			}
		};
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://o.aolcdn.com/dojo/1.4.3/dojo/dojo.xd.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
		dojo.require(&quot;my.Foo&quot;);
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tundra&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> dojoType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my.Foo&quot;</span>&gt;</span>
		This is a &quot;my.Foo&quot; button.
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<h4>my/Foo.js</h4>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my.Foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dijit.form.Button&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my.Foo&quot;</span><span style="color: #339933;">,</span> dijit.<span style="color: #660066;">form</span>.<span style="color: #660066;">Button</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
	onClick<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Button clicked!!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I have also provided a <a href="http://www.reigndropsfall.net/demos/xdlocal/xdlocal.html">live example</a> as well to show it working.  You can grab the source files in a <a href="http://www.reigndropsfall.net/demos/xdlocal.tar.bz2">tarball</a> or <a href="http://www.reigndropsfall.net/demos/xdlocal.zip">zip file</a> as well so you can play around with it.</p>
<p>The key part here is in the HTML file:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
	var djConfig = {
		isDebug: true,
		parseOnLoad: true,
		baseUrl: './',
		modulePaths: {
			'my': 'my'
		}
	};
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://o.aolcdn.com/dojo/1.4.3/dojo/dojo.xd.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p><code>djConfig</code> is used to configure Dojo upon loading.  The <code>modulePaths</code> object tells where to find the modules relative to <code>baseUrl</code>.  Changing <code>baseUrl</code> does not affect the URL used for <code>dojo</code>, <code>dijit</code>, or <code>dojox</code> because those are hard-coded in the cross-domain build.  The main thing to remember is that <code>baseUrl</code> must end in a <code>/</code>.  Given this information, we can deduce that I am telling Dojo that the <code>my</code> modules can be found at <code>http://www.reigndropsfall.net/demos/xdlocal/my/</code>.</p>
<p>As you can see from this simple example, Dojo&#8217;s module system is very versatile.  Not only can you host a build, host a custom build, host a cross-domain build, or use a CDN, but you can combine cross-domain builds with local modules which can save time and bandwidth costs.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/" title="Adding Axis Titles to DojoX Charts">Adding Axis Titles to DojoX Charts</a></li><li><a href="http://www.reigndropsfall.net/2010/06/15/monkey-patching/" title="Monkey patching">Monkey patching</a></li><li><a href="http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/" title="Internet Explorer Event Handler Leaks">Internet Explorer Event Handler Leaks</a></li><li><a href="http://www.reigndropsfall.net/2006/10/16/165/" title="Update on life">Update on life</a></li><li><a href="http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/" title="And life keeps flying by">And life keeps flying by</a></li><li><a href="http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/" title="My current endeavors">My current endeavors</a></li><li><a href="http://www.reigndropsfall.net/2006/01/21/updates-2/" title="Updates">Updates</a></li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/Fy0_VMVA8aE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/</feedburner:origLink></item>
		<item>
		<title>Monkey patching</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/8pyX7sKftjE/</link>
		<comments>http://www.reigndropsfall.net/2010/06/15/monkey-patching/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 21:28:38 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=889</guid>
		<description><![CDATA[Recently, David Walsh tweeted that I had schooled him. I received several questions about what I had schooled him in, so I decided to blog about it. David was trying to change the behavior of a method of a widget on a page, but for all instances of that widget rather than just one instance. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, <a href="http://davidwalsh.name/">David Walsh</a> <a href="http://twitter.com/davidwalshblog/status/16173861841">tweeted</a> that I had schooled him.  I received several questions about what I had schooled him in, so I decided to blog about it.</p>
<p>David was trying to change the behavior of a method of a widget on a page, but for all instances of that widget rather than just one instance.  What follows is a lesson on monkey patching.  I&#8217;m sure there are other tutorials out there about it, but this is more for my sanity (and to get David off my back about blogging) than anything else.</p>
<p>Wikipedia defines <a href="http://en.wikipedia.org/wiki/Monkey_patch">Monkey patching</a> as &#8220;a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, etc.) without altering the original source code.&#8221;  For our purposes, we&#8217;ll only be looking at JavaScript.  Wikipedia also lists four uses for monkey patching:</p>
<ul>
<li>Replace methods/attributes/functions at runtime.</li>
<li>Modify/extend behavior of a third-party product without maintaining a private copy of the source code.</li>
<li>Apply a patch at runtime to the objects in memory, instead of the source code on disk.</li>
<li>Distribute security or behavioral fixes that live alongside the original source code.</li>
</ul>
<p>In the JavaScript world, monkey patching can be very important if you&#8217;re using a third-party library.  This is especially true if you are using it from a <abbr title="Content Delivery Network"><a href="http://en.wikipedia.org/wiki/Content_delivery_network">CDN</a></abbr> or are checking it out from a version control system.  We&#8217;ll use this HTML as our test page and use <a href="http://dojotoolkit.org">Dojo</a> from the <a href="http://dev.aol.com/dojo">AOL CDN</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt; !DOCTYPE HTML&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>A Monkey-patching example<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://o.aolcdn.com/dojo/1.4.3/dojo/dojo.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
		dojo.ready(function(){
			dojo.declare(&quot;Adder&quot;, null, {
				constructor: function(base){
					this._base = base;
				},
&nbsp;
				add: function(what){
					return this._base + what;
				}
			});
		});
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>I will assume basic knowledge of JavaScript, object prototype chains, and <code>dojo.declare</code>.  The code examples that follow can be pasted into your browser&#8217;s JavaScript console (such as <a href="http://getfirebug.com/">Firebug</a>).</p>
<h3>Monkey patching object instances</h3>
<p>Let&#8217;s say you have an <code>Adder</code> instance and you&#8217;d like to change the calculation that happens when calling <code>add()</code>.  Simply add an <code>add</code> method to the object instance:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myAdder <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Adder<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myAdder.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 10</span>
myAdder.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>what<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> what <span style="color: #339933;">+</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
myAdder.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 25</span></pre></div></div>

<p>That&#8217;s all well and good, but how do we perform the original <code>add</code> calculation, yet modify it without rewriting the method completely?  Just store the <code>add</code> method to a variable, add an <code>add</code> method to the object instance, and call the stored <code>add</code> method within the new <code>add</code> method.  You&#8217;ll generally also want to wrap that all in a closure so only the new method can access the old method.  Confused yet?  Don&#8217;t be:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myAdder2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Adder<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myAdder2.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 10</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> oldAdd <span style="color: #339933;">=</span> myAdder2.<span style="color: #660066;">add</span><span style="color: #339933;">;</span>
	myAdder2.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>what<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> oldAdd.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> what<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myAdder2.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 30</span></pre></div></div>

<h3>One note</h3>
<p>You might have noticed that I didn&#8217;t use the word &#8220;overwrite&#8221; when talking about monkey patching object instances.  This is because object instances look up their properties from their prototype unless explicitly defined on them (like <code>_base</code>).  This is what one might call &#8220;masking&#8221; and is an important concept in JavaScript.  When we were monkey patching before, we were actually masking the <code>add</code> method on the <code>Adder</code> prototype (it&#8217;s a subtle difference, but important).</p>
<h3>Monkey patching an object&#8217;s prototype</h3>
<p>Great!  We can modify individual instances.  But let&#8217;s say we have 30 Adder instances and we don&#8217;t want to modify them all every time.  This is where monkey patching the prototype comes in:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myAdder3 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Adder<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myAdder3.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 10</span>
Adder.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>what<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._base <span style="color: #339933;">+</span> what <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
myAdder3.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 20</span></pre></div></div>

<p>Here, we&#8217;re overwriting (instead of masking) the <code>add</code> method.  This applies to instances already created that haven&#8217;t had their methods masked (try <code>myAdder.add(5);</code> and <code>myAdder2.add(5);</code>) and future created instances.  As we did before, you can also call the originally defined method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myAdder4 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Adder<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myAdder4.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 20</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> oldAdd <span style="color: #339933;">=</span> Adder.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">add</span><span style="color: #339933;">;</span>
	Adder.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>what<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> oldAdd.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> what<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myAdder4.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 30</span>
myAdder3.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 30</span></pre></div></div>

<p>Note how <code>myAdder3</code> is also affected by this new monkey patch.  As you can see, monkey patching is quite simple yet very powerful.  Although I used <code>dojo.declare</code>, this can be used to modify any JavaScript object or prototype chain: you can modify a widget&#8217;s behavior, add debugging output to a method, or even modify arguments passed to the original function.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/" title="Adding Axis Titles to DojoX Charts">Adding Axis Titles to DojoX Charts</a></li><li><a href="http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/" title="Using Local Modules With a Cross-Domain Dojo Build">Using Local Modules With a Cross-Domain Dojo Build</a></li><li><a href="http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/" title="Internet Explorer Event Handler Leaks">Internet Explorer Event Handler Leaks</a></li><li><a href="http://www.reigndropsfall.net/2006/10/16/165/" title="Update on life">Update on life</a></li><li><a href="http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/" title="And life keeps flying by">And life keeps flying by</a></li><li><a href="http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/" title="My current endeavors">My current endeavors</a></li><li><a href="http://www.reigndropsfall.net/2006/01/21/updates-2/" title="Updates">Updates</a></li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/8pyX7sKftjE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/06/15/monkey-patching/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2010/06/15/monkey-patching/</feedburner:origLink></item>
		<item>
		<title>Back from hiatus</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/tzZFGhgcbF0/</link>
		<comments>http://www.reigndropsfall.net/2010/06/15/back-from-hiatus/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 21:14:23 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=926</guid>
		<description><![CDATA[It&#8217;s been a long long time since I&#8217;ve seriously blogged and I&#8217;m going to get back into it again (at the behest of David Walsh). I&#8217;ve got a couple of tutorials and/or tips lined up and I intend to keep up with it. Alright, back to your regularly scheduled programming&#8230; Related PostsNo Related Post]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long long time since I&#8217;ve seriously blogged and I&#8217;m going to get back into it again (at the behest of <a href="http://davidwalsh.name/">David Walsh</a>).  I&#8217;ve got a couple of tutorials and/or tips lined up and I intend to keep up with it.  Alright, back to your regularly scheduled programming&hellip;</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/tzZFGhgcbF0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/06/15/back-from-hiatus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2010/06/15/back-from-hiatus/</feedburner:origLink></item>
		<item>
		<title>Recipe for Brye</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/6EWBmPEBdyM/</link>
		<comments>http://www.reigndropsfall.net/2009/02/24/recipe-for-brye/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 22:00:02 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[family]]></category>
		<category><![CDATA[recipe]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=465</guid>
		<description><![CDATA[A few people have asked me for this after I made mention of it in my status updates. This is a Forbes-family treat that is great when you&#8217;re feeling crummy. We used to have it all the time when I was a kid and we were home from school. I just learned the other night [...]]]></description>
			<content:encoded><![CDATA[<p>A few people have asked me for this after I made mention of it in my status updates.  This is a Forbes-family treat that is great when you&#8217;re feeling crummy.  We used to have it all the time when I was a kid and we were home from school.  I just learned the other night that it&#8217;s actually a dessert (I would never have guessed).  Anyway, here is the recipe for Brye:</p>
<blockquote><p>
Ingredients:<br />
2 cups milk<br />
1/2 cup sugar<br />
1/4 cup flour<br />
pinch of salt<br />
1 tsp vanilla<br />
1 egg<br />
2 tbsp butter</p>
<p>Directions:<br />
Put the sugar and flour into a medium saucepan.  Measure the milk in a glass measuring cup and pour into saucepan.  In the same measuring cup, whisk the egg and pour into saucepan.  Bring to a boil.  Add the butter, vanilla, and salt and stir until it thickens.  Let it cool a bit before partaking!
</p></blockquote>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/6EWBmPEBdyM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2009/02/24/recipe-for-brye/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2009/02/24/recipe-for-brye/</feedburner:origLink></item>
		<item>
		<title>A petition to take back our freedoms</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/sNqIS2DoZyw/</link>
		<comments>http://www.reigndropsfall.net/2008/07/29/a-petition-to-take-back-our-freedoms/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 23:48:50 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Politics]]></category>
		<category><![CDATA[bush administration]]></category>
		<category><![CDATA[impeachment]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/?p=293</guid>
		<description><![CDATA[I know it&#8217;s been a long time since I&#8217;ve written a real blog post, but I wanted to make sure everyone knew about this: A petition to bring articles of impeachment against the Bush administration. If you have the time, please read the articles. For those of you who don&#8217;t like to read and would [...]]]></description>
			<content:encoded><![CDATA[<p>I know it&#8217;s been a long time since I&#8217;ve written a real blog post, but I wanted to make sure everyone knew about this: <a href="http://kucinich.us/">A petition to bring articles of impeachment against the Bush administration</a>.  If you have the time, please read <a href="http://kucinich.us/impeachment/articles.pdf">the articles</a>.  For those of you who don&#8217;t like to read and would rather watch something (or even listen to it), please check out these links:</p>
<p><a href="http://www.youtube.com/watch?v=DRAcenaTVkQ">Hearing on Limits of Executive Power: Dennis Kucinich</a><br />
<a href="http://www.youtube.com/watch?v=80IphtHrFzg">Hearing on Limits of Executive Power: Bruce Fein</a><br />
<a href="http://www.youtube.com/watch?v=GDAFozFn4kU">Hearing on Limits of Executive Power: Vincent Bugliosi</a><br />
<a href="http://www.youtube.com/watch?v=_T1ojrKhp6E">Hearing on Limits of Executive Power: Robert Wexler</a></p>
<p>I want to let everyone know that I&#8217;m not a war for oil, moveon.org, or Bush==Hitler person.  I voted for Bush in 2000 and 2004.  I supported the Bush administration&#8217;s decision to go into Afghanistan to fight Al-Qaeda.  I even bought the need to go into Iraq.  But I&#8217;m also a person who believes that if you make a mistake, you don&#8217;t cover it up: you own up.  I believe that when an someone abuses the power they have been given, they need to be held accountable.  I urge you to read the articles of impeachment and watch the clips and decide for yourself.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/sNqIS2DoZyw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2008/07/29/a-petition-to-take-back-our-freedoms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2008/07/29/a-petition-to-take-back-our-freedoms/</feedburner:origLink></item>
		<item>
		<title>If you support our troops…</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/6yomOCB2a3M/</link>
		<comments>http://www.reigndropsfall.net/2008/02/16/if-you-support-our-troops/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 17:32:58 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[ron paul]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/2008/02/16/if-you-support-our-troops/</guid>
		<description><![CDATA[I know I&#8217;m way overdue for an update, but I had to post this. I&#8217;ve also tried to stay out of the political posting this season, but I thought I&#8217;d post this. The mainstream media has done an excellent job of squelching a fine candidate: Ron Paul. Yes, he has very libertarian stands and that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I know I&#8217;m way overdue for an update, but I had to post this.  I&#8217;ve also tried to stay out of the political posting this season, but I thought I&#8217;d post this.  The mainstream media has done an excellent job of squelching a fine candidate: <a href="http://www.ronpaul2008.com">Ron Paul</a>.  Yes, he has very libertarian stands and that&#8217;s what appeals to me: he believes that the federal government should only take care of what is outlined in the constitution.  It seems like his message is resonating with our troops (warning: Flash content follows&#8230; I&#8217;m not a big fan of proprietary technology, but I feel like this needs to be spread):</p>
<p><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/j5Aue2yZAOs&#038;rel=1&#038;border=0"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/j5Aue2yZAOs&#038;rel=1&#038;border=0" type="application/x-shockwave-flash" wmode="transparent"width="425" height="355"></embed></object></p>
<p>Via <a href="http://www.dailypaul.com/node/38411">Daily Paul</a>.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/6yomOCB2a3M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2008/02/16/if-you-support-our-troops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2008/02/16/if-you-support-our-troops/</feedburner:origLink></item>
		<item>
		<title>Through field and barbed wire</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/WLGn9ks_QZ0/</link>
		<comments>http://www.reigndropsfall.net/2007/06/19/through-field-and-barbed-wire/#comments</comments>
		<pubDate>Tue, 19 Jun 2007 15:10:08 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[asaph's apprentice]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/2007/06/19/through-field-and-barbed-wire/</guid>
		<description><![CDATA[So last night, Asaph&#8217;s Apprentice drove down to Lamoine Christian Service Camp last night to play a show. It wouldn&#8217;t have been too much of a hassle, except that we got our directions from MapQuest (no link love for them&#8230; you&#8217;ll soon see why). Here are the directions we used. Note directions #14 and #16. [...]]]></description>
			<content:encoded><![CDATA[<p>So last night, Asaph&#8217;s Apprentice drove down to <a href="http://www.lamoinechristiancamp.org/">Lamoine Christian Service Camp</a> last night to play a show.  It wouldn&#8217;t have been too much of a hassle, except that we got our directions from MapQuest (no link love for them&#8230; you&#8217;ll soon see why).</p>
<p><a href="http://www.mapquest.com/directions/main.adp?go=1&#038;do=nw&#038;rmm=1&#038;mo=ma&#038;2si=navt&#038;un=m&#038;1rc=L1AAA&#038;cl=EN&#038;qq=IEaKSvfsRsoEUP0r5eJRzX7qYmW%252bAyeym7GXdrX4O%252bgj%252b%252baG9zMhFepX6KMMunttEQqysXg%252bE64nwGXm0CHRbVHguVKowXl%252f3jwElyocDorOTxn3r6ip%252f7lIsk0hxXx6syHEKbikMTcOog1EHx9wLilADknB1cQIHUvd4usWbYc3j50dPVVd1KeVtN4PlgFsVwn1L075lIDnPA%252fnw%252b3DxTE8CEgbR8mj8Y%252bS6MS6mz2n448PnMWHrCQD5GLjhsgg9JUjYgT%252b2cOd5rxMbiewE57dzAZhhWMm9470K8ydHqW3Qasbpz%252b2Hw7bmu5nZkYu20tNleSzsMgXY7FloZP1%252ff9C37X5Oa%252bPj1E4LsbqeZuPY%252fqkLYnM0C8l0DbzQoW2moMdRZI9oIUgoPIqBNyO9snEbwBNtgtSQgizwZ2cGbqtKhU6Bru39Uy%252bpEA8Fe14&#038;ct=NA&#038;r=f&#038;1si=navt&#038;2rc=L1AAA&#038;did=1182262736&#038;rsres=1&#038;1y=US&#038;1ffi=&#038;1l=&#038;1g=&#038;1pl=&#038;1v=&#038;1n=&#038;1pn=&#038;1a=111+CANDLESTICK+DR+NE&#038;1c=MOUNT+VERNON&#038;1s=IA&#038;1z=52314-1541&#038;panelbtn=1&#038;2y=US&#038;2ffi=&#038;2l=5XcEO8WjHPGO3n4yGAfNnA%253d%253d&#038;2g=7fAhh3FEtJdvsQVid548Ww%253d%253d&#038;2pl=&#038;2v=ADDRESS&#038;2n=HANCOCK+COUNTY&#038;2pn=&#038;2a=2760+E+County+Road+1600&#038;2c=Tennessee&#038;2s=IL&#038;2z=62374-3005">Here</a> are the directions we used.  Note directions #14 and #16.  Right turns on CR-2800&#8230; easy enough.  Or so we thought.  The first turn onto CR-2800 goes through some guy&#8217;s field and ends up where the directions say it should.  The second turn onto CR-2800 is a little more misleading.  If you click on the map link for direction #16, it shows you a nice straight road&#8230; this is not what CR-2800 currently looks like.  We ended up driving through another field and ended <strong>at a barbed wire fence</strong>.  I wish we would have taken a picture&#8230; it was priceless.  Thank you MapQuest.</p>
<p>Of course, Google Maps doesn&#8217;t get it <a href="http://maps.google.com/maps?f=d&#038;hl=en&#038;saddr=111+Candlestick+Dr,+Mount+Vernon,+IA+52314&#038;daddr=2760+E.+County+Road+1600,+Tennessee,+IL+62374&#038;sll=41.207096,-91.320419&#038;sspn=2.979475,3.702393&#038;ie=UTF8&#038;ll=41.178654,-91.318359&#038;spn=2.980792,3.702393&#038;z=8&#038;om=1">any better</a> and Rand McNally <a href="http://www.randmcnally.com/rmc/directions/dirGetDirections.jsp?BV_SessionID=@@@@0107167583.1182264793@@@@&#038;BV_EngineID=ccceaddlffgjhhdcefecggfdffhdgnh.0&#038;cmty=0&#038;notcache=1">fails miserably</a> as well.  The only directions site that gives directions that don&#8217;t end up in the middle of nowhere (literally) is Yahoo! Maps.  And <a href="http://maps.yahoo.com/broadband#gid2=17584515&#038;q2=2760+E.+County+Road+1600%2C+Tennessee%2C+IL+62374&#038;q1=Cedar+Rapids%2C+IA%2C+52403&#038;mvt=m&#038;trf=0&#038;lon=-91.238708&#038;lat=41.294317&#038;mag=10">their directions</a> end up taking less time too.</p>
<p>What is it with these map sites?  Why don&#8217;t they have a review feature?  It ended up taking us an hour more to get to the camp because we had to backtrack out of the field (while bottoming out a couple of times) and then flagging down someone who&#8217;s first question to us was, &#8220;Are you guys looking for the camp?&#8221;  If we hadn&#8217;t have planned to get there 2 hours early, we would have been late.  And yes, I know that all of these map sites have their little disclaimer that says something to the effect of &#8220;if these directions suck and you end up in the middle of a field or lake or get eaten by cannibals it&#8217;s not our fault,&#8221; but seriously&#8230; it&#8217;d be nice to be able to get alternate routes from people that actually live in the place that you&#8217;re going to or have been there before.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/WLGn9ks_QZ0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2007/06/19/through-field-and-barbed-wire/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2007/06/19/through-field-and-barbed-wire/</feedburner:origLink></item>
		<item>
		<title>Don’t Panic!!!</title>
		<link>http://feedproxy.google.com/~r/reigndropsfall/~3/gxlUqkbWmEs/</link>
		<comments>http://www.reigndropsfall.net/2007/05/25/dont-panic/#comments</comments>
		<pubDate>Fri, 25 May 2007 14:49:49 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[towel day]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/2007/05/25/dont-panic/</guid>
		<description><![CDATA[Today is Towel Day. That is all for now. Related PostsNo Related Post]]></description>
			<content:encoded><![CDATA[<p>Today is <a href="http://en.wikipedia.org/wiki/Towel_day">Towel Day</a>.  That is all for now.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul><img src="http://feeds.feedburner.com/~r/reigndropsfall/~4/gxlUqkbWmEs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2007/05/25/dont-panic/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.reigndropsfall.net/2007/05/25/dont-panic/</feedburner:origLink></item>
	</channel>
</rss>
