[Chapter 21] JavaScript Quick Reference
Author: Stephen Spainhour and Valerie Quercia
Date: Fri Aug 29 18:19:53 1997

#

| Chapter 21 |

# 21. JavaScript Quick Reference

Contents:
JavaScript Overview
Client-Side JavaScript Examples
The JavaScript Reference Pages

JavaScript is an evolving scripting language that can be used to extend the capabilities of HTML pages on the World Wide Web. At the end of this chapter is a concise summary of the various JavaScript objects, and the properties, methods, and event handlers defined for them. If you're already writing JavaScript code, this section should help refresh your memory about specific names and syntaxes. For newcomers, we also provide an overview of JavaScript and its capabilities. But if you want to learn JavaScript, you'll have to consult a book devoted to it, such as JavaScript: The Definitive Guide, by David Flanagan, published by O'Reilly & Associates.

JavaScript is being developed by Netscape Communications Corporation for use in their Netscape Web browser (client-side JavaScript) and their Web server products (server-side JavaScript). The core JavaScript language has become fairly stable in Netscape 3.0. However, the final JavaScript specification is still pending and plenty of features are still being tuned and added. (In Netscape 2.0, the version of JavaScript is much more limited.) Despite the beta status of JavaScript, tremendous numbers of people are already using it, and Netscape is offering it freely to the market in an attempt to create a standard.

But what is JavasScript and what does it actually do? It may be natural to assume that JavaScript is a simplified version of Java, the programming language from Sun Microsystems, but this is not the case. As a matter of fact, other than an incomplete syntactic resemblance and the ability of both languages to deliver "executable content" over networks, JavaScript and Java are entirely unrelated.

However, Java and JavaScript do provide complimentary capabilities, and thus work very well together. JavaScript can control browser behavior and content but cannot draw graphics or perform networking. Java has no control over the browser as a whole, but can do graphics, networking, and multithreading. In Netscape 3.0, JavaScript can communicate with the Java interpreter built into the browser and can work with and control any Java applets in a Web page.

## 21.1 JavaScript Overview

JavaScript is a simple, interpreted programming language. Client-side JavaScript is JavaScript embedded into HTML Web pages. It allows "executable content" to be distributed over the Internet. (Currently the only JavaScript-enabled browsers are Netscape versions 2.0 and 3.0, and Microsoft Internet Explorer version 3.0.) An example of a client-side JavaScript application appears later in this chapter.

Server-side JavaScript provides an alternative to CGI scripts but goes a step beyond: the code is embedded directly within HTML pages and allows executable server-side scripts to be intermixed with Web content. Server-side JavaScript dynamically generates HTML that is displayed by the client. But its most powerful features come from the server-side objects it has access to. The File object, for example, allows a server-side script to read and write files on the server. Keep in mind, however, that (as of this printing) Netscape's server-side JavaScript product, LiveWire, is still in beta testing, and no other vendors have yet developed products incorporating this technology.

Here's an overview of some of the things you can do with JavaScript:

- Control the appearance and content of HTML documents. You can write arbitrary HTML code into a document as the document is being parsed by the browser. You can also use the JavaScript Document object to generate documents entirely from scratch.

- Control the behavior of the browser. For example, the Window object supports methods (a function/subroutine in JavaScript parlance) to pop up dialog boxes, to create and open/close entirely new browser windows, etc.

- Interact with document content. The JavaScript Document object, and the objects it contains, allow programs to read, and sometimes interact with, portions of the document. It is not possible to read the actual text itself, but, for example, it is possible to obtain a list of all hypertext links in a document. By far the most important capability for interacting with document contents is provided by the Form object, and by the Form element objects it can contain: the Button, Checkbox, Hidden, Password, Radio, Reset, Select, Submit, Text, and Textarea objects. These objects allow you to read and write the values of any input element in any form in the document.

- Interact with the user. An important feature of JavaScript is the ability to define "event handlers"--arbitrary pieces of code to be executed when a particular event occurs (usually a user action). JavaScript can trigger any kind of action in response to user events. For example, code could be written to display a special message in the status line when the user positions the mouse over a hypertext link, or to pop up a confirmation dialog box when the user submits an important form.

- Read and write cookie values, and dynamically generate HTML based on the value of cookies. See Chapter 12, Cookies, for more information on cookies.

- Perform arbitrary (mathematical) computation. It also simplifies the process of computing and working with dates and times. (See the section entitled "Date Object" later in this chapter.)

These are just a few of JavaScript's many capabilities. JavaScript can be used to greatly enhance your HTML code.

Media Types and Subtypes |  | Client-Side JavaScript Examples