Tải bản đầy đủ (.pdf) (778 trang)

sams teach yourself javascript in 21 days (2002)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.36 MB, 778 trang )

800 East 96th St.,Indianapolis,Indiana,46240 USA
Andrew Watt and Jonathan Watt
with Jinjer Simon and Jim O’Donnell
JavaScript

in
21
Days
Teach Yourself
00 2978 FM 4/10/02 10:45 AM Page i
Sams Teach Yourself JavaScript

in 21 Days
Copyright © 2002 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical, photo-
copying, recording, or otherwise, without written permission from the publish-
er. 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
First Printing: May 2002
04 03 02 6 5 4 3
Trademarks
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.
Warning and Disclaimer
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 aris-
ing from the information contained in this book.
ACQUISITIONS EDITOR
Betsy Brown
DEVELOPMENT EDITOR
Jonathan Steever
MANAGING EDITOR
Charlotte Clapp
PROJECT EDITOR
Tony Reitz
COPY EDITOR
Karen Whitehouse
INDEXER
Larry Sweazy
PROOFREADER
Cindy Long
TECHNICAL EDITOR
Jim O’Donnell
TEAM COORDINATOR
Amy Patton
INTERIOR DESIGN
Gary Adair
COVER DESIGN
Aren Howell

PRODUCTION
Ayanna Lacey
00 2978 FM 4/10/02 10:45 AM Page ii
Contents at a Glance
Introduction 1
Week 1 7
1 Getting the Basics Right 9
2Working with Data 47
3 Functions and Statements 79
4JavaScript 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 283
8The 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

00 2978 FM 4/10/02 10:45 AM Page iii
Appendixes 687
ANew Features in JavaScript 1.5 689
B Color Codes 693
C Functions Reference 699
D Resources Online 717
EA Short History of JavaScript 723
Index 729
00 2978 FM 4/10/02 10:45 AM Page iv
Contents
Introduction 1
WEEK 1 A Sound Foundation 7
DAY 1 Getting the Basics Right 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
00 2978 FM 4/10/02 10:45 AM Page v
DAY 2Working 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
D
AY 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
vi Sams Teach Yourself JavaScript in 21 Days
00 2978 FM 4/10/02 10:45 AM Page vi
Quiz Answers 118
Exercise 118
D
AY 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
D
AY 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

Contents vii
00 2978 FM 4/10/02 10:45 AM Page vii
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
D
AY 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

<form> Element 206
Methods of the
Form Object 211
Form Elements 215
The
elements Collection 216
Properties Common to Several Form Elements 219
The
text Element 221
The
textarea Element 223
Check Boxes 226
Radio Buttons 228
Select Boxes 230
Buttons 233
The
String Object 233
String Properties 235
String Methods 236
Checking User Input 242
Summary 245
Workshop 246
Q&A 246
Quiz 246
Quiz Answers 246
Exercises 247
viii Sams Teach Yourself JavaScript in 21 Days
00 2978 FM 4/10/02 10:45 AM Page viii
DAY 7 Numbers and Math 249
The

Number Object 250
Numbers in JavaScript 250
Properties of the
Number object 255
Methods of the
Number Object 258
The
valueOf() method 266
The
Math Object 267
Predefined Properties 267
Methods of the
Math Object 270
Creating Your Own
Math Functions 278
Finding the Factorial of a Number 278
Finding the nth Root 279
Log to Base N 280
Summary 281
Workshop 281
Q&A 281
Quiz 281
Quiz Answers 281
Exercises 282
WEEK 2 Let’s Take It Further 283
DAY 8 The Browser Issue 285
Different Browsers, Different JavaScript 286
JavaScript History 286
ECMA Standards 287
Cross-Browser Compatible Scripting 288

