Chapter 22. Introduction to JavaScript

JavaScript is a client-side scripting language that adds interactivity and conditional behavior to web pages. With JavaScript, you can do such things as display additional information about links, create mouse rollover effects, change the contents of pages based on certain conditions, randomly display content on a page, load content in new browser windows and frames, and (with some help from CSS) move elements around the page.

This chapter is derived from material and code by Nick Heinle, author of Designing with JavaScript, (O’Reilly & Associates, 1997). For more tutorials on writing JavaScript, see Nick’s pages at For a more advanced reference, see JavaScript: The Definitive Guide, Third Edition, by David Flanagan (O’Reilly & Associates, 1998). Also note that for simple functionality, you may not need to write your own JavaScript at all; software like Macromedia’s Dreamweaver can do your coding for you.

JavaScripts are usually placed directly in the HTML document. They can go either in the head or the body and there can be numerous scripts in a single HTML document. Here’s the syntax:

script goes here

JavaScript Basics

There are two parts to most JavaScript applications: the functions that tell the browser what to do, and references to these functions. Let’s take the example of a simple web page that displays a linked document in a second window:

<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> ...

Get Web Design in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.