Event Interface Soup

This article is a brief introduction to the three different Event interfaces: DOM0, DOM2 and IE.

DOM0 Event Interface

There is no formal DOM0 standard. By "DOM0" we are referring to the object model supported by both IE4 and NN4. The DOM0 event interface is still supported by modern browsers.

Inline Event Handlers

An inline event handler is one that is assigned via an HTML attribute.

<a href='#' onclick='handleClick()'>click me</a>
    

You assign a string of Javascript to the attribute and, in effect, here's what the browser does:

a_element.onclick = function (event) {
  handleClick();
}
    

Whatever string you assign to the attribute becomes the body of an anonymous function. That function is assigned to the element's event handler property.

The anonymous function has one parameter, event. The string of Javascript you assign to the attribute can reference this parameter. This is how you would pass the event argument to the handleClick function:

<a href='#' onclick='handleClick(event)'>click me</a>
    

You can think of that as becoming the following. Note that in IE the Event object is not passed to the handler. However, this works in IE because the event identifier referenced in the string of Javascript is associated with the window.event object, which is part of the IE propietary model.

a_element.onclick = function (event) {
  handleClick(event);
}
    

Sometimes you want to pass a reference to the object clicked to the handler. You do that like this:

<a href='#' onclick='handleClick(this)'>click me</a>
    

Why does this work? Look at it like this:

a_element.onclick = function (event) {
  handleClick(this);
}
    

Within the function a_element.onclick , this references a_element.

Finally let me say... try not to use inline handlers. It is good practise to keep all script out of the HTML (separation of behavior and structure).

Event Handler Properties

As you saw above, an inline event handler is just a string of Javascript which becomes the body of an anonymous function which is assigned to the element's event handler property. In general, it is better to directly use event handler properties instead of using inline handlers. There are a wide variety of event handler properties, for example onclick, onmousemove, onchange, onsubmit, etc.

The following gets a reference to some element and assigns a function reference to the element's onclick handler.

window.onload = function () {
  var ele = document.getElementById('someElementID');
  if (ele) {
    ele.onclick = handleClick;
  }
};
function handleClick(e) {
  e = e || window.event;
}
    

Note that handleClick will be called as a method of the element object. This means the this keyword will reference the element object. Also note that the Event object is passed as an argument to handleClick, but not in IE. In IE the Event object is available as window.event.

Returning false from the handler will cancel the current default action.

DOM2 Event Interface

All modern browsers (except for IE7 and down) support the DOM2 Event interface. I will only summarize this interface here. Refer to the W3C's DOM2 Events and DOM3 Events documents for details.

All Element objects (and some other objects) have methods named addEventListener and removeEventListener. This interface allows adding multiple listeners for the same event on the same object. The Event object is passed as an argument to the listener.

In the listener, this references the object to which the listener was added, at least this is true for me in Opera 9.20, FireFox 2.0.0.3 and Safari 3.0.1 (all on WinXP). However I don't think this is part of the W3C standard (I'm not 100% sure) and I think I remember that previous versions of Opera and Mozilla, for example, did not support this.

IE Event Interface

Internet Explorer 7 and down do not support the DOM2 Event interface. IE supports the DOM0 Event interface and a proprietary Event interface. I will only summarize this interface here. Refer to the MSDN Event Model and attachEvent documents for details.

All Element objects (and some other objects) have methods named attachEvent and detachEvent. This interface allows adding multiple listeners for the same event on the same object. The Event object is passed as an argument to the listener. In the listener, this will not reference the object to which the listener was added.

In IE's DOM0 Event interface the Event object is not passed to the handler. There is a global window.event object which is valid during execution of the handler.

License

By your use of X and/or CBE and/or any Javascript from this site you consent to the GNU LGPL - please read it. If you have any questions about the license, read the FAQ and/or come to the forums.

Tech Support

Forum support is available at the X Library Support Forums.

About Cross-Browser.com

Cross-Browser.com is the home of X - a cross-browser Javascript library, and many demos, applications, articles and documentation.

Browser Support

The X core is designed to work with all browsers, Object-detection instead of browser-detection is used exclusively. Currently, I'm testing with the following browsers. X has been tested by others on a wide variety of platforms.

Win7 (Home): IE 9.

WinXP (SP3): Chrome 3.0.195.38, Firefox 3.5.5, IE 7 & 8, Opera 10.60 and Safari 4.0.3.

Linux (Ubuntu 9.10): Chrome 4.0.249.43 and FireFox 3.5.7.

Search

Cross-Browser.com

World Wide Web

Article Status

Currently being updated (27Jun07).