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.');">
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.
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.