Andrew Watt and Jonathan Watt with Jinjer Simon and Jim O’Donnell
Teach Yourself
JavaScript in
21 Days
800 East 96th St., Indianapolis, Indiana, 46240 USA
™
Sams Teach Yourself JavaScript™ in 21 Days
ACQUISITIONS EDITOR
Copyright © 2002 by Sams Publishing
Jonathan Steever
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. International Standard Book Number: 0-672-32297-8 Library of Congress Catalog Card Number: 2001092863 Printed in the United States of America
03
02
6
DEVELOPMENT EDITOR MANAGING EDITOR Charlotte Clapp
PROJECT EDITOR Tony Reitz
COPY EDITOR Karen Whitehouse
INDEXER Larry Sweazy
PROOFREADER Cindy Long
TECHNICAL EDITOR
First Printing: May 2002 04
Betsy Brown
Jim O’Donnell 5
4
3
Trademarks
TEAM COORDINATOR Amy Patton
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams 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.
INTERIOR DESIGN
Warning and Disclaimer
PRODUCTION
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
Gary Adair
COVER DESIGN Aren Howell Ayanna Lacey
Contents at a Glance Introduction Week 1
1 7
1
Getting the Basics Right
2
Working with Data
47
3
Functions and Statements
79
4
JavaScript Is Object-Based
119
5
An Introduction to Arrays
161
6
HTML Forms and the String Object
201
7
Numbers and Math
249
Week 2
9
283
8
The Browser Issue
285
9
Date and Time Manipulation
307
10
Events and Event Handling
331
11
Dynamic HTML
357
12
Windows and Frames
383
13
Regular Expressions Make It Easier
415
14
Advanced Array Management
453
Week 3
493
15
Debugging and Error Handling
495
16
Cookies: Storing Persistent Data
531
17
Privacy and Security
565
18
Plugins and Applets
583
19
Creating Your Own Objects
603
20
JavaScript in E-Commerce
621
21
JavaScript and SVG
649
Appendixes
687
A
New Features in JavaScript 1.5
689
B
Color Codes
693
C
Functions Reference
699
D
Resources Online
717
E
A Short History of JavaScript
723
Index
729
Contents Introduction
WEEK 1 A Sound Foundation DAY 1 Getting the Basics Right
1
7 9
Introducing JavaScript ..........................................................................................10 What Is JavaScript? ..........................................................................................10 What Does JavaScript Do? ..............................................................................12 JavaScript in Context ............................................................................................12 A Requirement for More Than Just HTML ....................................................12 Server-Side or Client-Side? ..............................................................................13 Server-Side ......................................................................................................13 Client-Side ........................................................................................................14 Reality ..............................................................................................................15 Selecting the Right Technology ............................................................................15 JavaScript, Jscript, and ECMAScript ....................................................................18 JavaScript..........................................................................................................18 JScript ..............................................................................................................18 ECMAScript ....................................................................................................18 Let’s Get Started ....................................................................................................19 Choosing a Text Editor and Web Browser ......................................................19 Basic Things to Remember ..............................................................................22 Where Does JavaScript Go? ............................................................................23 Reusing Code....................................................................................................32 Documenting Your Code ..................................................................................33 Reserved Words ................................................................................................34 Data Types ............................................................................................................36 Numbers............................................................................................................36 Boolean ............................................................................................................38 Strings ..............................................................................................................38 Some Useful Tools ................................................................................................41 The alert() Function ......................................................................................41 The confirm() Function ..................................................................................42 The prompt() Function ....................................................................................43 Summary ..............................................................................................................44 Workshop ..............................................................................................................44 Q&A ................................................................................................................44 Quiz ..................................................................................................................44 Quiz Answers....................................................................................................45 Exercises ..........................................................................................................45
vi
Sams Teach Yourself JavaScript in 21 Days
DAY 2 Working with Data
47
Simple Data Storage ..............................................................................................48 Variables ..........................................................................................................48 Constants ..........................................................................................................53 Operators................................................................................................................54 Understanding Operators..................................................................................55 JavaScript Operators ..............................................................................................57 Arithmetic Operators ........................................................................................57 Comparison Operators......................................................................................60 Conditional Operator ........................................................................................65 Logical Operators ............................................................................................67 String Operators................................................................................................70 Assignment Operators ......................................................................................71 Bitwise Operators ............................................................................................73 Operator Precedence ........................................................................................75 Summary ................................................................................................................76 Workshop ..............................................................................................................76 Q&A ................................................................................................................77 Quiz ..................................................................................................................77 Quiz Answers....................................................................................................77 Exercises ..........................................................................................................77 DAY 3 Functions and Statements
79
User Defined Functions ........................................................................................80 What Is a Function?..........................................................................................80 Creating a Function ..........................................................................................80 Using Functions................................................................................................82 Predefined Global Functions ................................................................................91 URI Encoding and Decoding ..........................................................................92 Evaluating Strings as Code ..............................................................................94 Arithmetic Functions ........................................................................................95 What Is a Statement? ..........................................................................................102 Control Statements ..............................................................................................103 Conditional Statements ..................................................................................103 Loop Statements ..................................................................................................110 The while Statement ......................................................................................111 The do while Statement ................................................................................112 The for Statement ..........................................................................................113 The for in Statement ....................................................................................116 Summary ..............................................................................................................117 Workshop ............................................................................................................117 Q&A ..............................................................................................................117 Quiz ................................................................................................................117
Contents
vii
Quiz Answers..................................................................................................118 Exercise ..........................................................................................................118 DAY 4 JavaScript Is Object-Based
119
Understanding Objects ........................................................................................120 What Are Objects?..........................................................................................120 Client-Side JavaScript Objects ............................................................................122 The window Object..........................................................................................122 The Dot Notation............................................................................................122 Variable or Property? Function or Method? ..................................................124 The window Object’s Children ........................................................................129 The document Object ..........................................................................................130 Properties ........................................................................................................131 Methods ..........................................................................................................133 Child Objects ..................................................................................................137 The body Object..............................................................................................140 Environment Objects ..........................................................................................141 The location Object ......................................................................................142 Loading a New Page ......................................................................................143 Accessing the URL ........................................................................................144 The history Object ......................................................................................147 The navigator Object ....................................................................................149 The screen Object..........................................................................................150 Core JavaScript Objects ......................................................................................150 Exploring an Object ............................................................................................154 Summary ..............................................................................................................158 Workshop ............................................................................................................159 Q&A ..............................................................................................................159 Quiz ................................................................................................................159 Quiz Answers..................................................................................................159 Exercises ........................................................................................................159 DAY 5 An Introduction to Arrays
161
What Is an Array? ................................................................................................161 Creating an Array ................................................................................................164 Providing Individual Arguments ....................................................................164 Creating an Empty Array and Populating It ..................................................165 Array Literals..................................................................................................167 Accessing Arrays ................................................................................................168 Parallel Arrays ................................................................................................171 Array Properties ..................................................................................................174
viii
Sams Teach Yourself JavaScript in 21 Days
Array Methods ....................................................................................................179 The toString() Method ................................................................................180 The toLocaleString() Method ....................................................................180 The join() Method ........................................................................................181 Adding and Removing Elements from an Array............................................182 The reverse() Method ..................................................................................189 The toSource() Method ................................................................................193 The valueOf() Method ..................................................................................193 Associative Arrays ..............................................................................................193 Storing Objects in Array Elements ......................................................................197 Summary ..............................................................................................................199 Workshop ............................................................................................................199 Q&A ..............................................................................................................199 Quiz ................................................................................................................199 Quiz Answers..................................................................................................200 Exercise ..........................................................................................................200 DAY 6 HTML Forms and the String Object
201
Collecting Data from HTML Forms....................................................................202 Why Use JavaScript in Forms? ......................................................................205 Accessing Form Elements Using JavaScript..................................................205 Properties of the