Handling EventsEvents 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.
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. |