11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Preventing Bubbling<br />

You can stop events from propagating up the hierarchy by setting the cancelBubble property<br />

of the Event object. This property is false by default, meaning that after a handler is finished<br />

with the event, it will continue on its way up to the next enclosing object in the hierarchy. Setting<br />

the cancelBubble property to true prevents further bubbling after the current handler has<br />

finished. For example, you could prevent the event from getting beyond the tag in the<br />

last example by making this small modification:<br />

...BOLD<br />

TEXT...<br />

<strong>The</strong> result of clicking on the bold text after this change is made is shown in Figure 11-2.<br />

Figure 11-2: If an event is cancelable, setting event.cancelBubble prevents the event from<br />

propagating.<br />

Not all events are cancelable; Table 11-9 indicates whether each event can be canceled in this<br />

way.<br />

It is important to keep in mind that returning false from a handler (or setting event.returnValue<br />

to false) prevents the default action for the event, but does not cancel bubbling. Later handlers<br />

invoked by the bubbling behavior will still have a chance to handle the event, and any value<br />

they return (or set event.returnValue to) will ―overwrite‖ the value set or returned by a previous<br />

handler.<br />

Conversely, canceling bubbling does not affect the event‘s return value. Because the default<br />

returnValue of an event is true, you need to be sure to return false or set returnValue to<br />

false if you wish to prevent the event‘s default action.<br />

Imitating Netscape’s Event Capture<br />

Because all parts of the page are scriptable in IE4+, performing event captures as in Netscape<br />

4 is very easy. Simply set the handler at the appropriate level in the hierarchy, for example, to<br />

capture clicks at the Document level with the function myHandler:<br />

document.onclick = myHandler;<br />

and omit Click handlers from lower objects. To unset event capture, simply set the appropriate<br />

handler to null, for example, to turn off click capturing at the Document level:<br />

document.onclick = null;<br />

Event Routing<br />

Events bubble up strictly through objects in the hierarchy that contain them. <strong>The</strong>re is, however,<br />

a primitive way to redirect to another object in Internet Explorer 5.5+. Each object has a<br />

fireEvent() method that transfers the event to the object on which it is invoked:<br />

object.fireEvent(“event to fire“ [, eventObject])<br />

<strong>The</strong> first argument is a string denoting the handler to fire, for example, "onclick". <strong>The</strong> optional<br />

eventObject parameter is the Event object from which the new Event object will be created. If<br />

eventObject is not given, a brand new Event is created and initialized as if the event had really<br />

occurred on the target object. If eventObject is specified, its properties are copied into the new

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!