1. Home
  2. Computing & Technology
  3. JavaScript

The Javascript DOM

20. attachEvent

clr gif

Attaching an Internet Explorer event processor to a web page is not a great deal different from the way that a standard DOM event listener is attached. Let's look at the IE equivalent of the code that we looked at in the last tutorial:

Unlike the standard DOM method, this method still requires us to add the word 'on' to the front of the event name. Also there is no third parameter because this method always passes the event outwards from the most specific object.

Also unlike the standard DOM method, the event is not passed to the function as a parameter. Instead IE supplies a standard window.event object to hold the details relating to the latest event. Most of the commonly used properties of this object are similar enough to the standard ones to allow for cross browser processing. One that is sligfhtly different is window.event.button which contains '1' for the left button, '2' for the right button, and '4' for the centre button with other values up to '7' representing combinations of two or three buttons pressed at the same time.

var fld = document.getElementById('myfield');
if (fld.attachEvent)
fld.attachEvent('onkeyup',eventkeyup);

The event methods though are completely different. In fact IE uses methods to handle these instead of methods.

  • set window.event.cancelBubble to true to prevent the event from being passed through to further event processors that may otherwise have received the event
  • set window.event.returnValue to false to stop the calling object from performing any action that it would have otherwise performed after the event processor finished

In the next tutorial we will look at how to combine this with the standard DOM method in order to use whichever of the two that the browser supports and also at what we can do for those old browsers that don't support either.

Explore JavaScript

More from About.com

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Animation & Effects
  5. Document Object Model
  6. attachEvent

©2008 About.com, a part of The New York Times Company.

All rights reserved.