Java Reference
In-Depth Information
is just a comment , solely for your benefit. The browser recognizes anything on a line after a double
forward slash ( // ) to be a comment and does not do anything with it. It is useful for you as a
programmer because you can add explanations to your code that make it easier to remember what you
were doing when you come back to your code later.
The alert() function in the second line of code is also new to you. Before learning what it does, you
need to know what a function is.
Functions are defined more fully in Chapter 4, but for now you need only think of them as pieces
of JavaScript code that you can use to do certain tasks. If you have a background in math, you
may already have some idea of what a function is: it takes some information, processes it, and
gives you a result. A function makes life easier for you as a programmer because you don't have to
think about how the function does the task—you can just concentrate on when you want the task
done.
In particular, the alert() function enables you to alert or inform the user about something by
displaying a message box. The message to be given in the message box is specified 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
clicking 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 first 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>
// 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 first 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, finally, 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 setting properties using HTML is static : A value can be set only once and never changed again by
Search WWH ::




Custom Search