Using Events

Okay, so you know about four events, but how do you use them? Well, let's start with the onLoad event.

Using the onLoad Event

To use the onLoad event to trigger a JavaScript function, you must place the following event handler in the <body> tag of the Web page:

<body onLoad="functionname()">

NOTE

An event handler is a piece of code that handles the event and does something as a result of it occurring, such as running a JavaScript function.

All you now must do to trigger the yourMessage() function is type its name between the double quotes of the event handler—for example, <body onLoad="yourMessage()">. Now, save the page and load it into the browser window:

 <html> <head> <title>A Simple Page</title> <script language="JavaScript"> ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.