Java Reference
In-Depth Information
In particular, the alert() function enables you to alert or inform the user about something by display-
ing a message box. The message to be given in the message box is specifi ed inside the parentheses of
the alert() function and is known as the function's parameter .
The message box displayed by the alert() function is modal . This is an important concept, which you'll
come across again. It simply means that the message box won't go away until the user closes it by click-
ing the OK button. In fact, parsing of the page stops at the line where the alert() function is used and
doesn't restart until the user closes the message box. This is quite useful for this example, because it
enables you to demonstrate the results of what has been parsed so far: The page color has been set to
white, and the fi rst paragraph has been displayed.
When you click OK, the browser carries on parsing down the page through the following lines:
<p>Paragraph 2</p>
<script type=”text/javascript”>
// Script block 2
document.bgColor = “RED”;
alert(“Second Script Block”);
</script>
The second paragraph is displayed, and the second block of JavaScript is run. The fi rst line of the script
block code is another comment, so the browser ignores this. You saw the second line of the script code
in the previous example — it changes the background color of the page to red. The third line of code
is the alert() function, which displays the second message box. Parsing is brought to a halt until you
close the message box by clicking OK.
When you close the message box, the browser moves on to the next lines of code in the page, displaying
the third paragraph and fi nally ending the web page.
<p>Paragraph 3</p>
</body>
</html>
Another important point raised by this example is the difference between setting properties of the page,
such as background color, via HTML and doing the same thing using JavaScript. The method of set-
ting properties using HTML is static : A value can be set only once and never changed again by means
of HTML. Setting properties using JavaScript enables you to dynamically change their values. The term
dynamic refers to something that can be changed and whose value or appearance is not set in stone.
This example is just that, an example. In practice, if you want the page's background to be red, you can
set the <body> tag's BGCOLOR attribute to “RED” and not use JavaScript at all. Where you want to use
JavaScript is where you want to add some sort of intelligence or logic to the page. For example, if the
user's screen resolution is particularly low, you might want to change what's displayed on the page;
with JavaScript, you can do this. Another reason for using JavaScript to change properties might be for
special effects — for example, making a page fade in from white to its fi nal color.
Search WWH ::




Custom Search