O'Reilly logo

Closure: The Definitive Guide by Michael Bolin

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 6. Event Management

The goog.events package is responsible for managing events and listeners throughout Closure. In addition to handling traditional DOM events (such as mouseover, click, etc.), Closure makes it possible to manage custom, user-defined events through the same API. This consistency is a refreshing deviation from the existing mess of inconsistencies in event handling across web browsers today.

A Brief History of Browser Event Models

Historically, web browsers have differed significantly in their event models. The original event registration model introduced by Netscape Navigator (which also goes by the retronym “DOM Level 0 event model”) works across all JavaScript browsers (well, almost). Unfortunately, it has a number of issues, particularly when it comes to adding more than one event handler to the same event on the same element. (QuirksMode does a great job of explaining the various pitfalls: http://www.quirksmode.org/js/events_tradmod.html.)

In the process of Netscape and Microsoft’s battle royale to become the maker of the most popular web browser, they improved their respective event models in similar (but different) ways. Both models provided an API that would take a string to identify the type of event to listen to and a function to call when the event was fired. However, the string values differed ("click" versus "onclick") as did the object that this referred to when the callback function was executed (the callback function’s arguments were also different). ...

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