<?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>Austin Matzko&#039;s Blog &#187; addEvent</title>
	<atom:link href="http://austinmatzko.com/tag/addevent/feed/" rel="self" type="application/rss+xml" />
	<link>http://austinmatzko.com</link>
	<description>A blog about philosophy, Christianity, web development and whatever else I feel like writing about.</description>
	<lastBuildDate>Wed, 16 Mar 2011 17:14:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2-RC4-18391</generator>
		<item>
		<title>A Good Enough addEvent</title>
		<link>http://austinmatzko.com/2008/04/14/addevent-preserving-this/</link>
		<comments>http://austinmatzko.com/2008/04/14/addevent-preserving-this/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 03:48:00 +0000</pubDate>
		<dc:creator>filosofo</dc:creator>
				<category><![CDATA[Web development]]></category>
		<category><![CDATA[addEvent]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[attachEvent]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441</guid>
		<description><![CDATA[Several years ago, PPK of Quirksmode sponsored a contest to come up with a new version of the trusty JavaScript addEvent function. The original addEvent was created by Scott Andrew LePera in 2001 as a way to merge Internet Explorer&#8217;s attachEvent with the W3C&#8217;s addEventListener. Both addEventListener and attachEvent allow you to attach a JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>Several years ago, PPK of Quirksmode sponsored <a href="http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html">a contest to come up with a new version of the trusty JavaScript addEvent function</a>.  The original addEvent was <a href="http://www.scottandrew.com/weblog/articles/cbs-events">created by Scott Andrew LePera in 2001</a> as a way to merge Internet Explorer&#8217;s <code>attachEvent</code> with the W3C&#8217;s <code>addEventListener</code>.  Both <code>addEventListener</code> and <code>attachEvent</code> allow you to attach a JavaScript event to a DOM object, but they differ in important ways.  In particular, IE&#8217;s <code>attachEvent</code> doesn&#8217;t maintain the scope of the <code>this</code> keyword: <code>this</code> refers to the <code>window</code> object instead of the object on which you&#8217;re attaching the event, as in the case of <code>addEventListener</code>.  </p>
<p>PPK&#8217;s contest itself ended up falling flat, as even the winner, John Resig (who later created the <a href="http://jquery.com/">jQuery library</a>),  later <a href="http://my.opera.com/hallvors/blog/2007/03/28/a-problem-with-john-resigs-addevent#comment2703457">repudiated it himself</a>. That&#8217;s probably because PPK&#8217;s contest requirements were like asking for all three of good, fast, and cheap.</p>
<p>So seven years later, there&#8217;s no widely-adopted replacement to the original addEvent that:</p>
<ol>
<li>Is short</li>
<li>Maintains the <code>this</code> scope in IE</li>
<li>Has a corresponding removeEvent</li>
</ol>
<p>The various libraries do a good job of 2 and 3, but not 1, and since I often find myself needing 1 and 2 but not 3, I came up with my own good-enough version of addEvent:</p>
<div class="filosofo-highlight-light javascript" style="font-family: monospace;"><span style="color: #003366; font-weight: bold;">var</span> addEvent = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span> obj, type, fn <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>obj.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>type, fn, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>obj.<span style="color: #006600;">attachEvent</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span style="color: #006600;">attachEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'on'</span> + type, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> fn.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>obj, <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #66cc66;">&#40;</span>window.<span style="color: #006600;">event</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
&nbsp;</div>
<p>It&#8217;s short, which is just what I need when I&#8217;m trying to keep the JavaScript size low.  (Whenever size isn&#8217;t so much of an issue, such as on the administrative side of a website, I&#8217;m more likely to use a library which will have a much more robust way of assigning events to objects.)  And my addEvent also makes <code>this</code> refer to object in question, even for IE.  </p>
]]></content:encoded>
			<wfw:commentRss>http://austinmatzko.com/2008/04/14/addevent-preserving-this/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
