The addEventListener() method will attach a function object to an EventTarget node in such a way that the function is called when the event is triggered. An Event object is passed to the function as an argument so you need to specify this in its declaration. This is how handler functions are registered as listeners.
You can replace a listener with another by registering a new listener with the same parameters. The old one will be discarded. If you want to stop the listener from responding to the event, you can deregister it with the removeEventListener() method.
The three parameters to the addEventListener() method are:
A string describing the event type being registered.
A reference to a function object that has been declared elsewhere in the script source text.
A Boolean flag value that indicates whether the event listener should use bubbling (bottom up) or capture (top down) event propagation. Both types can be registered separately but must also be removed separately.
Examining the DOM level 2 specification suggests that these event type strings may be used:
Event string | Event object type | DOM |
---|---|---|
abort | HTMLEvent | 2 |
blur | HTMLEvent | 2 |
change | HTMLEvent | 2 |
click | MouseEvent | 2 |
DOMActivate | UIEvent | 2 |
DOMAttrModified | MutationEvent | 2 |
DOMCharacterDataModified | MutationEvent | 2 |
DOMFocusIn | UIEvent | 2 |
DOMFocusOut | UIEvent | 2 |
DOMNodeInserted | MutationEvent | 2 |
DOMNodeInsertedIntoDocument | MutationEvent | 2 |
DOMNodeRemoved | MutationEvent | 2 |
DOMNodeRemovedFromDocument | MutationEvent | 2 |
DOMSubtreModified | MutationEvent | 2 |
error | HTMLEvent | 2 |
focus | HTMLEvent | 2 |
load | HTMLEvent | 2 |
mousedown | MouseEvent | 2 |
mousemove | MouseEvent | 2 |
mouseout | MouseEvent | 2 |
mouseover | MouseEvent | 2 |
mouseup | MouseEvent | 2 |
reset | HTMLEvent | 2 |
resize | HTMLEvent | 2 |
scroll | HTMLEvent | 2 |
select | HTMLEvent | 2 |
submit | HTMLEvent | 2 |
unload | HTMLEvent | 2 |
Note that the DOM level 2 event module specification is somewhat ambiguous on the specifics of event type values and you may want to refer to the specification if you are intending to exercise these capabilities in the Netscape Navigator version 6.0 browser. This functionality is fairly new and still in a state of evolution but since it is defined in a standard, it will become more widespread and is therefore the best way to ensure portability even if it takes a while for the various browser manufacturers to catch up and make their products compliant.
Prev | Home | Next |
EventTarget object | Up | EventTarget.dispatchEvent() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |