from Hacker News

JavaScript useCapture demystified

by benhowdle89 on 3/4/13, 9:34 AM with 6 comments

  • by jonchang on 3/4/13, 10:36 AM

    quirksmode has more historical background and actually explains what happens during capture and bubble and when you might want to use it.

    http://www.quirksmode.org/js/events_order.html

  • by jdlshore on 3/4/13, 5:55 PM

    "When an event is attached to an element, it will fire once the event has fully bubbled up to the document."

    This is not quite correct. The event will fire as it bubbles upward, not after it reaches the document level.

    The DOM Events Level 3 specification has a helpful diagram:

    http://www.w3.org/TR/DOM-Level-3-Events/#event-flow

    There's also the "browser default" behavior, which is stuff like highlighting selected text. That does happen after the event finishes bubbling. (Usually.)

    Note that stopping an event from bubbling (with `event.stopPropagation()`) and stopping an event's default behavior (with `event.preventDefault()`) are two separate, unrelated actions. However, returning false from an event handler is equivalent to doing both.

  • by benhowdle89 on 3/4/13, 10:05 AM

    So far, feedback has been:

    1. maybe an initial syntax overview: target.addEventListener(type, listener[, useCapture]);

    2. I think it’d be worth explaining what capture and bubble are.

    I agree!