Ken Ward's Java Script Tutorial ...

Those infuriating Hello and Goodbye Alerts

Click here to see the Hello and Goodby page. Do it once or twice and many times, and see how these alerts really make you hate that page! If you really want to annoy your user, then use this code:

<script type="text/javascript">


//Start of first function

function Hello()


alert("Welcome to My Page");


// start of second function

function Goodbye(){

alert("Thank you for visiting. Come again soon!");





As usual, this code is enclosed in the Java Script and HTML comment tags. It contains two functions: Hello() and Goodbye(). These functions give alert messages when called using the now familiar alert box.

The functions are called by the following placed in the body tags:

<body onload="Hello()" onunload="Goodbye()">

The onload calls Hello(), and the onunload calls Goodby(), producing the results you have already seen when you clicked on the link. Note: while HTML generally doesn't care, XHTML requires all attributes to be lowercase. So onload and onunload are written lowercase in the above.

Actually, these alert messages are ok provided the user visits the pages once only. If he or she has to continually open and close that page, it can get very annoying. You can solve this by creating two pages. One is the one the user would normally visit first, and the other, without the onLoad and onUnload events is the one he or she would access through hyperlinks on the site. For example, the user of this tutorial might want to visit the main page several times to view the contents. If it had alerts, then this would quickly become annoying. By creating two such pages, and putting the links in the lessons to the second one - the one without the alerts - may solve this problem. If the user uses the browser back button, however, the problem wouldn't be solved!

I have used an onLoad alert on this page, perhaps with justification? Once the frame page is loaded, it is not necessary to reload it, so the alert would occur once, and for a good reason (to maximise the page.)