<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reign Drops Fall... &#187; Code</title>
	<atom:link href="http://www.reigndropsfall.net/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reigndropsfall.net</link>
	<description>Ramblings from a hacker in Iowa</description>
	<lastBuildDate>Wed, 09 May 2012 19:48:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Internet Explorer Event Handler Leaks</title>
		<link>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/</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>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adding Axis Titles to DojoX Charts</title>
		<link>http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/</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>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Local Modules With a Cross-Domain Dojo Build</title>
		<link>http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/</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>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/07/07/local-modules-with-xdomain/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Monkey patching</title>
		<link>http://www.reigndropsfall.net/2010/06/15/monkey-patching/</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>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2010/06/15/monkey-patching/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>And life keeps flying by</title>
		<link>http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/</link>
		<comments>http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/#comments</comments>
		<pubDate>Tue, 20 Jun 2006 03:03:35 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojo developer day]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/</guid>
		<description><![CDATA[It&#8217;s been almost a month and a half and my blogging has been quite absent. Here&#8217;s what has been happening&#8230; Life Our trip to San Jose (last month) went great. We had 4 days to bum around Silicon Valley and the surrounding areas. Since this was a vacation, we decided not to do too much [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been almost a month and a half and my blogging has been quite absent.  Here&#8217;s what has been happening&#8230;</p>
<p><span style="font-weight: bold">Life</span><br />
Our trip to San Jose (last month) went great.  We had 4 days to bum around Silicon Valley and the surrounding areas.  Since this was a vacation, we decided not to do too much and really only did one touristy thing: we went down to Carmel and 17 mile drive for a day.  It was fun and it was a really beautiful area.  And the driving out there wasn&#8217;t too bad.  The Dojo Developer Day (which is what I went out there for in the first place) went great as well.</p>
<p><span style="font-weight: bold">Dojo</span><br />
Dojo has been taking off recently.  Last month, we released 0.3.0 and about a week ago we released 0.3.1.  For the 0.3.0 release, I refactored/rewrote the effects portion, James Burke wrote a cross-domain loader, Tom Trenka added a Google and Yahoo! maps interface, and Torrey Rice added the new widget theme.  There was more, but those are the ones that stick out in my mind.  We had about 50k downloads in the month that 0.3.0 was out.  For 0.3.1, the cross-domain loading has been improved and we have a cross-domain package now, plus AOL is now hosting Dojo on their CDN, IBM is actively helping with a11y, and Sun just announced they are going to start developing widgets for Dojo.  All-in-all, it&#8217;s been a good month.</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/2010/06/15/monkey-patching/" title="Monkey patching">Monkey patching</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/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>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2006/06/19/and-life-keeps-flying-by/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My current endeavors</title>
		<link>http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/</link>
		<comments>http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/#comments</comments>
		<pubDate>Sat, 01 Apr 2006 22:29:51 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[coaster]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[sitepen]]></category>

		<guid isPermaLink="false">http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/</guid>
		<description><![CDATA[Ok, so I said I would keep up with this blogging thing, but I&#8217;m not doing a very good job. It&#8217;s been over two months since I last blogged. What the heck is my problem? So here&#8217;s the run-down of what I&#8217;ve been doing: Work I&#8217;ve been steadily plugging away at Master Packing&#8217;s web site [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so I said I would keep up with this blogging thing, but I&#8217;m not doing a very good job.  It&#8217;s been over two months since I last blogged.  What the heck is my problem?  So here&#8217;s the run-down of what I&#8217;ve been doing:</p>
<p><strong>Work</strong><br />
I&#8217;ve been steadily plugging away at Master Packing&#8217;s web site over the last two months.  I&#8217;ve been trying to implement interactive features to some fairly boring things.  Once its up, I&#8217;ll highlight what I&#8217;ve done, but until then, I&#8217;ve got to explain.  On the mechanical packing page, I&#8217;ve used the Tree widget from <a href="http://dojotoolkit.org">Dojo</a> to let you figure out which style of our mechanical packing matches other companies&#8217; packing.  Also, you can compare the temperature, pH, and speed ratings on all the mechanical packing we offer using the SortableTable widget.  On the rail and marine page, you&#8217;ll be able to search for a partial or complete EMD number and the server will show you what part number we sell that part under and also suitable replacements.  This search feature is something that isn&#8217;t available anywhere else in the EMD aftermarket industry.  We should have it up and running by mid April.  I&#8217;ll definately post when it&#8217;s up.</p>
<p><strong>JavaScript</strong><br />
For the last month, I&#8217;ve been delving deep into the world of JavaScript for SitePen.  It has taken me a while, but I think I&#8217;ve gotten the hang of the prototype-base inheritance that JavaScript uses.  I think I&#8217;m past the fact that you can extend the prototype of any class so if you want a new method or want to modify an old method, you don&#8217;t create a new class.  You just &#8220;mask&#8221; the old method on the current class.  Coming from C++, that blew my mind.</p>
<p><strong>Coaster</strong><br />
Development has pretty much stalled.  I&#8217;ve been so busy with life, music, Dojo, <a href="http://www.djangoproject.com">Django</a>, work, contracting, and everything else that I haven&#8217;t had time to sit down and code on <a href="http://www.coaster-burner.org">Coaster</a> for a long time.  Also, the success of programs like Serpentine have shown me that other people are more motivated than I am.  If someone wants to take the coding portion of the project over, contact me or Sean Harshbarger on the Coaster mailing list.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2006/01/21/updates-2/" title="Updates">Updates</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/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/2007/02/25/long-overdue/" title="Long Overdue">Long Overdue</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/01/29/publicity/" title="Publicity">Publicity</a></li><li><a href="http://www.reigndropsfall.net/2005/11/09/dork-sided/" title="Dork-sided">Dork-sided</a></li><li><a href="http://www.reigndropsfall.net/2005/09/13/catching-up/" title="Catching up">Catching up</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2006/04/01/my-current-endeavors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clarification</title>
		<link>http://www.reigndropsfall.net/2005/04/07/clarification/</link>
		<comments>http://www.reigndropsfall.net/2005/04/07/clarification/#comments</comments>
		<pubDate>Thu, 07 Apr 2005 21:57:47 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drivel]]></category>
		<category><![CDATA[moveable type]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;d like to point out that yes, I know that version 1.3.2 of Drivel supports the MovableType API. I checked out CVS head to do my hacking because 1.3.2 was sending integers for the blogid and postid and the MovableType API wants strings for those values. If your blog software accepts 1.3.2 posting to an [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to point out that yes, I know that version 1.3.2 of Drivel supports the <a href="http://www.sixapart.com/movabletype/docs/mtmanual_programmatic">MovableType API</a>.  I checked out CVS head to do my hacking because 1.3.2 was sending integers for the blogid and postid and the MovableType API wants strings for those values.  If your blog software accepts 1.3.2 posting to an mt blog, then its probably not adhering to the mt API which probably means that it is broken.  That is all <img src='http://www.reigndropsfall.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/" title="Drivel and Coaster">Drivel and Coaster</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2005/04/07/clarification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drivel and Coaster</title>
		<link>http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/</link>
		<comments>http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/#comments</comments>
		<pubDate>Thu, 07 Apr 2005 16:46:03 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[coaster]]></category>
		<category><![CDATA[drivel]]></category>
		<category><![CDATA[moveable type]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;ve hacked Drivel to work with the MovableType API (and I filed a bug as well) so hopefully I&#8217;ll be blogging more often. I continue to work on audio support in Coaster, so look for a release sometime in the next month! Related PostsClarificationMy current endeavorsUpdatesDork-sidedCatching upUpdatesOh the joy of technologyCoaster siteI can almost hear [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve hacked Drivel to work with the MovableType API (and I filed a bug as well) so hopefully I&#8217;ll be blogging more often.  I continue to work on audio support in <a href="http://www.coaster-burner.org">Coaster</a>, so look for a release sometime in the next month!</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.reigndropsfall.net/2005/04/07/clarification/" title="Clarification">Clarification</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><li><a href="http://www.reigndropsfall.net/2005/11/09/dork-sided/" title="Dork-sided">Dork-sided</a></li><li><a href="http://www.reigndropsfall.net/2005/09/13/catching-up/" title="Catching up">Catching up</a></li><li><a href="http://www.reigndropsfall.net/2005/08/07/updates/" title="Updates">Updates</a></li><li><a href="http://www.reigndropsfall.net/2005/07/14/oh-the-joy-of-technology/" title="Oh the joy of technology">Oh the joy of technology</a></li><li><a href="http://www.reigndropsfall.net/2005/03/24/coaster-site/" title="Coaster site">Coaster site</a></li><li><a href="http://www.reigndropsfall.net/2005/02/20/i-can-almost-hear-it/" title="I can almost hear it&#8230;">I can almost hear it&#8230;</a></li><li><a href="http://www.reigndropsfall.net/2005/02/08/not-quite-the-universe-and-everything/" title="Not Quite The Universe and Everything">Not Quite The Universe and Everything</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Coaster site</title>
		<link>http://www.reigndropsfall.net/2005/03/24/coaster-site/</link>
		<comments>http://www.reigndropsfall.net/2005/03/24/coaster-site/#comments</comments>
		<pubDate>Thu, 24 Mar 2005 21:50:34 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[coaster]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[The Coaster site is down. Sean forgot to renew our registrar stuff and the result is what you have now. For now, the Coaster tarball is located here. Sorry for the inconvenience. Related PostsMy current endeavorsUpdatesDork-sidedCatching upUpdatesOh the joy of technologyDrivel and CoasterI can almost hear it&#8230;Not Quite The Universe and EverythingA Load of Dingo&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.coaster-burn.org">Coaster</a> site is down.  Sean forgot to renew our registrar stuff and the result is what you have now.  For now, the Coaster tarball is located <a href="http://www.reigndropsfall.net/coaster/coaster-0.1.4.2.tar.gz">here.</a>  Sorry for the inconvenience.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><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><li><a href="http://www.reigndropsfall.net/2005/11/09/dork-sided/" title="Dork-sided">Dork-sided</a></li><li><a href="http://www.reigndropsfall.net/2005/09/13/catching-up/" title="Catching up">Catching up</a></li><li><a href="http://www.reigndropsfall.net/2005/08/07/updates/" title="Updates">Updates</a></li><li><a href="http://www.reigndropsfall.net/2005/07/14/oh-the-joy-of-technology/" title="Oh the joy of technology">Oh the joy of technology</a></li><li><a href="http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/" title="Drivel and Coaster">Drivel and Coaster</a></li><li><a href="http://www.reigndropsfall.net/2005/02/20/i-can-almost-hear-it/" title="I can almost hear it&#8230;">I can almost hear it&#8230;</a></li><li><a href="http://www.reigndropsfall.net/2005/02/08/not-quite-the-universe-and-everything/" title="Not Quite The Universe and Everything">Not Quite The Universe and Everything</a></li><li><a href="http://www.reigndropsfall.net/2005/01/30/a-load-of-dingos-kidneys/" title="A Load of Dingo&#8217;s Kidneys">A Load of Dingo&#8217;s Kidneys</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2005/03/24/coaster-site/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I can almost hear it&#8230;</title>
		<link>http://www.reigndropsfall.net/2005/02/20/i-can-almost-hear-it/</link>
		<comments>http://www.reigndropsfall.net/2005/02/20/i-can-almost-hear-it/#comments</comments>
		<pubDate>Sun, 20 Feb 2005 23:50:14 +0000</pubDate>
		<dc:creator>Bryan Forbes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[coaster]]></category>
		<category><![CDATA[gstreamer]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[This last week I started working on audio disc support in Coaster. It has been interesting trying to figure out the Gstreamer library and how to get it to interact well with Coaster without using a wrapping library (to save you guys the hassle of another binding dependency). So far, I have some test programs [...]]]></description>
			<content:encoded><![CDATA[<p>This last week I started working on audio disc support in <a href="http://www.coaster-burn.org">Coaster.</a>  It has been interesting trying to figure out the <a href="http://gstreamer.freedesktop.org">Gstreamer</a> library and how to get it to interact well with Coaster without using a wrapping library (to save you guys the hassle of another binding dependency).  So far, I have some test programs that read in the information I need; next thing to do is to get an audio store, layout, and view up and going.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><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><li><a href="http://www.reigndropsfall.net/2005/11/09/dork-sided/" title="Dork-sided">Dork-sided</a></li><li><a href="http://www.reigndropsfall.net/2005/09/13/catching-up/" title="Catching up">Catching up</a></li><li><a href="http://www.reigndropsfall.net/2005/08/07/updates/" title="Updates">Updates</a></li><li><a href="http://www.reigndropsfall.net/2005/07/14/oh-the-joy-of-technology/" title="Oh the joy of technology">Oh the joy of technology</a></li><li><a href="http://www.reigndropsfall.net/2005/04/07/drivel-and-coaster/" title="Drivel and Coaster">Drivel and Coaster</a></li><li><a href="http://www.reigndropsfall.net/2005/03/24/coaster-site/" title="Coaster site">Coaster site</a></li><li><a href="http://www.reigndropsfall.net/2005/02/08/not-quite-the-universe-and-everything/" title="Not Quite The Universe and Everything">Not Quite The Universe and Everything</a></li><li><a href="http://www.reigndropsfall.net/2005/01/30/a-load-of-dingos-kidneys/" title="A Load of Dingo&#8217;s Kidneys">A Load of Dingo&#8217;s Kidneys</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.reigndropsfall.net/2005/02/20/i-can-almost-hear-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

