Client-Side JavaScript: Executable Content in Web Pages
When a web browser is augmented with a JavaScript interpreter, it allows executable content to be distributed over the Internet in the form of JavaScript scripts. Example 1-1 shows a simple JavaScript program, or script, embedded in a web page.
Example 1-1. A simple JavaScript program
<html>
<body>
<head><title>Factorials</title></head>
<script language="JavaScript">
document.write("<h2>Table of Factorials</h2>");
for(i = 1, fact = 1; i < 10; i++, fact *= i) {
document.write(i + "! = " + fact);
document.write("<br>");
}
</script>
</body>
</html>When loaded into a JavaScript-enabled browser, this script produces the output shown in Figure 1-1.

Figure 1-1. A web page generated with JavaScript
As you can see in this example, the
<script> and
</script> tags are used to embed JavaScript
code within an
HTML file.
We’ll learn more about the <script>
tag in Chapter 12.
The main feature of JavaScript
demonstrated by this example is the use of the
document.write( ) method.[2] This method is used to dynamically output
HTML text that is parsed and displayed by the web browser;
we’ll encounter it many more times in this book.
Besides allowing control over the content of web pages, JavaScript allows control over the browser and over the content of the HTML forms that appear in the browser. We’ll learn about these capabilities of JavaScript ...