O'Reilly logo

JavaScript® 24-Hour Trainer by Jeremy McPeak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required