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

Get Closure: The Definitive Guide 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.