Friday, February 2nd, 2007
Stealing Events via Capture
Are you the kind of person who has always written document.addEventListener("keypress", someFunc, false) and never really looked into the false bit. You just always keep it as false as that is how you copied it from some example the first time?
Hallvord R. M. Steen explains what happens when you go true and capture events:
If you call addEventListener with true as the third argument you create a capturing event. The difference from a normal event is that the capturing listener detects all events in the document before they are sent to the actual target of the event.












I don’t believe IE’s attachEvent supports capture (correct me if I am wrong). While capturing is a nice capability of most browsers, without capturing available on IE, capturing isn’t really on option for cross browser apps, hence its lack of use.
On IE, you have to use setCapture/releaseCapture in order to capture mouse events (only mouse events can be captured on IE)
He makes a good attempt at explaining capturing, but the confusing use of “events” versus “event listeners” in the article is problematic. There is only one type of event but there are two ways to listen to it (on the capturing phase and on the bubbling phase).
The implementations of capture are (shock horror) different in IE from Firefox as well so they don’t quite do the same thing. I believe that in IE the event is captured no matter what DOM element it occurs on whereas in Firefox it obeys W3C and only captures events in the ancestor path of the DOM element on which the event occured.