Chapter 22. Internet Explorer's Event Object

Internet Explorer's (IE) event model has its roots in early versions of the browser, and it has remained virtually the same for over 12 years. In fact, the organizations behind Chrome, Safari, and Opera saw fit to implement it in their browsers—only Firefox doesn't support it. Despite this rather broad support, IE's event model is still considered to be incorrect, and its use in any standards-supporting browser is discouraged.

The key to IE's event model is an object called event. This object is populated with event information every time an event is handled. In this lesson you will learn how to access the event object and glean event information from it.

ACCESSING THE EVENT OBJECT

The event object is a global object, so it is actually a property of window. You can therefore access it rather easily, as shown in the following code:

eventUtility.addEvent(document, "click", function() {
    alert(event);
});

Here an event handler is assigned to the click event for the document object. When you click anywhere in the document an alert box, shown in Figure 22-1, simply displays the text "[object]".

When an event handler is assigned with the attachEvent() method, which the eventUtility.addEvent() method uses, the event object is also passed as a parameter to the handling function, much as the standard Event object is in standards-supporting browsers. The following code shows this:

eventUtility.addEvent(document, "click", function(event) { alert(event); ...

Get JavaScript® 24-Hour Trainer 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.