www.it-ebooks.info by Emily Vander Veer JavaScript ™ FOR DUMmIES ‰ 4TH EDITION 01_576593 ffirs.qxd 10/12/04 9:55 PM Page iii www.it-ebooks.info 01_576593 ffirs.qxd 10/12/04 9:55 PM Page ii www.it-ebooks.info JavaScript ™ FOR DUMmIES ‰ 4TH EDITION 01_576593 ffirs.qxd 10/12/04 9:55 PM Page i www.it-ebooks.info 01_576593 ffirs.qxd 10/12/04 9:55 PM Page ii www.it-ebooks.info by Emily Vander Veer JavaScript ™ FOR DUMmIES ‰ 4TH EDITION 01_576593 ffirs.qxd 10/12/04 9:55 PM Page iii www.it-ebooks.info JavaScript ™ For Dummies, ® 4th Edition Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 Copyright © 2005 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permis- sion of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, e-mail: brandreview@ wiley.com. Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. JavaScript is a trademark of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR. For general information on our other 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 technical support, please visit www.wiley.com/techsupport. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2004107963 ISBN: 0-7645-7659-3 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 4B/QS/RR/QU/IN 01_576593 ffirs.qxd 10/12/04 9:55 PM Page iv www.it-ebooks.info About the Author Freelance author and Web guru Emily A. Vander Veer has penned several books and countless articles on Internet-related technologies and trends. You can e-mail her at eav@outtech.com. 01_576593 ffirs.qxd 10/12/04 9:55 PM Page v www.it-ebooks.info 01_576593 ffirs.qxd 10/12/04 9:55 PM Page vi www.it-ebooks.info Dedication For the D. Author’s Acknowledgments Many thanks to Gareth Hancock for giving me the opportunity to write the very first edition of this book; to Craig Lukasik, who reviewed this book for technical accuracy; and to all of the other tireless professionals at Wiley, without whom this book wouldn’t have been possible. 01_576593 ffirs.qxd 10/12/04 9:55 PM Page vii www.it-ebooks.info [...]... JavaScript Web page document Image document.myImage HTML form document.myForm Button document.myForm myButton To add JavaScript to a Web page, all you have to do is embed JavaScript code in an HTML file Below the line in which you embed the JavaScript code, you can reference, or call, that JavaScript. .. 336 Using the CD 336 JavaScript For Dummies Chapter Files 337 What You’ll Find 337 If You Have Problems (Of the CD Kind) 338 Index 341 www.it-ebooks.info xvii xviii JavaScript For Dummies, 4th Edition www.it-ebooks.info Introduction W elcome to the wonderful world of Web programming with JavaScript If you’ve worked with HTML before but want to add more flexibility... using JavaScript and the book you’re reading right now you can create all these effects and many more! The Web page in Figure 1-1 shows you an example of the kinds of things that you can look forward to creating for your own site A lot has changed since the previous edition of JavaScript For Dummies came out Perhaps the biggest change is the evolution of DHTML, or dynamic HTML DHTML refers to JavaScript. .. breathtakingly cool Web sites than ever before www.it-ebooks.info 10 Part I: Building Killer Web Pages for Fun and Profit Figure 1-1: JavaScript lets you add interactive features to your Web site quickly and easily Along with this increased power comes increased complexity, unfortunately — but that’s where this new, improved, better-tasting edition of JavaScript For Dummies comes in! Even if you’re not... subject A good one to try is HTML 4 For Dummies, 4th Edition, by Ed Tittel and Natanya Pitts (Wiley Publishing, Inc.) It’s easy! (Sort of) JavaScript has a reputation of being easy to use because ߜ The bulk of the document object model (the portion of the language that defines what kind of components, or objects, you can manipulate in JavaScript) is pretty straightforward ߜ For example, if you want to trigger... coming up www.it-ebooks.info 5 6 JavaScript For Dummies, 4th Edition Take just a second to become familiar with the kind of information you can expect from each icon: This icon flags some of the cool stuff you can find on the CD-ROM included in the back of this book Because all the JavaScript source code listings are on the CD (plus lots more), you can load up the scripts for each section and follow along... Infobahn-wagon, this book’s for you Although I don’t assume that you know HTML, much of what you want to do with JavaScript is interact with objects created by using HTML — so you understand the examples in this book that much quicker if you have a good HTML reference handy One to consider is HTML 4 For Dummies, 4th Edition, by Ed Tittel (Wiley Publishing, Inc.) I do my best to describe how JavaScript works by... this book For more information and instructions on installing the CD-ROM, see the About the CD appendix in the back of this book System Requirements Here’s what you need to get the most out of this book and the enclosed CD-ROM: ߜ A computer with a CD-ROM drive and a modem ߜ A sound card (okay, this one’s strictly optional, but it’s a lot of fun!) www.it-ebooks.info 2 JavaScript For Dummies, 4th Edition. .. but you don’t have to understand what’s going on! Technical Stuff icons point out in-depth information that explains why things work as they do (interesting if you’re in the mood, but not necessary to get the most out of the JavaScript examples I present) www.it-ebooks.info 3 4 JavaScript For Dummies, 4th Edition Foolish Assumptions Everybody’s got to start somewhere, right? I’m starting out with the... www.it-ebooks.info xv xvi JavaScript For Dummies, 4th Edition Taking Advantage of Others’ Experience 282 Exercising the Time-Honored Trial-and-Error Approach 283 Just Try and Catch Me Exception Handling! 283 Taking Advantage of Debugging Tools 287 Netscape’s JavaScript console 288 Microsoft Internet Explorer’s built-in error display .290 Part VI: Appendixes 293 Appendix A: JavaScript . Vander Veer JavaScript ™ FOR DUMmIES ‰ 4TH EDITION 01_576593 ffirs.qxd 10/12/04 9:55 PM Page iii www.it-ebooks.info JavaScript ™ For Dummies, ® 4th Edition Published. Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies. com, and