Browsers That Don’t Support JavaScript 289
Browsers with JavaScript Support Turned Off 290
Determining Browser Information 292
Determining Browser Support for Specific Objects 297
The W3C DOM 298
Accessing Nodes 300
Summary 302
Workshop 303
Q&A 303
Quiz 304
Quiz Answers 304
Exercises 305
D
AY 9 Date and Time Manipulation 307
JavaScript and Dates 308
The Date Object 309
Creating a Specific
Date Object 310
Contents ix
00 2978 FM 4/10/02 10:45 AM Page ix
x Sams Teach Yourself JavaScript in 21 Days
Date Formatting 312
Converting the Numeric Day and Month Values 314
Combining Date Values 316
Time Formatting 317
Converting to an AM/PM Time Frame 319
Working with Time Zones 322
Converting Date and Time Formats 323
Date Calculations 325
Converting Between Strings and Dates 327

Summary 327
Workshop 328
Q&A 328
Quiz 328
Quiz Answers 328
Exercises 329
D
AY 10 Events and Event Handling 331
Understanding Events 332
Creating an Event Handler 332
Handling an Event with a JavaScript Statement 333
Handling Events with JavaScript Function Calls 334
Capturing Events Directly Within JavaScript 335
Types of Events 337
How to Handle Events 339
Monitoring Form Changes 339
Working with Keyboard Events 342
Monitoring an Object’s Focus 344
Monitoring Window and Document Events 346
Capturing Mouse Actions 346
Summary 353
Workshop 354
Q&A 354
Quiz 354
Quiz Answers 355
Exercises 355
D
AY 11 Dynamic HTML 357
What is DHTML? 358
Using Cascading Style Sheets 358

Defining Styles 359
Creating CSS Rules 363
Creating Classes 369
Working with Layers 371
Changing Attributes of an HTML Element 374
00 2978 FM 4/10/02 10:45 AM Page x
Contents xi
Moving Things 377
Summary 380
Workshop 381
Q&A 381
Quiz 382
Quiz Answers 382
Exercises 382
D
AY 12 Windows and Frames 383
What Are Windows and Frames? 384
Window Properties and Methods 385
Determining Window Location 390
Working with Window History 393
Working with the Status Line 394
Using the
Screen Object 397
Working with Frames 399
Referencing Frame Locations 403
Breaking Out of Frames 406
Forcing Frames 407
Working with Popups 409
Summary 411
Workshop 412

Q&A 412
Quiz 412
Quiz Answers 413
Exercises 413
D
AY 13 Regular Expressions Make It Easier 415
Why Regular Expressions Are Useful 416
What Is a Regular Expression? 417
Matching a Literal Character Sequence 418
Some Simple Patterns 422
Single Literal Character Choices 422
Choices Using Sequences of Literal Characters 425
Patterns That Use Numbered Occurrences of Classes 426
Patterns Using Variable Numbers of Occurrences 430
Regular Expressions Overview 436
Defining Regular Expressions 436
Creating a Regular Expression Using Literals 436
Creating Regular Expressions Using a Constructor 437
Special Characters 440
Quantifiers 444
00 2978 FM 4/10/02 10:45 AM Page xi
xii Sams Teach Yourself JavaScript in 21 Days
The Methods of the RegExp Object 446
Using the
exec() Method 446
Using the
test() Method 447
The Properties of the
RegExp Object 448
Scoping a Regular Expression Using the

global Property 448
Making a Regular Expression Case Insensitive Using the
ignoreCase
Property 449
Summary 449
Workshop 449
Q&A 449
Quiz 450
Quiz Answers 450
Exercises 450
D
AY 14 Advanced Array Management 453
The
Array Object’s Methods 453
The
concat() Method 454
The
slice() Method 463
The
splice() Method 464
The
sort() Method 470
Multidimensional Arrays 484
Summary 491
Workshop 491
Q&A 491
Quiz 491
Quiz Answers 492
Exercises 492
WEEK 3 Advanced Topics 493

