A Complete Guide to Web Design

(やまだぃちぅ) #1

379


JavaScript

Web Design in a Nutshell, eMatter Edition

Chapter 22JavaScript

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 addi-
tional 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 atwebcoder.com.For a more advanced refer-
ence, 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 Dream-
weaver 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">
<!—-
Free download pdf