<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: A Good Enough addEvent</title>
	<atom:link href="http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/</link>
	<description>A blog about philosophy, Christianity, web development and whatever else I feel like writing about.</description>
	<pubDate>Sun, 27 Jul 2008 01:11:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Tino Zijdel</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197597</link>
		<dc:creator>Tino Zijdel</dc:creator>
		<pubDate>Fri, 18 Apr 2008 21:50:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197597</guid>
		<description>&lt;em&gt;It’s rare that one developer “owns” the window.onload event.&lt;em&gt;

No-one should 'own' that event in such circumstances; you'll need global conventions such as 'onload events should be registered using this-and-such interface'.

I was just trying to illustrate that even though it is good that &lt;em&gt;you&lt;/em&gt; try to play nice, other scripts might not and can screw up yours (or each other) anyway.

My more general 'criticism' is about the 'good enough' part of this article: I argued that for most people your original addEvent wrapper might still be 'good enough' and even just using object.onevent=function is 'good enough' in most cases. It just all depends on what you think is important and on what you actually need. You just need to be aware of the limitations of the solution you implement, and in my humble opinion most people are just not knowledgeable enough for that.

In general I think that 'brevity' should not be such a heavy factor when evaluating cross-browser eventhandling solutions, especially not if you already include several KB of script for other generic purposes.</description>
		<content:encoded><![CDATA[<p><em>It’s rare that one developer “owns” the window.onload event.</em><em></em></p>
<p>No-one should &#8216;own&#8217; that event in such circumstances; you&#8217;ll need global conventions such as &#8216;onload events should be registered using this-and-such interface&#8217;.</p>
<p>I was just trying to illustrate that even though it is good that <em>you</em> try to play nice, other scripts might not and can screw up yours (or each other) anyway.</p>
<p>My more general &#8216;criticism&#8217; is about the &#8216;good enough&#8217; part of this article: I argued that for most people your original addEvent wrapper might still be &#8216;good enough&#8217; and even just using object.onevent=function is &#8216;good enough&#8217; in most cases. It just all depends on what you think is important and on what you actually need. You just need to be aware of the limitations of the solution you implement, and in my humble opinion most people are just not knowledgeable enough for that.</p>
<p>In general I think that &#8216;brevity&#8217; should not be such a heavy factor when evaluating cross-browser eventhandling solutions, especially not if you already include several KB of script for other generic purposes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: scottandrew</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197541</link>
		<dc:creator>scottandrew</dc:creator>
		<pubDate>Fri, 18 Apr 2008 20:03:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197541</guid>
		<description>&lt;em&gt;I think it’s a bad sign if you don’t know what other scripts are doing on your page&lt;/em&gt;

Try working on a site like where multiple departments with multiple developers own discrete sections of a large website. It's rare that one developer "owns" the window.onload event.

Being able to overload event handlers safely is far more valuable than the ability to detach handler functions. If you're attaching so many handlers that you have to detach them to prevent leakage, I'd say that's a poor design to begin with.</description>
		<content:encoded><![CDATA[<p><em>I think it’s a bad sign if you don’t know what other scripts are doing on your page</em></p>
<p>Try working on a site like where multiple departments with multiple developers own discrete sections of a large website. It&#8217;s rare that one developer &#8220;owns&#8221; the window.onload event.</p>
<p>Being able to overload event handlers safely is far more valuable than the ability to detach handler functions. If you&#8217;re attaching so many handlers that you have to detach them to prevent leakage, I&#8217;d say that&#8217;s a poor design to begin with.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tino Zijdel</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197370</link>
		<dc:creator>Tino Zijdel</dc:creator>
		<pubDate>Fri, 18 Apr 2008 06:56:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197370</guid>
		<description>I think it's a bad sign if you don't know what other scripts are doing on your page (they just as well might overwrite your addEvent function!) ;)

Besides that I personally never use window.onload; I have a DomLoaded class (in my toolbox) and register onload functions to that using DomLoaded.addLoadEvent()</description>
		<content:encoded><![CDATA[<p>I think it&#8217;s a bad sign if you don&#8217;t know what other scripts are doing on your page (they just as well might overwrite your addEvent function!) <img src='http://www.ilfilosofo.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Besides that I personally never use window.onload; I have a DomLoaded class (in my toolbox) and register onload functions to that using DomLoaded.addLoadEvent()</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dustin Diaz</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197359</link>
		<dc:creator>Dustin Diaz</dc:creator>
		<pubDate>Fri, 18 Apr 2008 03:54:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197359</guid>
		<description>This is good and makes perfect sense. I also came up with a very short and concise addEvent in my article to &lt;a href="http://www.dustindiaz.com/javascript-no-no/" rel="nofollow"&gt;write better JavaScript&lt;/a&gt;. It even does a simple branch so you're not always detecting the object in question (W3 vs IE).
All in all, you make a good case. And I too rarely ever use removeEvent... it seems like a waste of time for smaller websites :)</description>
		<content:encoded><![CDATA[<p>This is good and makes perfect sense. I also came up with a very short and concise addEvent in my article to <a href="http://www.dustindiaz.com/javascript-no-no/" rel="nofollow">write better JavaScript</a>. It even does a simple branch so you&#8217;re not always detecting the object in question (W3 vs IE).<br />
All in all, you make a good case. And I too rarely ever use removeEvent&#8230; it seems like a waste of time for smaller websites <img src='http://www.ilfilosofo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: filosofo</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197339</link>
		<dc:creator>filosofo</dc:creator>
		<pubDate>Fri, 18 Apr 2008 00:27:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197339</guid>
		<description>I guess I have another, tacit criterion: I like to know that my script won't interfere with or be interfered by other scripts. 
 If you do something like the following

&lt;code&gt;window.onload = function1;&lt;/code&gt;
&lt;code&gt;window.onload = function2;&lt;/code&gt;

&lt;code&gt;function2&lt;/code&gt; will override &lt;code&gt;function1&lt;/code&gt;, but
 
&lt;code&gt;addEvent(window, 'load', function1 );&lt;/code&gt;
&lt;code&gt;addEvent(window, 'load', function2 );&lt;/code&gt;

works fine.</description>
		<content:encoded><![CDATA[<p>I guess I have another, tacit criterion: I like to know that my script won&#8217;t interfere with or be interfered by other scripts.<br />
 If you do something like the following</p>
<p><code>window.onload = function1;</code><br />
<code>window.onload = function2;</code></p>
<p><code>function2</code> will override <code>function1</code>, but</p>
<p><code>addEvent(window, 'load', function1 );</code><br />
<code>addEvent(window, 'load', function2 );</code></p>
<p>works fine.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tino Zijdel</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197327</link>
		<dc:creator>Tino Zijdel</dc:creator>
		<pubDate>Thu, 17 Apr 2008 20:45:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197327</guid>
		<description>What I meant is: even when you create websites enhanced with JS behaviour but don't use any of the big frameworks/libraries such as jQuery or prototype you'll usually have some sort of script with utilities (a 'toolbox' of some sort) that you often use and that contains more than a generic function for adding events. My 'toolbox' contains f.i. a light-weight Ajax class, class-dealing functions, cross-browser Array extra's/generics etcetera.

If you only want to add an eventhandler to some object why use this solution instead of just doing object.onclick=handler - that also 'fixes' the 'this'-issue and by your own reasoning: how often do you find yourself needing to add multiple handlers to the same event on the same object? ;)

If you insist on a generic function for that, here it is:

&lt;code&gt;var addEvent = function( obj, type, fn ) {
	obj['on'+type] = fn;
}&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>What I meant is: even when you create websites enhanced with JS behaviour but don&#8217;t use any of the big frameworks/libraries such as jQuery or prototype you&#8217;ll usually have some sort of script with utilities (a &#8216;toolbox&#8217; of some sort) that you often use and that contains more than a generic function for adding events. My &#8216;toolbox&#8217; contains f.i. a light-weight Ajax class, class-dealing functions, cross-browser Array extra&#8217;s/generics etcetera.</p>
<p>If you only want to add an eventhandler to some object why use this solution instead of just doing object.onclick=handler - that also &#8216;fixes&#8217; the &#8216;this&#8217;-issue and by your own reasoning: how often do you find yourself needing to add multiple handlers to the same event on the same object? <img src='http://www.ilfilosofo.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>If you insist on a generic function for that, here it is:</p>
<p><code>var addEvent = function( obj, type, fn ) {<br />
	obj['on'+type] = fn;<br />
}</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: filosofo</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197322</link>
		<dc:creator>filosofo</dc:creator>
		<pubDate>Thu, 17 Apr 2008 19:14:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197322</guid>
		<description>@molily: I don't know why I didn't think of that.

@Tino Zijdel: It depends on what you mean by a library of "several" kilobytes.  Most of the time I just want to run some code when the window has loaded or attach a click event, and then it's "good enough"--no need to add kilobytes.</description>
		<content:encoded><![CDATA[<p>@molily: I don&#8217;t know why I didn&#8217;t think of that.</p>
<p>@Tino Zijdel: It depends on what you mean by a library of &#8220;several&#8221; kilobytes.  Most of the time I just want to run some code when the window has loaded or attach a click event, and then it&#8217;s &#8220;good enough&#8221;&#8211;no need to add kilobytes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tino Zijdel</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197302</link>
		<dc:creator>Tino Zijdel</dc:creator>
		<pubDate>Thu, 17 Apr 2008 11:24:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197302</guid>
		<description>One other point that this solution does not fix, since it is still a wrapper around MSIE's propriety (and sub-optimal) eventmodel, is the fact that addEventListener will ignore it when you try to add the same handler to the same event on the same object. attachEvent doesn't.

This solution does suffice for most cases though, but then Scott Andrews original addEvent also worked for most apart from the scope-issue.

In some cases however correctness is more important than brevity, and if you already include a library that's several kilobytes in size than I prefer a solution that's more correct and flexible than one that is only short and may backfire on me.

And all things considered you can count yourself lucky if you don't have to support browsers that support neither the W3C DOM2 eventmodel nor MSIE's eventmodel ;)</description>
		<content:encoded><![CDATA[<p>One other point that this solution does not fix, since it is still a wrapper around MSIE&#8217;s propriety (and sub-optimal) eventmodel, is the fact that addEventListener will ignore it when you try to add the same handler to the same event on the same object. attachEvent doesn&#8217;t.</p>
<p>This solution does suffice for most cases though, but then Scott Andrews original addEvent also worked for most apart from the scope-issue.</p>
<p>In some cases however correctness is more important than brevity, and if you already include a library that&#8217;s several kilobytes in size than I prefer a solution that&#8217;s more correct and flexible than one that is only short and may backfire on me.</p>
<p>And all things considered you can count yourself lucky if you don&#8217;t have to support browsers that support neither the W3C DOM2 eventmodel nor MSIE&#8217;s eventmodel <img src='http://www.ilfilosofo.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javascript News &#187; Blog Archive &#187; A good enough addEvent</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197262</link>
		<dc:creator>Javascript News &#187; Blog Archive &#187; A good enough addEvent</dc:creator>
		<pubDate>Wed, 16 Apr 2008 23:54:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197262</guid>
		<description>[...] days. But before it truly shuffles off into the sunset, allow me to point you to filosofo&#8217;s &#8220;good enough&#8221; addEvent. Austin gets around PPK&#8217;s pesky requirements by ignoring one of them (namely, having a [...]</description>
		<content:encoded><![CDATA[<p>[...] days. But before it truly shuffles off into the sunset, allow me to point you to filosofo&#8217;s &#8220;good enough&#8221; addEvent. Austin gets around PPK&#8217;s pesky requirements by ignoring one of them (namely, having a [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: molily</title>
		<link>http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/#comment-197260</link>
		<dc:creator>molily</dc:creator>
		<pubDate>Wed, 16 Apr 2008 23:32:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.ilfilosofo.com/?p=441#comment-197260</guid>
		<description>The slightly shorter &lt;code&gt;fn.call(obj, window.event)&lt;/code&gt; should suffice.</description>
		<content:encoded><![CDATA[<p>The slightly shorter <code>fn.call(obj, window.event)</code> should suffice.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