DAY 15 Debugging and Error Handling 495
Preventing and Classifying JavaScript Errors 496
Commenting Code 496
Keeping Track of JavaScript Variables 497
JavaScript Error Types 498
Finding Syntax Errors 498
Errors in Netscape Navigator 499
Errors in Internet Explorer 500
Debugging Load-Time Errors 501
Using the
document.writeln() Method 503
Using the Alternate Inputs 506
Debugging Run-Time Errors I: Discrete Events 509
Using the
alert() Method 511
Using HTML Form Elements 514
00 2978 FM 4/10/02 10:45 AM Page xii
Contents xiii
Debugging Run-Time Errors II: Continuous Events 518
Advanced Debugging Techniques 524
Creating a Run-Time JavaScript Interpreter 524
Opening a Debug Browser Window 527
Using JavaScript Debuggers 527
Summary 529
Workshop 529
Q&A 529
Quiz 530
Quiz Answers 530
Exercise 530
D

AY 16 Cookies: Storing Persistent Data 531
Maintaining State 532
Cookies: An Introduction 533
Advantages of Cookies 534
Limitations of Cookies 534
Disadvantages of Cookies 534
Cookie Myths 535
Using Cookies 535
Retrieving Cookie Values 536
Setting Cookie Values 537
A Cookie Example 542
Where Are Cookies Going? 557
Where to Find More Information About Cookies 557
Other State Maintenance Options 558
Using Query Strings to Send Information 558
How to Use Hidden Form Variables 561
Summary 562
Workshop 562
Q&A 562
Quiz 562
Quiz Answers 563
Exercise 563
D
AY 17 Privacy and Security 565
Privacy for Web Users 566
File Restrictions 566
Cookies 568
Browser Window Access 569
Computer Resource Limitations 569
Privacy for Webmasters 570

Same Origin Policy 572
00 2978 FM 4/10/02 10:45 AM Page xiii
xiv Sams Teach Yourself JavaScript in 21 Days
Privacy Impact on JavaScript 573
Password Protection 573
Creating Signed Scripts in Netscape 577
Summary 580
Workshop 580
Q&A 580
Quiz 581
Quiz Answers 581
Exercises 582
D
AY 18 Plugins and Applets 583
Plugins Versus Applets 584
Detecting Plugin Installation 585
Plugin Handling by Browsers 586
Checking for a Plugin 588
Determining Browser Support for a File Type 592
Working with Plugin Objects 594
Working with Applets 595
Java Basics 596
Calling a Java Applet 597
Interfacing with Java Applets 598
Summary 599
Workshop 599
Q&A 599
Quiz 600
Quiz Answers 600
Exercises 601

D
AY 19 Creating Your Own Objects 603
What Is a Custom Object? 604
Different Data Types Within One Object 605
Combine Multiple Data Elements into One Object 605
Create Methods and Properties Specific for the Custom Object 605
Use of the Constructor to Create Objects 606
Adding Properties to the Object 606
Creating an Instance of a Custom Object 608
Accessing Properties of a Custom Object 610
Changing the Value of an Object Property 611
Creating Object Methods 613
Linking Objects Together 616
Summary 618
00 2978 FM 4/10/02 10:45 AM Page xiv
Workshop 619
Q&A 619
Quiz 620
Quiz Answers 620
Exercises 620
D
AY 20 JavaScript in E-Commerce 621
Requirements for an E-Commerce Site 622
Shop Structure 622
The Online Catalog 625
Creating the Custom Database 625
Designing Your Catalog 627
The Bookstore Code 629
Creation of the
Books.js File 631

Shopping Carts 635
Solving Floating-Point Math Errors 636
Deleting Orders 638
Gathering User Information 639
Coding for the Shopping Cart Page 641
Issues to Consider 645
Summary 646
Workshop 646
Q&A 646
Quiz 647
Quiz Answers 647
Exercises 647
D
AY 21 JavaScript and SVG 649
Overview of Scalable Vector Graphics 650
Why SVG? 650
Why SVG and JavaScript? 651
Basic SVG Tools 651
Example SVG Code 654
The SVG Document Object Model 656
Batik DOM Viewer 656
SVG DOM Basics 658
Using JavaScript in SVG 659
The
<script> Element in SVG Documents 659
Specifying the Scripting Language 660
A Skeleton SVG Document with JavaScript 661
Adding a Title to an SVG Image 661
Creating a Simple Shape Using JavaScript 664
Adding Text Using JavaScript 666

