04.11.2015 Views

javascript

Create successful ePaper yourself

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

Chapter 12: Events<br />

value of the node, and newValue , which is the new value for the node. After that, the<br />

DOMSubtreeModified event fires on the text node that was changed. Consider the following<br />

HTML page:<br />

< html ><br />

< head ><br />

< title > Text Change Events Example < /title ><br />

< /head ><br />

< body ><br />

< div id=”myDiv” > Hello world! < /div ><br />

< /body ><br />

< /html ><br />

If you change the text contained in the < div > element, the DOMCharacterDataModified event fires on<br />

the text node containing “ Hello world! ” . Then the DOMSubtreeModified event fires on the text node.<br />

The following code illustrates this:<br />

EventUtil.addHandler(window, “load”, function(event){<br />

var div = document.getElementById(“myDiv”);<br />

EventUtil.addHandler(document, “DOMSubtreeModified”, function(event){<br />

alert(event.type);<br />

alert(event.target);<br />

});<br />

EventUtil.addHandler(document, “DOMCharacterDataModified”, function(event){<br />

alert(event.type);<br />

alert(event.target);<br />

alert(event.prevValue); //”Hello world!”<br />

alert(event.newValue); //”Some new text”<br />

});<br />

});<br />

div.firstChild.nodeValue = “Some new text”;<br />

Since both DOMCharacterDataModified and DOMSubtreeModified events bubble, the event handlers<br />

are attached to the document . The DOMCharacterDataModified event fires immediately after the text<br />

value has changed. On the event object, prevValue is “ Hello world! ” and newValue is “ Some new<br />

text ” . After that, DOMSubtreeModified fires on the text node that just changed.<br />

Proprietary Events<br />

The DOM specification doesn ’ t cover all events that are supported by all browsers. Many browsers have<br />

implemented custom events for various purposes either based on user need or a specific use case. These<br />

events may not be supported across all browsers but are useful enough to mention.<br />

The contextmenu Event<br />

Windows 95 introduced the concept of context menus to PC users via a right mouse click. Soon, that<br />

paradigm was being mimicked on the Web. The problem developers were facing was how to detect that<br />

a context menu should be displayed (in Windows, it ’ s a right - click; on a Mac, it ’ s a Ctrl+click), and then<br />

how to avoid the default context menu for the action. This resulted in the introduction of the<br />

407

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

Saved successfully!

Ooh no, something went wrong!