So far we have look at running Javascript either before or during the loading of your web page. Often you will want Javascript code to run once the page has over loading or even afterwards in reply to some action by the person viewing the page.
The means that Javascript provide to allow Javascript code to be triggered once the page has laden is to fasten event handlers to those part of your page that are to act as triggers for the Javascript that you desire to have run.
Types of Event Handler
There are a number of dissimilar types of event handler that will be triggered by different events. The types of events that can trigger Javascripts attached to event handlers include:
- mouse actions
- keyboard actions
- actions on form fields (regardless of how caused)
- when the page is first loaded or unloaded
- after a specified time has passed
You can also set up events that will be triggered by any part of your page without having to fasten the event to each part of the page unconnectedly by making global event handler).
A lot of of the event handlers are paired with one being triggered by a certain action (eg. mouseover) and one more being triggered by the reverse action (eg. mouseout). By attaching both of a pair of event handlers to the same object on the page you can set up one to perform an exact action and the other to perform the reverse action returning the page to its original appearance.
Attaching Event Handlers
The simplest way to attach event handlers into your page is to put in them in a straight line as quality on the HTML tags of the thing that will trigger them. To trigger an event handler so attached require that the particular action that the event handler recognizes be perform in relation to the object to which it is expressively involved.
In universal event handlers will be attached into a tag using code similar to the following (substituting the suitable event name and function to run for the code in italics):
Note that this is not the most excellent way of attaching event handlers but it is the simplest and most normally used. When you become more familiar with Javascript you will want to move the event handlers out of your HTML into the outside Javascript file the length of with the rest of your Javascript code.
What's Next
Event handlers are what allow you to attach Javascript to your page that will allow your page to interact with your visitors. Over the next few tutorials we will look at the a variety of event handlers and how to trigger them.