Arman Danesh
201 West 103rd Street, Indianapolis, Indiana 46290
To my parentsMichele and Hossainfor instilling in me a thirst for knowledge, and to my wifeTahirihwho inspires me to press forward.
FIRST EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability
is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed
for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
International Standard Book Number: 1-57521-073-8
Library of Congress Catalog Card Number: 95-72943
999897964321
Interpretation of the printing code: the rightmost double-digit number is the year of the book's printing; the rightmost single-digit, the number of the book's printing. For example, a printing code of 96-1 shows that the first printing of the book
occurred in 1996.
Printed in the United States of America
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting
the validity of any trademark or service mark.
President, Sams Publishing:
Richard K. Swadley
Publishing Manager:
Mark Taber
Managing Editor:
Cindy Morrow
Marketing Manager
John Pierce
Assistant Marketing Manager:
Kristina PerryAcquisitions Editor
Mark Taber
Development Editor
Kelly Murdock
Software Development Specialist
Merle Newlon
Production Editor
Marla Reece
Technical Reviewer
Wes Tatters
Editorial Coordinator
Bill Whitmer
Technical Edit Coordinator
Lynette Quinn
Formatter
Frank Sinclair
Editorial Assistants
Carol Ackerman
Andi Richter
Rhonda Tinch-Mize
Cover Designer
Tim Amrhein
Book Designer
Gary Adair
Copy Writer
Peter Fuller
Production Team Supervisor
Brad Chinn
Introduction
Day 1
|
|
1
|
Where Does JavaScript Fit In?
|
2
|
Your First Script
|
Day 2
|
|
3
|
Working with Data and Information
|
4
|
Functions and ObjectsThe Building Blocks of Programs
|
|
From the Web: Dave Eisenberg's Calendar
|
Day 3
|
|
5
|
Events in JavaScript
|
6
|
Creating Interactive Forms
|
|
From the Web: Ashley Cheng's Ideal Weight Calendar
|
Day 4
|
|
7
|
Loops
|
8
|
Frames, Documents, and Windows
|
|
From the Web: Michael Yu's Civic Car Viewer
|
Day 5
|
|
9
|
Remember Where You've Been with Cookies
|
|
From the Web: James Thiele's Reminder Calendar
|
10
|
Strings, Math, and the History List
|
Day 6
|
|
11
|
Having Fun with JavaScript
|
|
From the Web: Michal Sramka's Matches Game
|
12
|
Creating a Spreadsheet in JavaScript
|
Day 7
|
|
13
|
Navigator GoldA JavaScript Development Tool
|
14
|
From JavaScript to JavaLooking into the Future
|
Appendixes
|
|
A
|
JavaScript Reference Resources
|
B
|
JavaScript Language Reference
|
C
|
Glossary
|
Index
|
|
Writing this book has been a process that has involved many people. I particularly would like to thank the staff at Sams.net who worked with me in developing the manuscript: Kelly Murdock, Marla Reece, and Mark Taber and many others I am sure were
involved.
I would also like to thank Gautam Das at the Baha'i World Centre in Haifa, Israel, for reading sections of the book as I was writing it.
Arman Danesh
Arman Danesh works as the Web Development Specialist at the Baha'i World Centre in Haifa. He is also Editorial Director of Juxta Publishing Limited, based in Hong Kong. He received his Masters of Science in Mass Communication from Boston University in
1990. He has also worked as a technology journalist and is a regular contributor and Internet columnist for the South China Morning Post and The Dataphile. Arman lives with his wife, Tahirih, in Haifa, Israel.
The World Wide Web has come a long way since its days as a modest hypertext system used by a few scientists to share information on the Internet.
Today the World Wide Web is the medium of information exchange for millions of people. They are sharing text, video, sound, and data, and increasingly, they are trying to make their Web pages interactive. Businesses are trying to sell their products,
artists are producing new forms of interactive art, and programmers are producing program development aidsall delivered via the World Wide Web and accessed from inside everyday Web browser applications.
In many ways, we are in the midst of an information revolution with a move away from document-centric computing to a network-centric paradigm. Right at the center of this shift is Netscape Communications and its immensely popular Web browser, Netscape
Navigator.
By incorporating Java from Sun Microsystems and its own JavaScript scripting language into the current version of the Netscape Navigator browser, Netscape has helped generate a flurry of movement on the World Wide Web aimed at creating interactive
documents and information.
Java is a platform-independent programming language designed for distributed applications on the Internet. JavaScript presently enhances the functionality of the immensely popular Netscape Navigator Web browser, enabling Web authors and developers to
produce content that changes in response to user actionsdynamic data that makes information more accessible and easier to organize and digest.
In this book we are going to take a look at JavaScriptthe internal scripting language Netscape has developed and included in the Navigator browser.
JavaScript is an evolving tool, like so many tools associated with the Internet and the World Wide Web. Still, the future of JavaScript is sufficiently clear that many people have already developed sophisticated Web-based applications using the
language.
As I mentioned in the Introduction, this book is designed to teach the JavaScript scripting language.
We start by taking a broad look at Netscape Navigator 2.0, the current version of the popular Web browser, which some estimates say commands more than 80 percent of the Web browser market. Following this, we take an introductory look at JavaScript and
its relationship with Java and its place in the Netscape suite of Web development tools.
Once this is done, we will be ready to look at the nuts and bolts of the JavaScript language and learn how to apply them to real-world scenarios on the Web. JavaScript can be used to add a wide range of interactivity and functionality to Web pages
including the following:
These types of functions already appear in numerous Web sites on the World Wide Web, and it is expected that the number will grow rapidly in early 1996 now that Navigator 2.0 has moved from being a beta product to commercial release software.
Throughout the book, you will have the opportunity to develop several small scripts that you can immediately use in your own Web pages.
Finally, we close with a review of the future of JavaScript and where it seems to be heading. We will consider Netscape's plans as well as announcements from other companies to include JavaScript in their products.
The JavaScript scripting language naturally interests a diverse group of people, including Web designers and authors, programmers, and application developers.
This book is clearly of interest to Web developers and authors with experience using HTML and designing Web sites, including using Netscape extensions. Although basic knowledge of HTML is assumed throughout the book, any advanced or complicated HTML
tags being used are introduced and described as needed.
Naturally, programming knowledge and an understanding of the Common Gateway Interface (CGI)used for adding interactivity at the server endis helpful in any discussion of Web development. However, it is not essential for learning JavaScript,
and readers with a sound knowledge of HTML tags can follow the lessons in this book. By the end of the book, not only will you be able to write simple (and complex) JavaScript programs, but you also will have learned to use some of the newer Netscape
extensions to HTML.
Naturally, discussion of a programming language like JavaScript is not solely of interest to Web authors and designers.
Programmers looking to add the latest technology to their list of credits are increasingly interested in learning JavaScript. In addition, applications developers looking at Navigator 2.0 as an engine for deploying platform-independent graphical user
interface applications will quickly find that JavaScript is going to play a critical role in implementing their applications.
In order to take full advantage of this book, you will need several tools. A copy of the latest version of Netscape 2.0 is essential to develop and test program code. In addition, a good editing program that you will feel comfortable using will make the
program development process easier.
Today, JavaScript capabilities can only be found in Netscape's Navigator 2.0 Web browser. Navigator 2.0 moved from being beta software to actual release software in early 1996..
In order to take full advantage of the lessons in this book, it is necessary to have access to a copy of Navigator 2.0 to try the examples and exercises for yourself. Navigator 2.0 is available for most computer platforms, including all versions of
Windows, Mac OS, and a wide range of UNIX variants including Sun OS, Solaris, and Linux. At the present time, there is no native OS/2 version of Netscape Navigator available.
If you need to download a copy of the current version of Navigator 2.0, you can get it from Netscape's home page at http://home.netscape.com/ or from Netscape's numerous FTP servers or their many mirrors:
ftp://ftp.leo.chubu.ac.jp/pub/WWW/netscape/ (Japan)
ftp://sunsite.ust.hk/pub/WWW/netscape/ (Hong Kong)
ftp://sunsite.huji.ac.il/Netscape/ (Israel)
ftp://ftp.adelaide.edu.au/pub/WWW/Netscape/ (Australia)
ftp://sunsite.doc.ic.ac.uk/computing/information-systems/www/Netscape/ (United Kingdom)
ftp://ftp.informatik.rwth-aachen.de/pub/mirror/ftp.netscape.com/ (Germany)
ftp://wuarchive.wustl.edu/packages/www/Netscape/ (U.S.A.)
ftp://sunsite.unc.edu/pub/packages/infosystems/WWW/clients/Netscape (U.S.A.)
In addition to a copy of Navigator 2.0, a strong editor or development tool will make the task of entering, developing, and debugging JavaScript much easier.
If you already do a lot of HTML authoring or programming, you probably have your own favorite tools that will be well-suited to JavaScript development. As long as your editing software produces plain ASCII text files, you should be just fine.
However, several tools may make it easier to develop, edit, and trouble-shoot your JavaScript programs.
In considering editors, it would be worth looking at tools that can help you identify the current line number for debugging scripts. In addition, the ability to launch Netscape Navigator from an editor is a useful feature already found in many HTML
editors. Most HTML editors are suitable to JavaScript development, although a fewincluding leading products such as HoTMetaLare designed to perform validation of HTML and can't be used to develop JavaScript scripts easily.
In addition to a high quality text or programming editor, many users may want to consider Netscape's Navigator 2.0 Gold. Although Navigator Gold was only available in an early beta version (beta 1) when this book was written, Netscape has indicated that
it will take the Navigator 2.0 browser and adds a suite of development and editing tools that make it easy for developers to produce interactive applications deployed on Netscape technologyall in a WYSIWYG (What You See Is What You Get) environment
(or as close as is possible with the Web).
More information about Navigator Gold is available from the site at
http://home.netscape.com/comprod/products/navigator/version_2.0/gold.html.
This book uses certain conventions to aid you, the reader, in your learning process.
"Input" appears next to a listing that you should enter to follow along with the author's lesson. A listing without an Input icon is for illustration or explanation only.
[ic:ccc]This arrow at the beginning of a line of code means that a single line of code requires multiple lines on the page. Many lines of code contain a large number of characters, which might normally wrap on your screen. However, printing limitations
require a break when lines reach a maximum number of characters. Continue typing all characters after the [ic:ccc] as though they are part of the previous line.
Besides on-screen output, "Output" is often used, in this book, to point to a figure that results from the preceding code listing.
"Analysis" The author offers detailed explanations regarding the parts and purposes of the code. (Hint: If you think you might not understand what the code is meant to perform, skip to this section before you input the
listings!)
This book also uses monospaced fonts to denote terms, functions, keywords, variables, and so on, that are taken from or are part of the code. monospaced italics refers to a placeholder that should be filled in with the actual number, variable, or
value represented. Typically, HTML code terms are in ALL CAPS, while JavaScript terms are in the case required by this case-sensitive language.