Name

Element.attachEvent( ): register an event handler — IE 4:

Synopsis

void attachEvent(Stringtype,
                 Function listener);

Arguments

type

The type of event for which the event listener is to be invoked, with a leading “on” prefix. For example, “onload”, “onclick”, or “onmousedown”.

listener

The event listener function that is invoked when an event of the specified type is dispatched to this element. This function is not passed any arguments but can obtain the Event object from the event property of the Window object.

Description

This method is an IE-specific event registration method. It serves the same purpose as the standard addEventListener( ) method (which IE does not support) but is different from that function in several important ways:

  • Since the IE event model does not support event capturing, attachEvent( ) and detachEvent( ) expect only two arguments: the event type and the handler function.

  • The event-handler names passed to the IE methods should include the “on” prefix. For example, use “onclick” with attachEvent( ) instead of “click” for addEventListener( ).

  • Functions registered with attachEvent( ) are invoked with no Event object argument. Instead, they must read the event property of the Window object.

  • Functions registered with attachEvent( ) are invoked as global functions, rather than as methods of the document element on which the event occurred. That is, when an event handler registered with attachEvent( ) executes, the this keyword refers to ...

Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.