<html>
<head> <title> JavaScript example </title>
</head>
<body onLoad="window.alert('I
sure hope you bookmarked this website!')"
bgcolor="#FFFFFF" text="#000000">
<h1> This is an example with JavaScript </h1>
</body>
</html>
Result
A few comments:
- OnLoad is a
called an event handler. OnLoad tells the browser that the
event must happen when the web page is first or initially
loaded (that is, produce this Alert window onLoading the web page).
- When the browser sees window.alert(),
it knows what to do. Window is called an object and alert()
is called a method of the window object. An object
can have many methods and for the window object, alert( ) is
one them.
- The message is called an
argument and arguments must always be enclosed within parentheses.
The argument belongs to the method alert( ) and note that it
is enclosed with single quotation marks. Note that the whole
statement for the onLoad event handler is enclosed with double
quotation marks. When two sets of quotation marks are required, then one
set must be double and the other set must be single quotation marks.
Otherwise the browser will be unable to keep things
straight. Omitting these quotation marks will result in
an error message. Note that these quotation marks did not appear in the
pop-up window.
- If you want the message
to be displayed only when the visitor leaves your web page,
use onUnload instead of onLoad. You can also change the
message to suit your own needs.
Note: The onLoad attribute of the BODY tag (onLoad="window.alert('I
sure hope you bookmarked this website!')") must be printed on one
line in Notepad or in whatever text editor you are using. A browser looks for
the closing quotation marks on the same line as the opening
quotation marks. If the closing quotation marks are on a different line, you
have a JavaScript error and the script will not work. Therefore make sure that onLoad="window.alert('I
sure hope you bookmarked this website!')" appears all on the same line
in your text editor such as Notepad.
Also note
that the JavaScript Alert window that appeared when you loaded this web
page will appear every time you re-load or return to this page.