Special Edition



Using

JAVASCRIPT


Mark C. Reynolds, et. al.


C  O  N  T  E  N  T  S


Introduction

Chapter 1   What Is JavaScript?

Chapter 2   JavaScript: The Language

Chapter 3   Events and JavaScript

Chapter 4   JavaScript Objects

Chapter 5   Built-In JavaScript Objects

Chapter 6   Interactive HTML Objects

Chapter 7   Advanced HTML Objects and Navigation

Chapter 8   Dynamic HTML and Browser Objects

Chapter 9   Creating Your Own JavaScript Objects

Chapter 10   Netscape Plug-Ins and JavaScript

Chapter 11   A Java Tutorial

Chapter 12   More About Java

Chapter 13   VBScript and OLE Controls

Chapter 14   Controlling Web Page Appearance

Chapter 15   Visual Effects

Chapter 16   Creative User Interaction

Chapter 17   JavaScript on the Server

Chapter 18   Tools for JavaScript Development

Chapter 19   Using Frames and Cookies in Advanced Applications

Chapter 20   Learning from the Pros: Site Outlines

Chapter 21   Learning from the Pros: Adding Frames and Controlling Navigation

Chapter 22   Learning from the Pros: Online Ordering System

Appendix A   JavaScript Resources

Appendix B   JavaScript Glossary

Appendix C   JavaScript Commands and Grammar

Appendix D   Current Bugs and Future Enhancements


Copyright 1996 by Que Corporation.

All rights reserved. Printed in the United States of America. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 W. 103rd Street, Indianapolis, IN, 46290. You may reach Que's direct sales line by calling 1-800-428-5331.

ISBN: 0-7897-0789-6


PresidentRoland Elgey PublisherJoseph B. Wikert
Publishing DirectorJim Minatel Editorial Services DirectorElizabeth Keaffaber
Managing EditorSandy Doell Director of MarketingLynn E. Zingraf
Senior Series EditorChris Nelson Acquisitions EditorDoshia Stewart
Product DirectorMark Cierzniak Production EditorsKelli M. Brooks, Jeff Riley
EditorsTom Cirtin, Chuck Hutchinson, Theresa Mathias Assistant Product Marketing Manager Kim Margolius
Software SpecialistsDr. Donald Doherty, Oran J. Sands Technical EditorsDr. Donald Doherty, Stephen Feather, Faisal Jawdat, Chris Means, Joe Risse, Doug Welch, Martin Wyatt
Technical SpecialistNadeem Muhammad Operations CoordinatorPatricia J. Brooks
Editorial AssistantAndrea Duvall Acquisitions AssistantsJane K. Brownlow, Andrea Duvall, Lisa Farley
Book DesignerRuth Harvey Cover DesignersDan Armstrong, Ruth Harvey
Production TeamBrian Buschkill, Chad Dressler, Trey Frank, Jason Hand, Sonja Hart, Damon Jordan, Daryl Kessler, Stephanie Layton, Michelle Lee, Clint Lahnen, Julie Quinn, Kaylene Riemen, Laura Robbins, Bobbi Satterfield
IndexerTim Griffin



About the Authors

Mark C. Reynolds has wide-ranging interests in network programming, UNIX internals, and computer animation. He holds an M.S. degree in mathematics from M.I.T. He has edited and translated a number of works of mathematics, including Stanislaus Ulam's posthumous collection of essays, "Science, Computers and People: From the Tree of Mathematics."

He is Contributing Editor for Web Developer magazine, and co-author of Que's hugely successful title, Client/Server Programming with RPC and DCE. Currently, Mark is a consultant for Adaptive Optics Associates, Inc. (a unit of United Technologies Corporation), where he works on device drivers, image processing, Java, Tcl, and computer special effects. He is also an avid rock climber and mountaineer. Mark can be reached at mark@aoa.aoainc.com.

Ray Daly started the world's first consumer software mail order company in 1978, and a year later he started the first software store in the world. With the store came a customer support BBS, followed later by a fiction BBS called Story Board.

His current online activities include www.onsports.com, "Sponsor/Sponsored Site of the Day," e-mail services including the Capitals mailing list at mailcall.com, Web pages for his family (Janine, Juno, and Red), HTMLjive, and customer dependent consulting.

Rick Darnell is a midwest native currently living with his wife and two daughters in Missoula, Montana. He began his career in print at a small weekly newspaper after graduating from Kansas State University with a degree in broadcasting. While spending time as a freelance journalist and writer, Rick has seen the full gamut of personal computers since starting out with a Radio Shack Model I in the late 1970's. When not in front of his computer, he serves as a volunteer firefighter and member of a regional hazardous materials response team.

Dr. Donald Doherty is a brain scientist and a computer expert. His research into signal processing in both brains and computers keeps him pushing technology to its fullest capacity. Don enjoys sharing some of his adventures through writing about computers and the Internet.

Bill Dortch has developed software professionally for nearly 20 years. In 1995 he founded hIdaho Design, a Web site design company focused on highly interactive, multimedia site development. Prior to starting hD, Bill was Principal Software Architect at Frye Computer Systems, a leading supplier of network management software. Bill's products have been both commercially and critically successful, and have won many awards, including LAN Magazine's Product of the Year, Editor's Choice from PC Magazine, Infoworld, and PC Week, and many others.

