Chapter 6. Scripting Events
A graphical user interface constantly monitors the computer's activity for signs of life from devices such as the mouse, keyboard, network port, and so on. Programs are written to respond to specific actions, called events, and run some code based on numerous conditions associated with the event. For example, was the Shift key held down while the mouse button was clicked? Where was the text insertion pointer when a keyboard key was pressed? As you can see, an event is more than the explicit action initiated by the user or system—an event also has information associated with it that reveals details about the state of the world when the event occurred.
6.1. Event Types
Events have been scriptable since the earliest scriptable browsers. The number and granularity of events has increased with the added scriptability of each browser generation. The HTML 4 and DOM Level 2 recommendations cite a group of events called "intrinsic events," which all browsers since Navigator 4 and IE 4 have in common (many of them dating back to the time of Navigator 2). These events include the onclick, onmouseover, onkeypress, and onload events, as well as many other common events. But beyond this list, there are a number of events that are browser specific and support the idiosyncrasies of the document object models implemented in Navigator and Internet Explorer. By far the biggest group of browser-specific events belongs to IE 5 and later—most of those implemented thus far only in the Windows version.
Every event has a type name, such as click, keydown, and load. For example, when a user clicks a mouse button, the physical action fires a "click" event. But, as described later in this chapter, you will frequently associate an event type with an element by what is called an event handler that corresponds to the event. An event handler adopts the event name and appends the word "on" in front of it. Thus, a button element knows to do something with a click event because it has an onclick event handler associated with the button.
It is not uncommon to hear someone call an event handler an event. There is a fine distinction between the two, but you won't be arrested by the jargon police if you say "the onclick event." It is more important that you understand the range of events available for a particular browser version and what user or system action fires the event in the first place.
Table 6-1 is a summary of all the event handlers that are implemented in common for the IE 4 and W3C DOMs. Most of these event handlers are part of the HTML and XHTML recommendations, and will validate as lowercase attributes for elements in XHTML-Strict. A handful of other event handlers are not part of the formal standards, but have been available in scriptable browsers since the early days. See Chapter 10 for complete details about each event type.
Table 6-1. Event handlers for all DHTML browsers
Beyond the cross-browser events in Table 6-1, Microsoft implements an additional set that allows DHTML scripts to react to more specific user and system actions. Table 6-2 lists the IE-only events that may assist a DHTML application. Pay special attention to the columns that show in which version of each browser the particular event handler was introduced. Many of these events are available only in the Windows version of IE. Not listed in Table 6-2 are the many event handlers that apply only to Internet Explorer's data binding facilities, which allow form elements to be bound to server database sources. Bear in mind, however, that an event handler introduced in one browser version may have been extended to other objects in a later browser version. Chapter 10 provides implementation details on all available events.
Table 6-2. Internet Explorer DHTML events
Copyright © 2003 O'Reilly & Associates. All rights reserved.