Table 11-18 summarizes the major features of the major event models. As we’ve seen in this chapter, the browser events situation makes for an ugly mess if you wish to do anything other than basic event processing in JavaScript.
Major Features |
Basic Model |
Netscape 4 Model |
Internet Explorer 4+ Model |
DOM2 Model |
---|---|---|---|---|
>To bind a handler… |
(X)HTML attributes |
(X)HTML attributes, captureEvents() |
(X)HTML attributes, attachEvent() |
(X)HTML attributes, addEventListener() |
>To detach a handler… |
Set (X)HTML attribute to null with script |
Set (X)HTML attribute to null with script, releaseEvents() |
Set (X)HTML attribute to null with script, detachEvent() |
Set (X)HTML attribute to null with script, removeEventListener() |
>The Event object... |
N/A |
Implicitly available as event in attribute text, passed as an argument to handlers bound with JavaScript |
Available as window.event |
Passed as an argument to handlers |
>To cancel the default action… |
Return false |
Return false |
Return false |
Return false, preventDefault() |
>How events propagate |
N/A |
From the Window down to the target |
From the target up to the Document |
From the Document down to the target and then back up to the Document |
>To stop propagation… |
N/A |
N/A |
N/A |
stopPropagation() |
>To redirect an event… |
N/A |
routeEvent() |
fireEvent() |
dispatchEvent() |
Deciding which event mode to use is largely dictated by the browsers your clients are likely to use. For the next few years, it is likely that the Internet Explorer model will be the most widely used. The Netscape 4 model is quickly dying out as the last pockets of Netscape 4 convert to Mozilla-based browsers and while the DOM2 model is available in a rapidly increasing number of browsers, it is unlikely to displace IE any time soon (if at all).
Given these facts, it will probably be necessary to write cross-browser event handlers to carry out your tasks. Doing so is not hard if you limit yourself to standard events and straightforward applications. If you need to do non-trivial tasks, then it’s probably worthwhile to find a cross-browser event library on the Web, or to write your own.