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


What It Handles


The user aborted loading the page


The user left the object


The user changed the object


The user clicked an object


The script encountered an error


The user made an object active


The object finished loading


The cursor moved over an object


The cursor moved off an object


The user selected the contents of an object


The user submitted a form


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