- Understanding Event Handlers
- Using Mouse Events
- Using Keyboard Events
- Using the onLoad and onUnload Events
- Workshop: Adding Link Descriptions to a Web Page
- Summary
- Q&A
- Quiz
- Exercises
Using the onLoad and onUnload Events
Another event you'll use frequently is onLoad. This event occurs when the current page (including all of its images) finishes loading from the server.
The onLoad event is related to the document object, and to define it you use an event handler in the <body> tag. For example, the following is a <body> tag that uses a simple event handler to display an alert when the page finishes loading:
<body onLoad="alert('Loading complete.');">
NOTE
Since the onLoad event occurs after the HTML document has finished loading and displaying, you cannot use the document.write or document.open statements within an onLoad event handler. This would overwrite the current document.
In JavaScript 1.1 and later, images can also have an onLoad event handler. When you define an onLoad event handler for an <img> tag, it is triggered as soon as the specified image has completely loaded.
You can also specify an onUnload event for the <body> tag. This event will be triggered whenever the browser unloads the current documentthis occurs when another page is loaded or when the browser window is closed.