A former Boston resident, Bill now lives in Northern Idaho with his cat, Lucky. He can be reached at http://www.hidaho.com or bdortch@hidaho.com.

Mona Everett, Ph.D., is a biochemist turned programmer. She works as a senior scientific software development specialist for Computer Data Systems, Inc., and is currently working on developing front-end access for a large medical epidemiological database. She is expert in Window's Delphi and Visual Basic as well as Mac's HyperCard.

JavaScript is the most recent addition to her programming repertoire. In addition, she can program in C or Pascal for either platform. Because she has taught for many years, she is particularly interested in educational and other software that enables people to use computers and communicate comfortably. You can contact her at everett@txdirect.net or check out her Morphic Molecules pages at http://www.txdirect.net/users/everett.

Scott J. Walter "cut his teeth" in computers on an Apple II (no plus) when he was in the seventh grade. By the time he reached senior high, he was working as an assistant to the computer science teacher and programming in BASIC, FORTRAN, Pascal, and assembly language. He was hired by a Minnesota-based software publisher in 1986, and has been developing retail software ever since. In that time, he has built and directed research and development departments at two companies; taught Pascal, C, C++, Windows, and Macintosh programming at the individual and small-business levels; co-authored (and continues to host the Web site for) The Complete Idiot's Guide to JavaScript; and had the time to invent a recipe for "Cajun-Italian Spaghetti Sauce" with his brother, Matthew.

Scott's current penchants are for Java, JavaScript, VBScript, ActiveX, UNIX, Windows, C++, Delphi, and other budding development technologies. He is currently a "consultant at large" in the Minneapolis area, and invites you to contact him via e-mail at sjwalter@winternet.com or through his home page at http://www.winternet.com/~sjwalter/.

Andrew Wooldridge is assistant Webmaster at Wells Fargo Bank, a pioneer in online banking and Internet services since 1989. Prior to joining Wells Fargo, he was Webmaster of Global Village Communications. Andrew started the HTML Writer's Guild, and has created the popular JavaScript Index at http://www.c2.org/~andreww/javascript/, which receives 40,000 hits per month.

Acknowledgments

The people at Que deserve a large measure of praise for their patience, perseverance, and hard work. I would especially like to thank Doshia Stewart and Kelli Brooks for their ongoing support, guidance, and impressive organizational skills. Many other people at Que have also worked very hard on this book, and are to be congratulated on the outcome.

This book would not have been possible without the strenuous efforts and prodigious technical knowledge of the entire writing team. I am very grateful for their individual contributions, each of which was indispensable.

Mark C. Reynolds

We'd Like To Hear from You!

As part of our continuing effort to produce books of the highest possible quality, Que would like to hear your comments. To stay competitive, we really want you, as a computer book reader and user, to let us know what you like or dislike most about this book or other Que products.

You can mail comments, ideas, or suggestions for improving future editions to the address below, or send us a fax at (317) 581-4663. Our staff and authors are available for questions and comments through our Internet site, at http://www.mcp.com/que, and Macmillan Computer Publishing also has a forum on CompuServe (type GO QUEBOOKS at any prompt).

In addition to exploring our forum, please feel free to contact me personally to discuss your opinions of this book: I'm mcierzniak.que.mcp.com on the Internet, and 76245,476 on CompuServe.

Thanks in advance-your comments will help us to continue publishing the best books available on new computer technologies in today's market.

Mark Cierzniak
Product Director
Que Corporation
201 W. 103rd Street
Indianapolis, Indiana 46290
USA

NOTE
Although we cannot provide general technical support, we're happy to help you resolve problems you encounter related to our books, disks, or other products. If you need such assistance, please contact our Tech Support department at 800-545-5914 ext. 3833.
To order other Que or Macmillan Computer Publishing books or products, please call our Customer Service department at 800-835-3202 ext. 666.



Introduction

Five years ago the Internet was mostly the province of academics and programmers, and the World Wide Web was an obscure idea in the minds of a few researchers. Today both are experiencing explosive growth and unparalleled interest. Web pages are being created at an astonishing rate. The fundamental challenge of Web page development is that while it is easy to create a Web page, it is more difficult to create an attractive and exciting one.

HTML, the markup language that describes the appearance of a page, is easy to learn, and requires no background in programming. HTML has undergone several revisions in order to meet the expanding needs of Web page authors. However, there are limits to what can be achieved inside HTML. The Java programming language was introduced to dramatically extend the Web developer's set of tools, but is still more complex than HTML. Java is very easy to learn; however, like most programming languages, it isn't easy to master. JavaScript bridges this gap.

JavaScript offers the Web page author a new level of sophistication without requiring him to become a programmer. JavaScript brings dynamic and powerful capabilities to Web pages, yet JavaScript is no more difficult to learn than HTML. JavaScript can be used to solve common problems, such as validating forms input, and can also be used to create dramatic and visually appealing content, which would be impossible with HTML. The goal of this book is to completely explore JavaScript, from the mundane to the extraordinary. It is designed as an introduction, a reference, and a continuous source of ideas, so that you may continually improve the Web pages that you create.

