This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] • • • Table of Contents Index Examples JavaScript™ by Example By Ellie Quigley Publisher: Prentice Hall Pub Date: June 19, 2003 ISBN: 0-13-140162-9 Pages: 752 In JavaScript by Example, Ellie Quigley teaches JavaScript using the same proven approach that's made her the world's favorite scripting author and instructor This book's unique and classroom-tested learning path contains hundreds of small, easy-to-understand examples that demonstrate the full range of JavaScript's power One step at a time, Quigley guides new JavaScript programmers through every essential technique, from script setup to advanced DOM and CSS programming Coverage includes: Data types, literals, and variables Operators, control structures, and functions Building dialog boxes and working with JavaScript objects using the Browser object: navigator, windows, frames, dynamic images, and links Event handling, regular expressions, and form validation The JavaScript Document Object Model (DOM), Cascading Style Sheets (CSS), and Dynamic HTML Whether you're a first-time or long-time scripter, a Web professional or sysadmin, Ellie Quigley will help you write rocksolid JavaScripts that get the job done [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] • • • Table of Contents Index Examples JavaScript™ by Example By Ellie Quigley Publisher: Prentice Hall Pub Date: June 19, 2003 ISBN: 0-13-140162-9 Pages: 752 Copyright About Prentice Hall Professional Technical Reference Preface Acknowledgments Chapter Introduction to JavaScript Section 1.1 What JavaScript Is Section 1.2 What JavaScript Is Not Section 1.3 What JavaScript Is Used For Section 1.4 JavaScript and Events Section 1.5 What Versions? What Browsers? Section 1.6 Where to Put JavaScript> Section 1.7 JavaScript and Old or Disabled Browsers Section 1.8 JavaScript from External Files EXERCISES Chapter Script Setup Section 2.1 The HTML Document and JavaScript Section 2.2 Syntactical Details Section 2.3 Generating HTML and Printing Output Section 2.4 About Debugging EXERCISES Chapter The Building Blocks: Data Types, Literals, and Variables Section 3.1 Data Types Section 3.2 Variables Section 3.3 Bugs to Watch For EXERCISES Chapter Dialog Boxes Section 4.1 Interacting with the User This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Section 4.1 Interacting with the User EXERCISES Chapter Operators Section 5.1 About JavaScript Operators and Expressions Section 5.2 Types of Operators Section 5.3 Number, String, or Boolean? Datatype Conversion Section 5.4 Special Operators EXERCISES Chapter Under Certain Conditions Section 6.1 Control Structures, Blocks, and Compound Statements Section 6.2 Conditionals Section 6.3 Loops EXERCISES Chapter Functions Section 7.1 What Is a Function? Section 7.2 Debugging EXERCISES Chapter Objects Section 8.1 What Are Objects? Section 8.2 User-Defined Objects Section 8.3 Manipulating Objects EXERCISES Chapter JavaScript Core Objects Section 9.1 What Are Core Objects? Section 9.2 Array Objects Section 9.3 Array Properties and Methods Section 9.4 The Date Object Section 9.5 The Math Object Section 9.6 What Is a Wrapper Object? EXERCISES Chapter 10 The Browser Objects: Navigator, Windows, and Frames Section 10.1 JavaScript and the Browser Object Model EXERCISES Chapter 11 The Document Objects: Forms, Images and Links Section 11.1 The Document Object Model Section 11.2 Introduction to Forms Section 11.3 Introduction to Images Section 11.4 Introduction to Links EXERCISES Chapter 12 Handling Events Section 12.1 Introduction to Event Handlers Section 12.2 Event Handlers as JavaScript Methods Section 12.3 Handling a Window or Frame Event Section 12.4 Handling Mouse Events Section 12.5 Handling Link Events Section 12.6 Handling a Form Event Section 12.7 Handling Key Events: onKeyPress, onKeyDown, and onKeyUp Section 12.8 Handling Error Events Section 12.9 The event Object EXERCISES Chapter 13 Regular Expressions and Pattern Matching Section 13.1 What Is a Regular Expression? Section 13.2 Creating a Regular Expression This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Section 13.2 Creating a Regular Expression Section 13.3 Getting Control—The Metacharacters Section 13.4 Form Validation with Regular Expressions EXERCISES Chapter 14 Cookies Section 14.1 What Are Cookies? Section 14.2 Creating a Cookie with JavaScript EXERCISES Chapter 15 Dynamic HTML: Style Sheets, the DOM, and JavaScript Section 15.1 What Is Dynamic HTML? Section 15.2 What Is a Style Sheet? Section 15.3 Types of Style Sheets Section 15.4 Where Does JavaScript Fit In? EXERCISES Appendix A JavaScript Web Resources Appendix B HTML Documents: A Basic Introduction Section B.1 Intro to the Intro Section B.2 What Is HTML? Section B.3 HTML Tags Section B.4 The Minimal HTML Document Section B.5 Character Formatting Section B.6 Linking Section B.7 Adding Comments Section B.8 Case Sensitivity Section B.9 Graphics and Images Section B.10 Troubleshooting Section B.11 Metatags, Cascading Style Sheets, and Java Section B.12 Looking Behind the Scenes (or, What Did We Do Before the Right-Click?) Section B.13 What About Frames? Section B.14 Some Final Thoughts Appendix C CGI and Perl: The Hyper Dynamic Duo Section C.1 What Is CGI? Section C.2 Internet Communication Between Client and Server Section C.3 Creating a Web Page with HTML Section C.4 How HTML and CGI Work Together Section C.5 Log Files Section C.6 Where to Find CGI Applications Section C.7 Getting Information Into and Out of the CGI Script Section C.8 Processing Forms with CGI Section C.9 The CGI.pm Module EXERCISE C.1 EXERCISE C.2 License Agreement and Limited Warranty About the CD-ROM Using the CD-ROM Contents System Requirements License Agreement Technical Support Index [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Copyright Library of Congress Cataloging-in-Publication Data Quigley, Ellie JavaScript by example / Ellie Quigley p cm ISBN 0-13-140162-9 JavaScript (Computer program language) I Title QA76.73.J39Q54 2003 005.13'3 dc21 2003050630 Production editor/compositor: Vanessa Moore Cover design director: Jerry Votta Cover designer: Anthony Gemmellaro Manufacturing buyer: Maura Zaldivar Editor-in-chief: Mark Taub Editorial assistant: Sarah Hand Marketing manager: Curt Johnson Full-service production manager: Anne R Garcia © 2004 by Pearson Education, Inc Publishing as Prentice Hall Professional Technical Reference Upper Saddle River, NJ 07458 Prentice Hall books are widely used by corporations and government agencies for training, marketing, and resale The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales For more information, please contact: U.S Corporate and Government Sales 1-800-382-3419; corpsales@pearsontechgroup.com For sales outside of the U.S., please contact: International Sales 1-317-581-3793; international@pearsontechgroup.com All products or services mentioned herein are the trademarks or registered trademarks of their respective owners All rights reserved No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher JavaScript 1.5 Core Reference © 2000 Netscape Communications Corporation All rights reserved JavaScript is a registered trademark of Sun Microsystems used under license for technology developed and owned by Netscape All other trademarks are the exclusive property of their respective owners Learn more about Netscape DevEdge at http://devedge.netscape.com/ or find links to Netscape's browser download site at http://channels.netscape.com/ns/browsers/download.jsp Printed in the United States of America 1st Printing Pearson Education Ltd Pearson Education Australia PTY, Limited Pearson Education Singapore, Pte Ltd Pearson Education North Asia Ltd This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Pearson Education North Asia Ltd Pearson Education Canada, Ltd Pearson Educación de Mexico, S.A de C.V Pearson Education—Japan Pearson Education Malaysia, Pte Ltd [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] About Prentice Hall Professional Technical Reference With origins reaching back to the industry's first computer science publishing program in the 1960s, and formally launched as its own imprint in 1986, Prentice Hall Professional Technical Reference (PH PTR) has developed into the leading provider of technical books in the world today Our editors now publish over 200 books annually, authored by leaders in the fields of computing, engineering, and business Our roots are firmly planted in the soil that gave rise to the technical revolution Our bookshelf contains many of the industry's computing and engineering classics: Kernighan and Ritchie's C Programming Language, Nemeth's UNIX System Adminstration Handbook, Horstmann's Core Java, and Johnson's High-Speed Digital Design PH PTR acknowledges its auspicious beginnings while it looks to the future for inspiration We continue to evolve and break new ground in publishing by providing today's professionals with tomorrow's solutions [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Preface After years of teaching scripting languages, I recently decided it was time to try teaching JavaScript, and went on a search for the right book for my students There were so many JavaScript books on the bookshelves, it was hard to know where to begin I started buying one at a time, reading and sifting through them, always feeling that something was missing Like Cinderella's shoe, I couldn't find the right fit Either the book was too heavy on the technical side, but lacking in pictures and examples, or it was filled with slide shows, rollovers, and scrolling banners, but brief on the technical side I wanted something that did both, a book that would demonstrate both the technical and the fun elements of JavaScript I searched and searched, and found many excellent sources, but in the end, I couldn't find the perfect book for my class, so I decided to write my own With books and manuals piled high around my computer desk, I weeded through the best and the worst, gleaning out information and beginning to compile my new "By Example" book I browsed through Internet tutorials and student guides, and studied Web pages, always looking for the best way to get the material across I even delved into Adobe Photoshop and Macromedia Dreamweaver, while roaming from Maine to San Francisco with my digital camera, snapping pictures that would enhance my examples and add to the colorful side of JavaScript And after much cutting and pasting, cropping and trimming, testing and trying, a new book has evolved—the one I was looking for The shoe finally fits! [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Acknowledgments Many thanks go to the folks at Prentice Hall: Mark L Taub, editor-in-chief, and the most supportive person I know; Vanessa Moore, production editor, for being the very best in her business and for polishing a rough diamond into a gem Thanks also to Dan Livingston and Tony Arguelles for technical proofing; Frank Peters for starting the idea; Steve Dobbins for setting the stage in San Francisco; my daughter, Jody Savage, for early proofing; and baby William and Christian for posing for pictures Ellie Quigley May, 2003 [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Chapter Introduction to JavaScript [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [P] [Q] [R] [S] [T] [U] [W] Query Query Strings QUERY_STRING environment variable [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [P] [Q] [R] [S] [T] [U] [W] Root directory 2nd [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [P] [Q] [R] [S] [T] [U] [W] Server 2nd 3rd Server Side Includes split Function SSI Standard input Status code Status codes Submit button System Calls [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [P] [Q] [R] [S] [T] [U] [W] TCP/IP telnet text/html text/plain [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [P] [Q] [R] [S] [T] [U] [W] URL 2nd 3rd 4th 5th [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [P] [Q] [R] [S] [T] [U] [W] WAIS Web pages Web server Web sites [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Brought to You by Like the book? Buy it! ... comparable to JavaScript called JScript The Table 1.2 lists versions of both JavaScript and JScript Table 1.2 JavaScript and JScript JavaScript or JScript Version Browsers Supported JavaScript 1.0... URL) < /script> < /script> Example 1.6 First JavaScript Sample