04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12: Events<br />

All modern browsers support event bubbling, although there are some variations on how it is<br />

implemented. IE 5.5 and earlier skip bubbling to the < html > element (going from < body > directly to<br />

document). Firefox, Chrome, and Safari continue event bubbling up to the window object.<br />

Event Capturing<br />

The Netscape Communicator team came up with an alternate event flow called event capturing . The<br />

theory of event capturing is that the least specific node should receive the event first and the most<br />

specific node should receive the event last. Event capturing was really designed to intercept the event<br />

before it reached the intended target. If the previous example is used with event capturing, clicking the<br />

< div > element fires the click event in the following order:<br />

1. document<br />

2. < html ><br />

3. < body ><br />

4. < div ><br />

With event capturing, the click event is first received by the document and then continues down the<br />

DOM tree to the actual target of the event, the < div > element. This flow is illustrated in Figure 12 - 2 .<br />

Document<br />

1<br />

Element html<br />

2<br />

Element body<br />

3<br />

Element div 4<br />

Figure 12-2<br />

Although this was Netscape Communicator ’ s only event flow model, event capturing is currently<br />

supported in Safari, Chrome, Opera, and Firefox. All three actually begin event capturing at the<br />

window - level event despite the fact that the DOM Level 2 Events specification indicates that the events<br />

should begin at document .<br />

DOM Event Flow<br />

The event flow specified by DOM Level 2 Events has three phases: the event capturing phase, at the<br />

target, and the event bubbling phase. Event capturing occurs first, providing the opportunity to intercept<br />

events if necessary. Next, the actual target receives the event. The final phase is bubbling, which allows a<br />

final response to the event. Considering the simple HTML example used previously, clicking the < div ><br />

fires the event in the order indicated in Figure 12 - 3 .<br />

367

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

Saved successfully!

Ooh no, something went wrong!