Who Should Use This Book?

JavaScript is a very new language-even newer than Java. Despite its newness it has attracted great attention because of its expressive power. This book is directed at anyone who wishes to master that power in order to create more attractive, dynamic, and interesting Web pages.

No programming knowledge is required to benefit from this book, but some knowledge of HTML and Web page authoring is assumed. No prior experience with JavaScript is required, either. This book is designed to be inclusive, and provide information to all JavaScript users, from complete beginners to established experts. If you create Web pages and wish to enliven and enhance them, this book adds JavaScript to your toolbox. If you have already learned JavaScript and wish to go further and break through to complete mastery, this book gives you the information to do so.

How This Book Is Organized

The organization of this book is based on a modular approach to learning JavaScript. The intent is to provide material suitable for all levels of knowledge, from the complete beginner to the advanced JavaScript programmer. To this end the book has five sections.

Part I, "JavaScript the Language," introduces the JavaScript language. The complete syntax and semantics of the language are thoroughly described, with particular attention paid to the close correspondence between HTML elements and JavaScript objects. Chapter 1, "What Is JavaScript?" discusses JavaScript's overall role in the development of Web pages. Chapter 2 "JavaScript: The Language," gives the syntax of JavaScript. This leads directly into a description of the relationship between events on a Web page and JavaScript, in chapter 3, "Events and JavaScript." This is followed by an introduction to the all-important topic of JavaScript objects in chapter 4, "JavaScript Objects."

Part II, "JavaScript Objects," is a greatly expanded presentation of the JavaScript object model that begins in chapter 4 of part I. JavaScript objects can be classified as built-in objects or HTML objects. Built-in objects are thoroughly described in chapter 5, "Built-In JavaScript Objects," while chapters 6 through 8 focus on HTML objects. Validation of HTML forms is the subject of chapter 6; each form element is also a JavaScript object. Navigation objects, such as links and anchors, are then described in chapter 7, while chapter 8 presents the top-level objects associated with the Web browser itself. Part II concludes with a thorough treatment of user-defined objects in chapter 9.

One of the tremendous advantages of a scripting language such as JavaScript is its capability to integrate diverse technologies on a single Web page. Part III is devoted to examining such technologies. Chapter 10 deals with plug-ins, which are becoming increasingly abundant and useful on the World Wide Web. The Java programming language has received massive attention, and is quite similar to JavaScript in structure. Chapter 11 provides a thorough introduction to Java, while chapter 12 focuses on the critical topic of Web page animation using Java. Finally, chapter 13 presents the Visual Basic scripting language in brief, and also looks at its plug-in technology, OLE controls.

Part IV brings the user the most advanced material available on creating special effects using JavaScript. Controlling Web page appearance, producing spectacular visual effects, and fine-tuning user interaction are the subjects of in-depth treatment in chapters 14 through 16. Each chapter contains at least one fully worked example that can be used immediately. JavaScript server technology is reviewed in chapter 17, while various development tools for JavaScript are covered in chapter 18. Part IV concludes with an in-depth look at Web page development using the innovative frames technology in chapter 19.

The fifth part of this book is devoted to Learning from the Pros. This part contains advanced solutions to common, yet difficult problems. Several innovative techniques are described here, as well as pointers on how to enliven any JavaScript Web page. Chapters 20 through 22 describe site outlines in JavaScript, conversion from standard HTML to frames, and a JavaScript online order system.

The book concludes with a series of reference appendixes that summarize critical information presented in the main body of the text. A glossary of common JavaScript terms is given, along with a capsule description of all major JavaScript resources. A language summary is provided, as well as a list of known bugs in the current implementation of JavaScript (version 2.0.1). Future enhancements are also discussed in brief.

How to Use This Book

If you are completely new to JavaScript then you should begin with an in-depth study of the introductory language materials of part I. This should be followed by the more thorough treatment of JavaScript objects in part II. From that point on any chapter or section can be consulted, based on your own particular interest. It should be noted that later chapters are generally more advanced than earlier ones, however.

If you are already familiar with JavaScript then you are encouraged to explore this book in a goal-oriented manner. The alternative technologies discussed in part III may well be new to you, even if you are an experienced Web professional. Finally, parts IV and V should have something new and informative for everyone, as they are intended to help you stretch the limits of JavaScript technology.

Conventions Used in This Book

Que has more than a decade of experience writing and developing the most successful computer books available. With that experience, we've learned what special features help readers the most. Look for these special features throughout the book to enhance your learning experience.

The following font conventions are used in this book to help make reading it easier.

TIP
Tips present short advice on a quick or often overlooked procedures. These include shortcuts.

NOTE
Notes present interesting or useful information that isn't necessarily essential to the discussion. A note provides additional information that may help you avoid problems or offers advice that relates to the topic.

CAUTION
Cautions look like this and warn you about potential problems that a procedure may cause, unexpected results, or mistakes to avoid.

This icon indicates you can also find the related information on the enclosed CD-ROM.