onLoad Events
Upon loading this page, a window appeared with a welcome message in it. The window stayed for 5 seconds, then closed. If you want to see it again, click RELOAD
The onLoad
event is placed in the <BODY>
tag, and it is used to trigger events automatically upon loading the page. You can use the onLoad
to display a welcome message, place the focus in a particular textbox
, execute a special effect, or prompt for a password. The <BODY>
tag of this page looks like this:
<BODY bgcolor="white" onLoad="MsgBox()">
MsgBox()
is a function in the <SCRIPT>
area which opens a window, then closes it after 5 seconds. We will discuss the setTimeout() method used to close the window automatically in more detail later.
onUnload Events
As you may have guessed, the onUnload()
event handler will trigger an event when the user exits the current page.
I had this page open into a new window so that you can experiment with the onUnload
event handler without losing your place in the tutorial.
Here is the <BODY>
tag at the top of this document.
<BODY BGCOLOR="white" onUnload="alert('Please come back soon')"
Use any method you like to leave this page. Click the back button, type a URL into the location box, reload, or close the window.
No matter how you leave the page the onUnload()
event will trigger. Yes you can use onUnload
to make a page difficult to leave, but this would quickly result in a very lonely web site.