Bible Series - JavaScript Examples.pdf - 4855-7 Cover 9:16 AM Page 1 The essential companion to your JavaScript Bible \u2022 Experiment with proprietary

Bible Series - JavaScript Examples.pdf - 4855-7 Cover 9:16...

This preview shows page 1 out of 631 pages.

You've reached the end of your free preview.

Want to read all 631 pages?

Unformatted text preview: 4855-7 Cover 6/19/01 9:16 AM Page 1 The essential companion to your JavaScript Bible . . . • Experiment with proprietary and industry-standard DOM features in Internet Explorer 5.x and Netscape Navigator 6 • See the latest object-detection coding techniques to accommodate a wide range of browsers today and tomorrow Apply concepts to real applications • Learn how to apply language specifics to real application challenges • Gain insight from extensive code listings and Danny’s analysis of coding strategy and design decisions Experiment in interactive labs ™ CD-ROM includes: 100% ONE HUNDRED PERCENT ™ • Interact with DOM and JavaScript core language terminology in real time with Danny’s exclusive workbench: The Evaluator C O M P R E H E N S I V E JavaScript Examples Bible Inside, you’ll find ready-to-run examples and step-by-step guides 100% The Essential Companion to JavaScript Bible Even the giant 4th Edition and massive Gold Edition of the JavaScript Bible couldn’t hold all of the superior knowledge and experience of scripting guru Danny Goodman. Examples sections from reference Parts III and IV appeared only on the CD-ROMs of those two books. Now, in this JavaScript Examples Bible, we bring you the Document Object Model (DOM) and core JavaScript language examples sections in their entirety in convenient form — plus new introductory sections by Danny to guide you to the most important concepts and examples in each chapter. COMPREHENSIVE AUTHORITATIVE WHAT YOU NEED ONE HUNDRED PERCENT Immerse yourself in a JavaScript master’s code examples and analysis Learn JavaScript and DOM behavior via interactive labs Complete your JavaScript Bible library with this invaluable supplement System Requirements: PC running Windows 95 or later, Windows NT 4 or later; Power Macintosh running System 7.6 or later. See Appendix A for details and complete system requirements. *85 5 -AJE Fb $29.99 USA $44.99 Canada £24.99 UK incl. VAT Reader Level: Shelving Category: Beginning to Advanced Web Development/JavaScript ISBN 0-7645-4855-7 ,!7IA7G4-feif a!:p;M;t;t;T GOODMAN Hundreds of Example Scripts on CD-ROM! avaScript J Examples Bible ™ • A searchable e-version of this book • Over 300 ready-to-run scripts from the JavaScript Bible, Gold Edition The perfect complement to your 4th or Gold Edition JavaScript Bible CD-ROM INSIDE! Over 300 Ready-to-Run Example Scripts and More on CD-ROM! The Essential Companion to JavaScript ™ Bible Danny Goodman Author of the bestselling JavaScript Bible 4855-7 FM.F 6/26/01 8:33 AM Page i Praise for Danny Goodman’s JavaScript Bible “JavaScript Bible is the definitive resource in JavaScript programming. I am never more than three feet from my copy.” — Steve Reich, CEO, PageCoders “This book is a must-have for any Web developer or programmer.” — Thoma Lile, President, Kanis Technologies, Inc. “Outstanding book. I would recommend this book to anyone interested in learning to develop advanced Web sites. Mr. Goodman did an excellent job of organizing this book and writing it so that even a beginning programmer can understand it.” — Jason Hensley, Director of Internet Services, NetVoice, Inc. “Goodman is always great at delivering clear and concise technical books!” — Dwayne King, Chief Technology Officer, White Horse “JavaScript Bible is well worth the money spent!” — Yen C.Y. Leong, IT Director, Moo Mooltimedia, a member of SmartTransact Group “A must-have book for any Internet developer.” — Uri Fremder, Senior Consultant, TopTier Software “I love this book! I use it all the time, and it always delivers. It’s the only JavaScript book I use!” — Jason Badger, Web Developer “Whether you are a professional or a beginner, this is a great book to get.” — Brant Mutch, Web Application Developer, Wells Fargo Card Services, Inc. “I never thought I’d ever teach programming before reading your book [JavaScript Bible]. It’s so simple to use — the Programming Fundamentals section brought it all back! Thank you for such a wonderful book, and for breaking through my programming block!” — Susan Sann Mahon, Certified Lotus Instructor, TechNet Training “I continue to get so much benefit from JavaScript Bible. What an amazing book! Danny Goodman is the greatest!” — Patrick Moss “Danny Goodman is very good at leading the reader into the subject. JavaScript Bible has everything we could possibly need.” — Philip Gurdon 4855-7 FM.F 6/26/01 8:33 AM Page ii “An excellent book that builds solidly from whatever level the reader is at. A book that is both witty and educational.” — Dave Vane “I continue to use the book on a daily basis and would be lost without it.” — Mike Warner, Founder, Oak Place Productions “JavaScript Bible is by far the best JavaScript resource I’ve ever seen (and I’ve seen quite a few).” — Robert J. Mirro, Independent Consultant, RJM Consulting 4855-7 FM.F 6/26/01 8:33 AM Page iii JavaScript Examples Bible: The Essential Companion to JavaScript Bible ™ ™ 4855-7 FM.F 6/26/01 8:33 AM Page iv 4855-7 FM.F 6/26/01 8:33 AM Page v JavaScript Examples Bible: The Essential Companion to JavaScript Bible ™ ™ Danny Goodman Best-Selling Books • Digital Downloads • e-Books • Answer Networks • e-Newsletters • Branded Web Sites • e-Learning Indianapolis, IN ✦ Cleveland, OH ✦ New York, NY 4855-7 FM.F 6/26/01 8:33 AM Page vi JavaScript™ Examples Bible: The Essential Companion to JavaScript™ Bible Published by: Hungry Minds, Inc. 909 Third Avenue New York, NY 10022 Copyright © 2001 Danny Goodman. All rights reserved. No part of this book, including interior design, cover design, and icons, may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or otherwise) without the prior written permission of the publisher. Library of Congress Control No.: 2001091964 ISBN: 0-7645-4855-7 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/RY/QX/QR/IN Distributed in the United States by Hungry Minds, Inc. Distributed by CDG Books Canada Inc. for Canada; by Transworld Publishers Limited in the United Kingdom; by IDG Norge Books for Norway; by IDG Sweden Books for Sweden; by IDG Books Australia Publishing Corporation Pty. Ltd. for Australia and New Zealand; by TransQuest Publishers Pte Ltd. for Singapore, Malaysia, Thailand, Indonesia, and Hong Kong; by Gotop Information Inc. for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft for South Africa; by Eyrolles for France; by International Thomson Publishing for Germany, Austria, and Switzerland; by Distribuidora Cuspide for Argentina; by LR International for Brazil; by Galileo Libros for Chile; by Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer Publishing Corporation, Inc., for the Philippines; by Contemporanea de Ediciones for Venezuela; by Express Computer Distributors for the Caribbean and West Indies; by Micronesia Media Distributor, Inc. for Micronesia; by Chips Computadoras S.A. de C.V. for Mexico; by Editorial Norma de Panama S.A. for Panama; by American Bookshops for Finland. For general information on Hungry Minds’ products and services please contact our Customer Care department; within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002. For sales inquiries and resellers information, including discounts, premium and bulk quantity sales and foreign language translations please contact our Customer Care department at 800-434-3422, fax 317-572-4002 or write to Hungry Minds, Inc., Attn: Customer Care department, 10475 Crosspoint Boulevard, Indianapolis, IN 46256. For information on licensing foreign or domestic rights, please contact our Sub-Rights Customer Care department at 212-884-5000. For information on using Hungry Minds’ products and services in the classroom or for ordering examination copies, please contact our Educational Sales department at 800-434-2086 or fax 317-572-4005. For press review copies, author interviews, or other publicity information, please contact our Public Relations department at 317-572-3168 or fax 317-572-4168. For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 978-750-4470. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES. Trademarks: JavaScript is a registered trademark or trademark of Sun Microsystems, Inc. All other trademarks are property of their respective owners. Hungry Minds, Inc., is not associated with any product or vendor mentioned in this book. is a trademark of Hungry Minds, Inc. 4855-7 FM.F 6/26/01 8:33 AM Page vii About the Author Danny Goodman is the author of numerous critically acclaimed and bestselling books, including The Complete HyperCard Handbook, Danny Goodman’s AppleScript Handbook, and Dynamic HTML: The Definitive Reference. He is a renowned authority and expert teacher of computer scripting languages and is widely known for his “JavaScript Apostle” articles in Netscape’s ViewSource online developer newsletter. His writing style and pedagogy continue to earn praise from readers and teachers around the world. To help keep his finger on the pulse of real-world programming challenges, Goodman frequently lends his touch as consulting programmer and designer to leading-edge World Wide Web and intranet sites from his home base in the San Francisco area. 4855-7 FM.F 6/26/01 8:33 AM Page viii Credits Acquisitions Editor Sharon Cox Project Editor Neil Romanosky Technical Editor David Wall Copy Editors Jerelind Charles Victoria Lee O’Malley Editorial Manager Colleen Totz Project Coordinator Regina Snyder Graphics and Production Specialists Gabriele McCann Betty Schulte Jeremey Unger Erin Zeltner Quality Control Technicians Laura Albert David Faust Andy Hollandbeck Permissions Editor Laura Moss Media Development Specialist Greg Stephens Media Development Coordinator Marisa Pearman Book Designer Kurt Krames Proofreading and Indexing TECHBOOKS Production Services Cover Illustrator Kate Shaw 4855-7 FM.F 6/26/01 8:33 AM Page ix Preface A common thread running throughout most of my computer-book–writing career is that I tend to write a book I wish I had had in order to learn a new technology in the first place. Because I must write that book without the benefit of existing models, I begin by doing my best to master the technology, and then I write the book to help other newcomers learn as much as I did, but more quickly and with less pain, anguish, and confusion. To accomplish that goal, I write as much content as I feel is necessary to cover the topic in the depth that my readers require. When I started on what became the 4th and Gold editions of the JavaScript Bible, there were models to follow (my previous three editions) plus a substantial amount of brand new material, much of which had not yet been documented anywhere. I also assumed the responsibility of integrating the frequently conflicting and competing philosophies of the ways the JavaScript language is applied to a variety of browser brands and versions. Resolving these conflicts is a challenge that I face in my own programming work with clients, and I take great pleasure in sharing my solutions and approaches with other programmers floating in the same boat. As my editor and I began counting the pages I had assembled for these new editions, we discovered that the number of pages far outstripped the printer’s binding capabilities, even in a thicker volume made possible by using a hard cover (the Gold edition). Certainly not all of the words that I had written were so precious that some of them couldn’t be cut. But we were hundreds of pages beyond capacity. To cut that much content would have forced exclusion of coverage of language or document object model vocabulary. Fortunately, as had been done in previous editions, the plan for the new editions included Adobe Acrobat versions of the books on the accompanying CD-ROM. Although a significant compromise to ease of reading, it was possible to move some of the book’s content to the CD-ROM and leave the most important parts on the printed page. For the softcover 4th edition, reference chapters covering less-used or advanced subjects were pulled from print; for the hardcover Gold edition, which was longer and targeted more for professional scripters, the advanced chapters were put back into the book (along with 15 additional chapters for that edition), and the JavaScript tutorial was exiled to the CD-ROM. But even after making the difficult decisions about which chapters could go to the CD-ROMs, the page counts for both volumes were still excessive. Something else — something big — had to go. The remaining bundle that could free us from the page 4855-7 FM.F x 6/26/01 8:33 AM Page x JavaScript Example Bible: The Essential Companion to JavaScript Bible count devil was all of the Example sections from the reference vocabulary. By being nondiscriminatory about these extractions — that is, extracting all of them instead of only selected sections — we could convey to readers a consistent organizational model. In the end, the extracted Example sections from Parts III and IV found their way into Appendix F on the CD-ROMs of both editions of the larger tome. I knew that as a reader of my own books (and one of a certain age at that) I would not enjoy having to flip back and forth between book and screen to refresh my memory about a term and see it in action. A more pleasing solution for many JavaScript Bible readers would be a separate volume containing a printed version of the Examples sections. The new volume would act as a companion to both the 4th and Gold editions of the JavaScript Bible. Using Appendix F as a starting point, I divided the content into chapters along the same lines as the JavaScript Bible reference sections. This also gave me a chance to study the examples for each chapter with fresh eyes. The examples haven’t changed, but I had the opportunity to direct the reader’s attention to examples that I thought were particularly helpful in mastering a document-level or core language object. Thus, each chapter of this book begins with a scene-setting introduction and a list of highlights to which you should pay special attention. Also, since you will likely be scanning through the book from time to time, I added many illustrations of the pages produced from the code listings. These figures will help you visualize what important listing code does when the page is loaded into a browser. Now you know the story behind the JavaScript Examples Bible. Some budgetconscious readers may not be thrilled to pay more for what appears to be a printout of content they already own in electronic format. If so, then please continue using the Acrobat version. But if, like me, you enjoy the portability and visual scanability of a printed work, then keeping this book near your JavaScript Bible volume will enhance your learning and research activities. Organization and Features of This Book Almost all chapters in this book correspond to similarly named chapters in Parts III and IV from the JavaScript Bible 4th and Gold editions. Although chapters in this book are consecutively numbered starting with Chapter 1, each chapter title includes a reference to the corresponding chapter number from the big books. For example, Chapter 1 of this book provides the Examples sections for terms related to generic HTML elements. That subject is covered in Chapter 15 of the big books. There is not always a one-to-one relationship between chapters. Several chapters of the big books have no Examples sections in them because sample code is embedded as part of the big book text. Therefore, don’t be surprised to see gaps in pointers to JavaScript Bible reference chapters. 4855-7 FM.F 6/26/01 8:33 AM Page xi Preface Listing numbers are derived from their original order in what had been planned as a contiguous volume. Such listing numbers are the ones referred to in the “On the CD-ROM” pointers throughout Parts III and IV of the big books. This should help you locate an example’s listing when you reach one of those pointers in the JavaScript Bible. Notice, too, that the big books’ running footers with property, method, and event handler names appear in this book, too. Therefore, if you should be looking at an example listing of this book and wish to consult the more detailed discussion of the subject in the large book, turn to the corresponding big book chapter and locate the corresponding terminology within the object’s chapter. Many examples throughout this book refer to The Evaluator. This Web page application is described at length in Chapter 13 of the big books. You can find the file for The Evaluator within the Listings\Chap13 folder on the CD-ROM for either the big book or this book. CD-ROM The accompanying CD-ROM contains the complete set of over 300 ready-to-run HTML documents from the JavaScript Bible, Gold Edition. These include listings for both the Examples sections in this book and all other listings from the Gold edition. You can run these examples with your JavaScript-enabled browser, but be sure to use the index.html page in the Listings folder as a gateway to running the listings. This page shows you the browsers that are compatible with each example listing. The Quick Reference from Appendix A of the big books is in .pdf format on the CD-ROM for you to print out and assemble as a handy reference, if desired. Adobe Acrobat Reader is included on the CD-ROM so that you can read this .pdf file. Finally, the text of the book is in a .pdf file format on the CD-ROM for easy searching. Formatting and Naming Conventions The script listings and words in this book are presented in a monospace font to set them apart from the rest of the text. Because of restrictions in page width, lines of script listings may, from time to time, break unnaturally. In such cases, the remainder of the script appears in the following line, flush with the left margin of the listing, just as they would appear in a text editor with word wrapping turned on. If these line breaks cause you problems when you type a script listing into a document yourself, I encourage you to access the corresponding listing on the CD-ROM to see how it should look when you type it. To make it easier to spot in the text when a particular browser and browser version is required, most browser references consist of a two-letter abbreviation and a version number. For example, IE5 means Internet Explorer 5 for any operating system; xi 4855-7 FM.F xii 6/26/01 8:33 AM Page xii JavaScript Example Bible: The Essential Companion to JavaScript Bible NN6 means Netscape Navigator 6 for any operating system. If a feature is introduced with a particular version of browser and is supported in subsequent versions, a plus symbol (+) follows the number. For example, a feature marked IE4+ indicates that Internet Explorer 4 is required at a minimum, but the feature is also available in IE5, IE5.5, and so on. Occasionally, a feature or some highlighted behavior applies to only one operating system. For example, a feature marked IE...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture