Java Reference
In-Depth Information
Note that the preceding line of code is an example of a JavaScript statement . Every line of code
between the <script> and </script> tags is called a statement, although some statements may run on
to more than one line.
You'll also see that there's a semicolon ( ; ) at the end of the line. You use a semicolon in JavaScript to
indicate the end of a statement. In practice, JavaScript is very relaxed about the need for semicolons,
and when you start a new line, JavaScript will usually be able to work out whether you mean to start
a new line of code. However, for good coding practice, you should use a semicolon at the end of
statements of code, and a single JavaScript statement should fit onto one line rather than continue on to
two or more lines. Moreover, you'll find some situations in which you must include a semicolon, which
you'll come to later in the topic.
Finally, to tell the browser to stop interpreting your text as JavaScript and start interpreting it as
HTML, you use the script close tag:
</script>
You've now looked at how the code works, but you haven't looked at the order in which it works. When
the browser loads in the web page, the browser goes through it, rendering it tag by tag from top to
bottom of the page. This process is called parsing . The web browser starts at the top of the page and
works its way down to the bottom of the page. The browser comes to the <body> tag first and sets the
document's background to white. Then it continues parsing the page. When it comes to the JavaScript
code, it is instructed to change the document's background to red.
Writing more JavasCript
The first example let you dip your toes into the JavaScript waters. We'll write a few more JavaScript
programs to demonstrate the web page flow and one of the many ways to display a result in the
browser.
Way things Flow
trY it out
Let's extend the previous example to demonstrate the parsing of a web page in action. Type the
following into your text editor:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Chapter 1, Example 2</title>
</head>
<body bgcolor="white">
<p>Paragraph 1</p>
<script>
// script block 1
 
Search WWH ::




Custom Search