home   Java Script   MS Access   Perl   HTML   Delphi   C ++   Visual Basic   Java   CGIPerl   MS Excel   Front Page 98   Windows 98   Ms Word   Builder   PHP   Assembler     Link to us   Links    


Teach Yourself

              JavaScript 1.1  in a Week

                                  Second Edition

by Arman Danesh


C  O  N  T  E  N  T  S



Introduction

Day 1

Chapter 1  Where Does JavaScript Fit In?

Chapter 2  Your First Script

Day 2

Chapter 3  Working with Data and Information

Chapter 4  Functions and Objects-The Building Blocks of Programs

Day 3

Chapter 5  Events in JavaScript

Chapter 6  Creating Interactive Forms

Day 4

Chapter 7  Loops

Chapter 8  Frames, Documents, and Windows

Day 5

Chapter 9  Remember Where You've Been with Cookies

Chapter 10  Strings, Math, and the History List

Day 6

Chapter 11  Having Fun with JavaScript

Chapter 12  Creating a Spreadsheet in JavaScript

Day 7

Chapter 13  Navigator Gold-A JavaScript Development Tool

Chapter 14  From JavaScript to Java-Looking into the Future

appendixes

appendix A  JavaScript Reference Resources

appendix B  JavaScript Language Reference

appendix C  Glossary

appendix D  What's on the CD-ROM


Credits


To my parents-Michele and Hossain-for instilling in me a thirst for knowledge, and to my wife-Tahirih-who inspires me to press forward.


Copyright © 1996 by Sams.net Publishing

SECOND 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-195-5

HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : leafwriters@leaf.stpn.soft.net

Trademarks

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 Microsystems, Inc.


President, Sams Publishing Richard K. Swadley
Publishing Manager Mark Taber
Managing Editor Cindy Morrow
Director of Marketing John Pierce
Assistant Marketing Manager Kristina Perry


Acquisitions Editor Mark Taber Development Editor Kelly Murdock
Software Development Specialist Bob Correll Production Editor Marla Reece
IndexerChris Wilcox Technical Reviewer Karen Clere, Eric Wolf
Editorial Coordinator Katie Wise Technical Edit Coordinator Lorraine Schaffer
Editorial Assistants Carol Ackerman, Andi Richter, Rhonda Tinch-Mize Cover Designer Tim Amrhein
Book Designer Gary AdairCopy Writer Peter Fuller
Production Team Supervisor Brad Chinn
ProductionBruce Clingaman, Ayanna Lacey, Brad Lenser, Paula Lowell, Rebekah Stutzman


Acknowledgments

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 Bahá'í World Centre in Haifa, Israel, for reading sections of the book as I was writing it.


About the Author

Arman Danesh

Arman Danesh works as the Web Development Specialist at the Bahá'í 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.

Arman may be reached via e-mail at armand@juxta.com.

Tell Us What You Think!

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 book-as 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
Publishing Manager
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290


Introduction

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 aids-all 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 actions-dynamic data that makes information more accessible and easier to organize and digest.

In this book we are going to take a look at JavaScript-the 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.

Goal of this Book

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 3.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 as JavaScript develops maturity as a programming language.

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.

Who Should Read this Book?

The JavaScript scripting language naturally interests a diverse group of people, including Web designers and authors, programmers, and application developers.

Web Authors and Designers

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 end-is 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.

Others

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.

Preparing to Begin

In order to take full advantage of this book, you will need several tools. A copy of the latest version of Netscape 2.0 or 3.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.

Where to Obtain Navigator 3.0

In order to take full advantage of the lessons in this book, it is necessary to have access to a copy of Navigator 3.0 to try the examples and exercises for yourself. Navigator 3.0 is available for most computer platforms, including all versions of Windows, MacOS, 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 3.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.netscape.com/
ftp://ftp2.netscape.com/
ftp://ftp3.netscape.com/
ftp://ftp4.netscape.com/
ftp://ftp5.netscape.com/
ftp://ftp6.netscape.com/
ftp://ftp7.netscape.com/
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.)

Editing and Development Tools

In addition to a copy of Navigator 3.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 few-including leading products such as HoTMetaL-are designed to perform validation of HTML and can't be used to develop JavaScript scripts easily

Conventions in this Book

This book uses certain conventions to aid you, the reader, in your learning process.

A new term is highlighted in italics or with this icon to clarify its meaning.

Note
Note boxes highlight important or explanatory information in the surrounding text.

Tip
These helpful nuggets offer insight or short cuts to programming puzzles.

Warning
Pay special attention to warnings. They may just save your system!

This icon 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.

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 as though they are part of the previous line.

Besides on-screen output, this icon is often used, in this book, to point to a figure that results from the preceding code listing.

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.