Contents xv
00 2978 FM 4/10/02 10:45 AM Page xv
A Timed Animation Using JavaScript 668
Creating a Shape That Responds to Events 670
JavaScript Beyond Declarative Animation 676
Interaction Between the HTML and SVG DOM 682
Finding Out More About SVG 684
Summary 684
Workshop 684
Q&A 685
Quiz 685
Quiz Answers 685
Exercises 686
Appendixes 687
A New Features in JavaScript 1.5 689
Number Formatting Additions 689
toExponential Method 689
toFixed Method 690
toPrecision Method 690
Runtime Error Messages 690
Regular Expressions 691
Greedy Quantifiers 691
Non-Capturing Parentheses 691
Support for Positive and Negative Lookahead Assertions 691
The Multiple-Line Flag with RegExp 691
Conditional Function Declarations 691
Functions Can Be Declared Within an Expression 692
Multiple Catch Clauses 692
B Color Codes 693
Color Names Specified as Plain-Language 694

Hexadecimal Color Value 695
RGB Color Values 698
C Functions Reference 699
abs() 699
acos() 700
asin() 700
atan() 700
atan2() 701
atob() 701
Boolean() 701
btoa() 702
xvi Sams Teach Yourself JavaScript in 21 Days
00 2978 FM 4/10/02 10:45 AM Page xvi
captureEvents() 702
catch() 703
ceil() 703
cos() 703
Date() 704
decodeURI() 704
decodeURIComponent() 704
encodeURI() 704
encodeURIComponent() 705
Error() 705
escape() 705
eval() 706
exp() 706
floor() 706
Function() 707
GetObject() 707
handleEvent() 708

isFinite() 708
isNaN() 708
log() 709
max() 709
min() 709
Number() 710
Object() 710
parseFloat() 711
parseInt() 711
pow() 712
random() 712
releaseEvents() 712
rgb() 713
round() 713
routeEvents() 713
ScriptEngine() 714
sin() 714
sqrt() 714
tan() 714
toString() 715
unescape() 715
unwatch() 715
watch() 716
Contents xvii
00 2978 FM 4/10/02 10:45 AM Page xvii
D Resources Online 717
JavaScript 717
CNET Builder.COM 717
Dynamic Drive 718
JavaScript Kit 718

The JavaScript Source 718
JavaScript World 718
Microsoft JScript Reference 718
Netscape JavaScript Developer Central 718
Netscape Plugin Guide 718
W3C World Wide Web Consortium 719
W3Schools.com 719
WebReference.com 719
Java and Java Applets 719
FreewareJava.com 719
The Java Boutique 719
Java Technology Tutorials 719
Javalobby 719
JavaWorld 720
The Source for Java Technology 720
ZDNet Developer 720
HTML 720
HTML Writers Guild 720
NCSA Beginners Guide to HTML 720
Website Tips 720
CGI 721
CGI 101 721
CGI City 721
The CGI Resource Index 721
The Common Gateway Interface 721
EA Short History of JavaScript 723
Evolution of the Internet 723
Internet Programming Revolution 724
JavaScript Introduction 725
Browser Support of JavaScript Versions 727

