Previous Page
Next Page

Handling Events

Events are actions that the user performs while visiting your page. Submitting a form and moving a mouse over an image are two examples of events.

JavaScript deals with events using commands called event handlers. An action by the user on the page triggers an event handler in your script. The 12 most common JavaScript event handlers are listed in Table 1.1. We deal with other, more advanced event handlers in Chapter 9.

Table 1.1. Event Handlers

Event

What It Handles

onabort

The user aborted loading the page

onblur

The user left the object

onchange

The user changed the object

onclick

The user clicked an object

onerror

The script encountered an error

onfocus

The user made an object active

onload

The object finished loading

onmouseover

The cursor moved over an object

onmouseout

The cursor moved off an object

onselect

The user selected the contents of an object

onsubmit

The user submitted a form

onunload

The user left the page


For example, let's say that our cat handles the event onpetting by performing the actions purr and stretch.

In JavaScript, if the user clicks a button, the onclick event handler takes note of the action and perform whatever duties it was assigned.

When you write a script, you don't have to anticipate every possible action that the user might take, just the ones where you want something special to occur. For instance, your page will load just fine without an onload event handler. But you use the onload command if you want to trigger a script as soon as the page loads.


Previous Page
Next Page