Friday, February 2nd, 2007

Stealing Events via Capture

Category: Tip

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.

Posted by Dion Almaer at 2:31 am

3.8 rating from 26 votes


Comments feed TrackBack URI

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.

Comment by Ajax 2.0 Developer — February 2, 2007

On IE, you have to use setCapture/releaseCapture in order to capture mouse events (only mouse events can be captured on IE)

Comment by Julien Lecomte — February 2, 2007

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).

Comment by Nicholas C. Zakas — February 2, 2007

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.

Comment by Dave Johnson — February 6, 2007

Leave a comment

You must be logged in to post a comment.