|
JavaScript. The Definitive Guide Authors: Flanagan D. Published year: 2004 Pages: 164-165/767 |
17.6. The onload EventJavaScript code that modifies the document in which it is contained must typically run after the document is fully loaded (this is discussed in further detail in Section 13.5.7.). Web browsers fire an onload event on the Window object when document loading is complete, and this event is commonly used to trigger code that needs access to the complete document. When your web page includes multiple independent modules that need to run code in response to the onload event, you may find a cross-platform utility function like the one shown in Example 17-7 to be useful. Example 17-7. Portable event registration for onload event handlers
|
17.7. Synthetic EventsBoth the DOM Level 2 event model and the IE event model allow you to create synthetic event objects and dispatch them to event handlers registered on document elements. In essence, this is a technique for tricking browsers into invoking the event handlers registered on an element (and, in the case of bubbling events, the handlers registered on the ancestors of the element). With the Level 0 event model, synthetic events are not necessary because event handlers are available through the various event handler properties. In the advanced event models, however, there is no way to query the set of handlers registered with addEventListener or attachEvent , and those handlers can only be invoked using the techniques demonstrated in this section. In the DOM event model, you create a synthetic event with Document.createEvent( ) , initialize that event with Event.initEvent( ) , UIEvent.initUIEvent( ) , or MouseEvent.initMouseEvent( ) and then dispatch the event with the dispatchEvent method of the node to which it is to be dispatched. In IE, you create a new event object with Document.createEventObject and then dispatch it with the fireEvent( ) method of the target element. Example 17-8 demonstrates these methods . It defines a cross-platform function for dispatching synthetic dataavailable events and a function for registering event handlers for events of that type. It is important to understand that synthetic events dispatched with dispatchEvent( ) and fireEvent( ) are not queued and handled asynchronously. Instead, they are dispatched immediately, and their handlers are invoked synchronously before the call to dispatchEvent( ) or fireEvent( ) returns. This means that dispatching a synthetic event is not a technique for deferring execution of code until the browser has handled all pending events. For that, it is necessary to call setTimeout( ) with a timeout value of 0 milliseconds . It is possible to synthesize and dispatch low-level raw events such as mouse events, but it is not well specified how document elements respond to these events. It is typically more useful to use this functionality for higher-level semantic events to which the browser does not have a default response. This is why Example 17-8 uses the dataavailable event type. Example 17-8. Dispatching synthetic events
|
|
JavaScript. The Definitive Guide Authors: Flanagan D. Published year: 2004 Pages: 164-165/767 |
![]() Programming PHP | ![]() JavaScript: The Good Parts | ![]() CSS: The Definitive Guide | ![]() JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides) | ![]() JavaScript Patterns |
![]() Programming PHP | ![]() JavaScript: The Good Parts |
![]() CSS: The Definitive Guide | ![]() JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides) |
![]() JavaScript Patterns |