The Navigator Event Model: Processing Captured Events - Doc JavaScript | WebReference

The Navigator Event Model: Processing Captured Events - Doc JavaScript


Processing Captured Events

The captureEvents() method doesn't have any effect until you assign a function to handle the desired events. To capture all click events on a page, for example, you would need to define a function that handles the event, and then assign the function reference to the event handler. Here's an example:

function processClicks(e) {
  // statements to handle the click events
}
window.captureEvents(Event.CLICK); // click is an event
window.onclick = processClicks; // onclick is an event handler

Without specifying a function to handle click events at the window level, all click events would simply be captured at that level, but would not have any influence because a captured event does not proceed to the intended target event handler. Notice the e parameter in the function's definition. We'll discuss this later in the column.

Since the Event object is not supported by older versions of Navigator, or by Internet Explorer, the window.captureEvents() method requires an object detection statement so it doesn't generate an error on browsers that don't support the Event object. Alternatively, you can check if the browser supports the window.captureEvents() method, or the document.layers object. Here's an example:

function processClicks(e) {
  // statements to handle the click events
}
if (window.captureEvents)
  window.captureEvents(Event.CLICK); // click is an event
window.onclick = processClicks; // onclick is an event handler

See the column "Browser Compatibility" for more information on object detection routines. In this column we won't use any detection routines, for clarity. We'll put them to work in our next column, when we discuss Internet Explorer's event model, and cross-browser event handling.

https://www.internet.com


Created: December 16, 1997
Revised: December 16, 1997

URL: https://www.webreference.com/js/column9/process.html