Share this Page URL
Help

Chapter 17. The Programmable Page: Addin... > Some JavaScript Examples - Pg. 201

The Programmable Page: Adding JavaScripts to Your Pages function SayGoodbye() { alert("Thanks for visiting my Web site!") } 201 Here's how it looks within the page (see jsalert2.htm on the CD in this book): <HTML> <HEAD> <TITLE>Displaying a Message to the User, Part 2</TITLE> </HEAD> <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!-- function SayGoodbye() { alert("Thanks for visiting my Web site!") } //--> </SCRIPT> <BODY onUnload="SayGoodbye()"> <a href="/">Click here to leave this page and see the alert</a> </BODY> </HTML> The thing to notice here is the special onUnload statement in the <BODY> tag. This tells the browser to execute the SayGoodbye() function when the user leaves ("unloads") the page. So, to summarize, here's how the browser executes JavaScript statements: · If the statements aren't within a function, the browser executes them as soon as the page loads. · If the statements are within a function, the browser doesn't execute them until the function is called.