Skip to main content

Event Handlers

In simplest terms, an event is something that happens to an object. For example, the loading of a page would be an event for the Window object. Don't confuse a method with an event, however. Yes, the Window object has (for example) a history.back() method that loads the previous page in the history list, but this method only initiates the procedure of loading the page; the actual process of the page being loaded is the event.

Events can happen in two ways:

  1. They can happen programmatically by including the appropriate method in your code, such as history.back().
  1. They can happen by user intervention, such as the user clicking the browser's Back button.

Why are events important to you as a programmer? Because they enable you (or, more specifically, your code) to respond to those events in a useful or meaningful way.

For example, when a form is submitted, the Submit event fires. As a careful programmer, you probably want to ensure that the form data has been filled in completely and correctly before the data goes to the server.

You can do that by responding to the Submit event with a function that checks the form fields before allowing the submission to take place.

Two events that we will look at are the mouse click and the mouse over

Next: Mouse Click