Arman Danesh
201 West 103rd Street, Indianapolis, Indiana 46290
To my beloved wife, Tahirih. Because of her, for her, and with her support I succeed and move 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-084-3
Library of Congress Catalog Card Number: 96-67127
99 - 98 - 97 - 96 ------ 4 - 3 - 2 - 1
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. JavaScript is a trademark of Sun Microsystem Inc.
President, Sams Publishing:
Richard K. Swadley
Publishing Manager
Mark Taber
Managing Editor:
Cindy Morrow
Director of Marketing:
John Pierce
Assistant Marketing Managers:
Kristina Perry
Writing this book has been a process that has involved many people. I would like to thank the Sams.net staff who worked on the development of this book, in particular; Kelly Murdock, Howard Jones, and Mark Taber, as well as all those others who contributed in so many ways.
Arman Danesh works as the Web Development Specialist at the Bahá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.
Wes Tatters (wtatters@cnrstone.com) is a computer programmer, systems designer, and writer. He is the author of Teach Yourself Netscape Web Publishing in a Week, Navigating the Internet with America Online, and Navigating the Internet with CompuServe (all published by Sams.net).
As a reader, you are the most important critic and commentator of our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.
Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at http://www.mcp.com.
Note:
If you have a technical question about this book, call the technical support line at (800) 571-5840, ext. 3668.
As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this bookas well as what we can do to make our books stronger. Here's the information:
FAX: 317/581-4669
E-mail: newtech_mgr@sams.mcp.com
Mail: Mark Taber
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290
Since Netscape introduced Navigator 2 in 1995, JavaScript has come to be accepted by many as a useful tool for adding interactivity to otherwise static Web pages.
In contrast to Java, which provides powerful capabilities but requires a strong programming background to manage, JavaScript offers a simple way to develop and deploy applications on a Web page.
Presently, JavaScript is the internal scripting language of Netscape's Navigator 3 and is one of two scripting languages available in Microsoft's Internet Explorer 3.0 Web browser. In addition, other vendors have indicated their interest in including JavaScript in their products in the future.
JavaScript has also moved from the client environment to be included as the scripting language in LiveWire, Netscape's client-server application development environment. LiveWire extends a server to support scripted client-server applications.
Given its integration into these leading browser and server products, JavaScript is an important tool for anyone developing sophisticated, interactive applications deployed using Web technology.
This book is designed to provide a firm understanding of JavaScript as it is presently deployed in Navigator, Internet Explorer, and LiveWire. After reading this book, you should be able to develop sophisticated client- and server-based applications using JavaScript embedded in HTML documents.
The book begins with a look at the basics of the JavaScript language, including its syntax, key objects and functions. and how to work with data and variables.
Part Two examines the client JavaScript as its implemented in Navigator 3. This includes coverage of the Navigator Object Tree, processing events triggered in the client, designing and producing interactive forms, maintaining client-side state information with Cookies, and using JavaScript to work with multiple frames.
Part Three moves to the server end and takes a look at the JavaScript implementation in the LiveWire and LiveWire Pro server extensions. Here you see how it's possible to produce server-based applications that can track individual clients using the application and generate dynamic content on the server before sending it to the client.
The fourth section looks at LiveConnect, Netscape's mechanism for connecting Java, plug-ins, and JavaScript in Navigator 3. Using LiveConnect, it's possible to call methods of Java applets from inside JavaScript, to do the same for plug-ins, and for Java applets to call JavaScript functions and methods. LiveConnect extends the functionality of all three beyond their own limitations allowing, for instance, JavaScript to produce Java graphical controls or for HTML buttons to be used to control a plug-in by using JavaScript as the middle layer.
Part Five provides four examples of complete JavaScript-based applications:
Finally, Part Six looks at Microsoft Internet Explorer. In addition to covering the JavaScript implementation in Internet Explorer and the Internet Explorer Object Model, Part Six also covers other additional features of Internet Explorer:
In addition, the appendixes provide a complete language reference, an HTML guide, a list of character entities and color names and values, and an introduction to Navigator GoldNetscape's integrated Web page editor and browser.
This book is designed as a developer's guide rather than a beginner's introduction to JavaScript. It is of interest to any Web developer looking to add interactivity to a Web sites, and application developers who want to deploy network-oriented client-server applications on an Intranet, or on the Internet, using Web technology to deliver applications to the desktop.
This book assumes that the reader has experience developing Web sites that use HTML, including forms, tables, and frames, and likely has experience using the Common Gateway Interface (CGI)either on UNIX systems using PERL, C, or tcl, or on Windows or Mac systems.
It's helpful in grasping the programming concepts used in this book if you have previous programming experience in a structured programming languages like C, C++, Pascal, or Java. These concepts include arrays, procedures and functions, loops, variable scope, and logical operators.
In order to take full advantage of this book, it is useful to have a copy of Navigator 3 to test the examples to experiment for yourself. Navigator 3 is available free of charge on all Windows platforms, for the MacOS, and for most major UNIX systems. You can download a copy from Netscape's Web page at http://home.netscape.com/, or from one of their numerous ftp servers and mirrors:
ftp://ftp.leo.chuu.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://susnite.doc.ic.ac.uk/computing/information-systems/www/Netscape/ (United Kingdom)
ftp://ftp.infomratik.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.)
If you want to try Internet Explorer as an alternative browser, go to Microsoft's Internet Explorer Web page http://www.microsoft.com/ie.
In addition to a JavaScript-capable browser, it's important to find an editing tool with which you feel comfortable.
If you already do a lot of HTML coding, you probably have a favorite text editor which should be suitable for JavaScript programming. Basically, any text editor that produces ASCII text files can be used to editor JavaScript code.
You also may also want to look at Netscape Navigator 3.0 Gold, available from the many Netscape ftp servers and mirrors, as an alternative to a plain text editor. Navigator Gold combines the Navigator browser with a WYSIWYG (what you see is what you get) editing environment for HTML and JavaScript.
The ActiveX Control Pad from Microsoft, discussed in Part Six, also provides a functional tool for adding scripts to Web pages based on a model of providing actions for events. It probably represents the first truly drag-and-drop development environment for JavaScript (as well as VBScript, an alternative scripting language in Internet Explorer 3).