6.5.2. W3C Event Capture
Although event bubbling is the default mechanism in modern browsers,
a propagating event in Netscape 6 or later starts its life by
trickling down to the target. If you place an event listener for the
event's type at a higher level, however, it will
ignore the event as it trickles down unless event capture for that
element and that event type is turned on.
To engage event capture, use the same
addEventListener( ) method that the W3C event
model prefers for event binding. The third parameter is a Boolean
value that controls event capture. When you set the parameter to
true, the element invokes the listener function
during capture phase; after that function completes its task, the
event continues its journey toward to the target element. Therefore,
it is perfectly "legal" to add two
separate event listeners to an element for the same event type. In
capture phase, one listener function runs; in bubbling phase, another
listener function runs.
Using the same three parameters, you can
eliminate the event listener for the desired propagation phase with
the removeEventListener( ) method. Thus, you could
temporarily engage capture-phase processing and remove it without
disturbing bubbling-phase event processing for the same element and
event type.
At any point along W3C event
propagation, you can prevent the event from going any further by
invoking the event object's
stopPropagation( ) method in a statement inside
the listener function. Netscape 6 or later also wires the convenience
cancelBubble property to work during capture phase
as well.
The W3C root event object (and therefore event
objects of all types) implements a handful of other properties that
may be helpful while processing events within the two-way propagation
model. Table 6-4 lists these properties, for which
IE (through Version 6 on Windows) provides no analogues. A shared
event listener function might use these (and other properties) to
build code branches that execute when the desired combination of
conditions exist (e.g., when the target's class name
is "foo" and the event is being
processed from a container of several elements of the same
class).
Table 6-4. W3C event object propagation properties
Property
|
Description
|
bubbles
|
Boolean true if event can bubble
|
currentTarget
|
Reference to the node whose event listener invoked the current
listener function
|
eventPhase
|
Integer indicating in which phase the event listener is processing
(1 is capture; 2 is at target;
3 is bubbling)
|