WebReference.com - Part 1 of Chapter 6 from Dynamic HTML: The Definitive Reference, 2nd Edition. From O'Reilly (1/5).

current pageTo page 2To page 3To page 4To page 5
[next]

Dynamic HTML: The Definitive Reference, 2E. Chapter 6: Scripting Events

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.

Capitalization of event handler names is another fuzzy subject. When used as HTML tag attributes, event handler names are case-insensitive. A tradition among long-time scripters has been to capitalize the first letter of the event type, as in onClick. XHTML validation, however, requires all lowercase letters for event handler attributes, as in onclick. In other situations, you can assign an event handler as a property of an object. In this case, the event handler property name must be all lowercase to be compatible across platforms (because scripted items, such as property and method names, are case-sensitive in JavaScript). The trend, therefore, is toward all lowercase event handler attribute names in tags--the format used throughout this book.

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

Event handlerNNIE/WinIE/MacHTMLDescription
onabort343.01n/aThe user has interrupted the transfer of an image to the client
onblur233.014An element has lost the input focus because the user clicked out of the element or pressed the Tab key
onchange233.014An element has lost focus and the content of the element has changed since it gained focus
onclick233.014The user has pressed and released a mouse button (or keyboard equivalent) on an element
ondblclick443.014The user has double-clicked a mouse button on an element
onerror344n/aAn error has occurred in a script or during the loading of some external data
onfocus233.014An element has received the input focus
onkeydown4444The user has begun pressing a keyboard character key
onkeypress4444The user has pressed and released a keyboard character key
onkeyup4444The user has released a keyboard character key
onload233.014A document or other external element has completed downloading all data into the browser
onmousedown4444The user has begun pressing a mouse button
onmousemove4444The user has rolled the mouse (irrespective of mouse button state)
onmouseout333.014The user has rolled the mouse out of an element
onmouseover233.014The user has rolled the mouse atop an element
onmouseup4444The user has released the mouse button
onmove434n/aThe user has moved the browser window
onreset3444The user has clicked a Reset button in a form
onresize444n/aThe user has resized a window or object
onselect2334The user is selecting text in an input or textarea element
onsubmit233.014A form is about to be submitted
onunload233.014A document is about to be unloaded from a window or frame

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

Event handlerIE/WinIE/MacDescription
onbeforecopy5n/aThe user has issued a Copy command, but the operation has not yet begun
onbeforecut5n/aThe user has issued a Cut command, but the operation has not yet begun
onbeforepaste5n/aThe user has issued a Paste command, but the operation has not yet begun
onbeforeprint5n/aThe user has issued a Print command, but the document has not yet been sent to the printer
oncontextmenu5n/aThe user has pressed the context menu ("right click") mouse button
oncopy5n/aThe user has initiated a Copy command, but the operation has not yet begun
oncut5n/aThe user has issued a Cut command, but the operation has not yet begun
ondrag5n/aThe user is dragging the element
ondragend5n/aThe user has completed dragging the element
ondragenter5n/aThe user has dragged an element into the space of the current element
ondragleave5n/aThe user has dragged an element out of the space of the current element
ondragover5n/aThe user is dragging an element through the space of the current element
ondrop5n/aThe user has dropped a dragged element atop the current element
onfocusin6n/aThe user has acted to give focus to the element, but the actual focus has not yet occurred
onfocusout6n/aThe user has given focus to another element
onhelp44The user has pressed the F1 key or chosen Help from the browser menu
onmouseenter5.5n/aThe user has moved the cursor into the space of the element
onmouseleave5.5n/aThe user has moved the cursor to outside the space of the element
onmousewheel6n/aThe user is rolling the mouse wheel
onmoveend5.5n/aA positioned element has completed its motion
onmovestart5.5n/aA positioned element is starting its motion
onpaste5n/aThe user has issued a Paste command, but the operation has not yet begun
onscroll44The user has adjusted an element's scrollbar
onselectstart44The user is beginning to select an element

current pageTo page 2To page 3To page 4To page 5
[next]

Created: August 29, 2002
Revised: August 29, 2002

URL: http://webreference.com/programming/javascript/dhtmlref/chap6/1/