Chapter 21. The Standard Event Object

Over the course of the past several lessons you've learned how you can handle events with HTML attributes, DOM object properties, and DOM object methods. But one vital piece of information has been withheld from you: how to access a special object that contains information about any event. Not surprisingly, the manner in which you access this object differs according to the user's browser: standards-supporting browsers use the standard Event object, and legacy versions of Internet Explorer (IE) use their own proprietary event object.

This lesson focuses on the standard Event object. You'll be introduced to IE's legacy event object in the next lesson.

ACCESSING THE EVENT OBJECT

When you have a moment, flip through the previous lessons regarding event handling. Pay attention to the functions assigned to handle events; notice that none of these functions accept any arguments. This is by design, as you, the developer, do not call event handlers; the browser does. So, it makes little sense to write a function that accepts arguments.

Or does it?

In actuality, the functions you wire to events should accept one argument, because, according to the standard event model, the browser passes an Event object to the function handling an event, like this:

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

This code sets an event handler on the document object for the click event. When you click anywhere on the page, you see an alert box similar ...

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.