The NS6+ event ObjectThe event object used in Netscape Navigator 6 is based on the W3C DOM level 2 event object, with a number of properties thrown in from Netscape Navigator 4. Now that W3C is in the act, we'll see more sanity in event handling in the future. You can see the properties of the event object used in Netscape Navigator 6 in Table 15.7, and its methods in Table 15.8. Table 15.7. The Properties of the NS6 event Object
Table 15.8. The Methods of the NS6 event Object
In Netscape Navigator 6, events bubble by default, just as in the Internet Explorer (and unlike Netscape Navigator 4). You also can use the addEventListener method (see the sections "Handling Events in the W3C DOM" and "The addEventListener Method" in Chapter 6) to add event listeners for specific events to objects of your choice and so configure how events are routed in your code. Here's an example. In this case, I'll add onclick event handlers to the window , document , body , and button objects in this code that will indicate whether the event has been captured by a listener or is bubbling. Then I'll add click listeners to the document and form objects, in that order. Here's what the code looks like: (Listing 15-15.html on the web site) <HTML> <HEAD> <TITLE>W3C Capture and Bubbling</TITLE> </HEAD> <BODY> <H1>W3C Capture and Bubbling</H1> <FORM NAME="form1"> <INPUT TYPE="BUTTON" VALUE="Click Me!" ONCLICK="buttonClick(event)"> </FORM> <SCRIPT LANGUAGE="JavaScript"> <!-- window.onclick = windowClick document.onclick = documentClick document.body.onclick = bodyClick document.addEventListener("click", documentClick, true) document.form1.addEventListener("click", formCapture, true) document.form1.addEventListener("click", formBubble, false) function buttonClick(e) { switch (e.eventPhase) { case 1: text = "Captured" break case 2: text = "Seen" break case 3: text = "Bubbling" break } alert(text + " at the button level.") } function windowClick(e) { switch (e.eventPhase) { case 1: text = "Captured" break case 2: text = "Seen" break case 3: text = "Bubbling" break } alert(text + " at the window level.") } function documentClick(e) { switch (e.eventPhase) { case 1: text = "Captured" break case 2: text = "Seen" break case 3: text = "Bubbling" break } alert(text + " at the document level.") } function bodyClick(e) { switch (e.eventPhase) { case 1: text = "Captured" break case 2: text = "Seen" break case 3: text = "Bubbling" break } alert(text + " at the body level.") } function formCapture(e) { alert("Captured at the form level.") } function formBubble(e) { alert("Bubbling at the form level.") } //--> </SCRIPT> </BODY> </HTML> When you click the button in this example, you can watch the progress of the event with alert boxes. The event is first captured by the document and form objects because of the event listeners we've added to those objects, and then bubbles from the button up through the form , body , document , window objects. You can see this example at work in Figure 15.13. Figure 15.13. Event handling in Netscape Navigator 6. As you can see, you can use event bubbling in Netscape Navigator 6, as well as event listeners to track events as you want. And that's it for this chapter. You've seen a lot of JavaScript, including the mouse, keyboard, images, and in-depth event handling. In the next chapter, we'll start taking a look at another favorite JavaScript topic: Dynamic HTML. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||