Index 729
00 2978 FM 4/10/02 10:45 AM Page xviii
About the Authors
Andrew Watt is an independent consultant and author with expertise in XML and Web
technologies including SVG. He is author of Designing SVG Web Graphics (New Riders)
and XPath Essentials (Wiley). He is co-author of XML Schema Essentials (Wiley) and
contributing author to XHTML, XML & Java 2 Platinum Edition (Que), Professional
XSL, Professional XML 2nd Edition, and Professional XML Meta Data (Wrox).
Jonathan Watt has been working with JavaScript for two years and has been Webmaster
of the Strathclyde University Skills Society Web site, as well as a number of his own
Web sites such as dsvg.com. His primary expertise is in the application of JavaScript in
the client-side environment, but he also has used ASP and PHP to create database-driven
Web sites. He is currently in the third year of a master’s degree in engineering at
Strathclyde.
Jinjer Simon has been actively involved in the computer industry for the past 17 years.
Her involvement in the industry has included programming, providing software technical
support, end-user training, developing written and online user documentation, creating
software tutorials, developing Internet Web sites, and writing technical books. Jinjer and
her husband currently live in Coppell, Texas, with their two children where she currently
works as a consultant for MillenniSoft Inc. by providing Web site development and
online documentation development.
Jim O’Donnell was born on October 17, 1963, in Pittsburgh, Pennsylvania (you may
forward birthday greetings to
). After a number of unproductive years,
he went to Rensselaer Polytechnic Institute for 11 years earning three degrees. He now
lives in Washington, DC, and spends most of his time building spacecraft. He has been
writing and editing books for eight years. When he isn’t working, he collects comic
books and PEZ dispensers and plays ice hockey for the DC Nationals. Go, Nats!
00 2978 FM 4/10/02 10:45 AM Page xix
Dedication

I would like to dedicate my contribution to this book to the memory
of my late father George Alec Watt—a very special human being.
—Andrew Watt
To my immediate and extended family, all of whom I love very much.
—Jonathan Watt
To Ryan Miller. Italy in 2006!
—Jim O’Donnell
Acknowledgments
Andrew Watt and Jonathan Watt
We would first like to thank Shelley Johnston, who assisted in getting this project off the
ground. We also would like to sincerely thank Betsy Brown whose ingenuity, practical
suggestions, and almost limitless patience steered this project to a successful conclusion.
We would also like to thank Jim O’Donnell, whose knowledge of JavaScript ideally fit-
ted him to be technical editor for the book and who helped pick up those small errors
that are so easy to overlook as an author.
Jon Steever did his usual thorough and perceptive editing job on the submitted manu-
script. Thanks, Jon.
Finally, thanks to our fellow authors Jinjer Simon and Jim O’Donnell, without whose
efforts the book would not have been completed.
Jinjer Simon
I would like to thank Betsy Brown for giving me the opportunity to work on this book. I
would also like to thank Karen Whitehouse, Tony Reitz, and Jon Steever—the editors at
Sams who pulled everything together on the book. I would also like to thank the
Technical Editor on this book, Jim O’Donnell. He did a great job of identifying changes
that needed to be made to enhance the content of the book. Finally I would like to thank
my husband and two children for their patience while I worked to meet the deadlines on
this book.
00 2978 FM 4/10/02 10:45 AM Page xx
Jim O’Donnell
I would like to acknowledge Betsy Brown, Jon Steever, and all of the other fine editors

I’ve worked with over the years.
00 2978 FM 4/10/02 10:45 AM Page xxi
Tell Us What You Think!
As the reader of this book, you are our most important critic and commentator. 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 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.
Please note that I cannot help you with technical problems related to the topic of this
book, and that due to the high volume of mail I receive, I might not be able to reply to
every message.
When you write, please be sure to include this book’s title and author as well as your
name and phone or e-mail address. I will carefully review your comments and share
them with the author and editors who worked on the book.
E-mail:

Mail: Mark Taber
Associate Publisher
Sams Publishing
800 East 96th Street
Indianapolis,IN 46240 USA
00 2978 FM 4/10/02 10:45 AM Page xxii
Introduction
JavaScript has become the most important client-side scripting language on the Web. It
provides the Web developer with the functionality to create rollover effects, move objects
around the browser screen, check the validity of data entered into HTML and XHTML
forms, and much, much more. Sams Teach Yourself JavaScript in 21 Days will give you
an understanding of JavaScript that will help you carry out the day-to-day tasks that you
will use JavaScript to accomplish. As you master more and more of the important aspects

