with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

Event Capturing is near evil

Tuesday, May 15th, 2007

If you’re using a browser that implements the EventListener interface (W3 model), try this out for size

addEventListener useCapture

document.addEventListener('click', function(e) {
    e.stopPropagation();
    e.preventDefault();
}, true);

What do you think this will do? …Yeah, you’re right. If you have Firebug, copy the four lines above, then navigate to any webpage such as this one just for testing, open up your Firebug console command line, and paste it in there and run it. Now try clicking on any link in the document. Pretty sweet? Or almost near evil?

When Internet Explorer finally sports a W3 Event model, keep this little doozy in mind. I will admit, however, that there were times when I could have really used capturing, so don’t get the wrong impression. Cheers.

13 Responses to “Event Capturing is near evil”

  1. Daniel

    document.body.onclick = function() { return false; }

  2. Robert Otani

    I don’t know about “evil,” it seems like one of those things where you need to be “this tall” to be able to ride this roller coaster.

  3. NIkola

    This could be a great time saver, I really like it. But I see this in internet production at the earliest 2009. Some intranet applications however could make use of it earlier though.

  4. Wouldee

    Uh … for us idiots … can you tell me what it’s suppose to do?

    It doesn’t do anything as far as I can tell.

  5. andyduncan

    I guess it could be used for evil, but we use it (or various libraries’ versions of it) for “progressive enhancement” all the time. For example, a link that works as a link, unless you have javascript enabled, in which case it triggers some more fancy javascript code. As I’m typing this, I guess we could just return false, but that’s not always possible, and it would seem to have the same bad effects in many cases.

  6. jason

    The same thing happens if you set the parameter to true (bubble) and the anchor tag just has a normal href (no javascript event handler).

    Because with bubble the order is

    anchor handlers -> document handlers -> window handlers

    and the event stop is after the anchor click handlers have been executed.

  7. Anthony Ettinger

    If I’m not mistaken, running the code then clicking on a link does nothing?

    If that’s the case, then I agree with Robert’s statement about being “this tall” to use it.

  8. Dustin Diaz

    I think a few folks might have misunderstood the post. The point here is this. You can hijack (capture) all events and stop them from propagating at the document level. Comment #1 by Daniel isn’t even capable of doing this. If any elements had event listeners (or handlers) attached to them, they will not be dispatched. Eg:

    <a onclick="alert('hello');" href="http://www.google.com">Gooooooooooogle</a>
    <script>
        document.body.onclick = function() { return false; };
    </script>

    The above example will still alert “hello” when you click on the link. However, the code below, will not!

    <a onclick="alert('hello');" href="http://www.google.com">Gooooooooooogle</a>
    <script>
        document.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
        }, true);
    </script>

    Essentially meaning that once a higher level element (or object) sets “capture” to true, it receives first precedence over its children. Take that a step further and call stopPropagation() and you’ve now hijacked any other events from that same type of event (in this case ‘click’).

  9. Dustin Diaz

    Anthony, I agree with Roberts comment as well. It is more or less a feature that should be used with caution (same goes for eval()). This example really has nothing to do with how links are not followed… but rather an example event capturing and propagation being stopped at a high level. If you had ‘click’ events assigned to an inner <div> element, but captured the ‘click’ event to an outer <div> element, the events assigned to the inner will not fire.

  10. Nicholas

    Don’t forget about IE’s setCapture() method. Not quite the same as the DOM event capturing, but can be just as evil.

    All your clicks are belong to us!

  11. Robert Otani

    This also goes back to having to trust your source. For example, a not-so-cautious web developer may insert an external .js reference into his html. The JS may contain such an event capture method, and walla, she can track all sorts of behavior (or worse) on the victim’s site without knowing about it. Sounds evil to me, but again it brings things back to being “tall enough” to go on the ride…

  12. Harvey

    This is all fine and cool stuff but what about capturing the onBlur of a Div tag? For that matter what about adding my own custom events? Any ideas…

  13. big fan

    Newbie alert:

    How does one run the code once pasted into the console?

    Perhaps someone can explain what it supposed to happen with and without the code?

    Thanks!

Leave a Reply

Phone Number:

If you're about to post code in your comment, please wrap your code with the tag-combo <pre><code>. Also please escape your html entities - otherwise they will be stripped out. I recommend using postable.

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

Flickr

Submit a Prototype

All content copyright © 2003 - 2007 under the Creative Commons License. Wanna know something? Just ask.

About | Archives | Blog Search

[x] close

Loading...

Submit a prototype

By checking this prototype I agree that I am not submitting false credentials, pornography, or a hate crime website. I also understand that by submitting my entry I may or may not be accepted, and if accepted, my entry may be taken down at any given time if I violate these terms.