Reacting on JavaScript events can be done in different ways:
However, there are various, competing event mechanisms in the different browsers. Internet Explorer supports attaching events to an element using the attachEvent() method. The name of the event here equals the HTML attribute, so you use "onload", for instance (though the event itself is called "load"). All other relevant browsers support the addEventListener() method, a part of the W3C model. Here, you provide the name of the event, so just "load" instead of "onload". The following example shows how to attach an event to a button in a cross-browser fashion: Attaching an Event (attach.html)
Note You can remove event handlers, as well. Internet Explorer uses detachEvent(), whereas other browsers follow the W3C and name their function removeEventListener().
|