of JavaScript, you will be able to add functionality to your existing Web pages, improv-
ing their impact on the experience of your Web site visitors.
JavaScript has come a long way since it was first introduced in version 2 of the Netscape
browser. While JavaScript, currently at version 1.5, is still produced by Netscape, there is
an internationally recognized, standardized scripting language, ECMAScript, which is
made available to developers on a more open basis. Both Netscape’s JavaScript and
Microsoft’s JScript are based on ECMAScript, although both extend its functionality.
In the late 1990s, incompatibilities among JavaScript versions were a major problem for
Web developers. Many of those problems are disappearing as users upgrade to more
modern versions of browsers, which have been introduced since the ECMAScript stan-
dardization process started. With the advent of Netscape version 6, the Netscape browser
is making fuller use of standards such as the Document Object Model and Cascading
Style Sheets. Unfortunately the first release, Netscape 6.0, had a number of problems that
are being progressively ironed out as the Netscape browser transitions through versions
6.1 and later. As users discontinue use of browsers such as Netscape 3 and Internet
Explorer 3, the need to support outdated implementations of client-side scripting
becomes less. This makes writing cross-browser JavaScript code a much less frustrating
and demanding task than it was only a few years ago. Both Microsoft and Netscape gen-
uinely seem to attach importance to standards as they relate to JavaScript. Hopefully
many of the remaining problems will be ironed out in the near future.
The future for JavaScript is an exciting one.
How This Book Is Organized
Sams Teach Yourself JavaScript in 21 Days is organized into 21 chapters, and they are
written in such a way that you can work through one chapter a day and complete the
book in three weeks, if you have the time available to do so. The book has been written
so that you can work through chapters in the order in which they are presented. If you
decide to dip into a particular chapter that interests you, then you may need to refer back
01 2978 Intro 4/10/02 10:45 AM Page 1
to earlier chapters to fully understand the code and how you can further develop it for
your own use.

Week 1
Day 1, “Getting the Basics Right,” introduces you to JavaScript and helps you to under-
stand what kind of language JavaScript is. The tools you need to create and view
JavaScript are discussed, and how to correctly create a JavaScript script within an
HTML/XHTML file is covered.
Day 2, “Working with Data,” introduces you to how JavaScript handles data. Day 2 also
shows you how to store data in variables, and then how to change the value of that data,
as well as how to create constants that can be used unchanged throughout a script are
discussed.
In Day 3, “Functions and Statements,” you are shown how to create code, stored in func-
tions, which you can re-use as often as needed in your scripts. You also are shown how to
create code that executes depending on conditions you define, and how to create code
that loops through a process as often as you specify.
Day 4, “JavaScript Is Object-Based,” introduces you to the important topic of JavaScript
objects by discussing what an object is, and how you can use objects to write efficient
scripts.
Day 5, “An Introduction to Arrays,” teaches you what an array is, and how to create and
use them.
Collecting information from users is a hugely important part of the interactive Web.
Day 6, “HTML Forms and the
String Object,” introduces you to using JavaScript with
HTML/XHTML forms to collect data from visitors to the Web sites you have created.
Strings of characters are commonly collected when using forms, and Day 6 will show
you how strings are handled in JavaScript.
Day 7, “Numbers and Math,” introduces the JavaScript
Number and Math objects and
teaches you how numbers are handled in JavaScript as well as how to use the built-in
mathematical functions which the JavaScript interpreter makes available to a scripter.
Week 2
Day 8, “The Browser Issue,” tells you about the differences in the variety of browser ver-

sions and how they handle JavaScript. It also shows you how to produce JavaScript code
that will run on commonly used modern browsers.
Day 9, “Date and Time Manipulation,” introduces you to the JavaScript
Date object and
shows you how to create code that will handle dates and times.
2 Sams Teach Yourself JavaScript in 21 Days
01 2978 Intro 4/10/02 10:45 AM Page 2

×