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 http://webcoder.com. 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 LANGUAGE="JavaScript">
script goes here
</SCRIPT>

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.