captures events by mistake

This is a snippet from a cut-and-paste JavaScript function that has been incorporated in dozens of websites, it is often said to be written by Scott Andrew LePera though I have not found this exact version on his site:

function addEvent(obj, evType, fn)


if (obj.addEventListener)


obj.addEventListener(evType, fn, true);

return true;



This addEvent function is not entirely standards-compliant. It will work for now in FireFox but once they fix <> it will not work reliably in FireFox anymore. It quite often does not do what the webmaster wants in Opera, which supports the standards correctly.

The issue is that this function sets up a "capturing" event handler by using "true" as the third argument to the addEventListener function. That true should be false. Using "true" has two major side effects that are probably not intended:

1) A capturing "load" event listener to the window object will capture every "load" event in the document, including any IMG load events. In other words, the event handler will fire many more times than expected.

2) Capturing event handlers are not supposed to fire for events directed at the target itself. So a capturing event handler on an A element with no elements inside, or an IMG element (which can not contain elements) will never do anything at all. is one of the sites that has this problem. I have written to them but I thought the title of this post was amusing enough to blog 🙂


One thought on “ captures events by mistake

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s