Wrox Programmer to Programmer™
Join the discussion @ p2p.wrox.com
4th Edition
Beginning
JavaScript
®
Paul Wilton, Jeremy McPeak
Beginning
Wilton, McPeak
$39.99 USA
$47.99 CAN
Web Development/JavaScript
JavaScript
®
Step-by-step guidance to creating
powerful web apps with JavaScript
4th Edition
wrox.com
Programmer
Forums
Join our Programmer to
Programmer forums to ask
and answer programming
questions about this book,
join discussions on the
hottest topics in the industry,
and connect with fellow
programmers from around
the world.
Code Downloads
Take advantage of free code
samples from this book, as
well as code samples from
hundreds of other books, all
ready to use.
Read More
Find articles, ebooks, sample
chapters and tables of contents
for hundreds of books, and
more reference resources on
programming topics that matter
to you.
JavaScript allows you to enhance your web pages and web applications
by providing dynamic, personalized, and interactive content. Serving
as a great introduction to JavaScript, this book offers all you need
to start using JavaScript on your web pages right away. It’s fully
updated and covers utilizing JavaScript with the latest versions of the
Internet Explorer
®
, Firefox
®
, and Safari
®
browsers.
• Walks you through the basics of JavaScript: what it is, how it works,
and what you can do with it
• Covers the various tools needed to create JavaScript web applications
• Escorts you through selecting a single character from a string,
converting character codes to a string, and copying string parts
• Shows you how to join arrays, copy parts of an array, sort arrays,
and reverse an array’s order
• Explains how using a JavaScript framework (such as jQuery,
Prototype, and MooTools) makes JavaScript programming faster
and more efficient
• Offers an in-depth look at Ajax
• Reviews common mistakes, debugging, and error handling
Paul Wilton owns his own company, providing online booking systems to
vacation property owners, which is largely developed using JavaScript.
Jeremy McPeak is a self-taught programmer who began his career by tinkering
with web sites in 1998. He is the coauthor of Professional Ajax, 2nd Edition
and several online articles covering topics such as XSLT, ASP
.NET Web Forms,
and C#. He is currently employed at an energy-based company
building in-house conventional and web applications.
Wrox Beginning guides are crafted to make learning programming languages
and technologies easier than you think, providing a structured, tutorial format
that will guide you through all the techniques involved.
525937_cover_2.indd 1525937_cover_2.indd 1 12/18/09 2:29 PM12/18/09 2:29 PM
Get more out of
WROX.com
Programmer to Programmer
™
Interact
Take an active role online by participating in
our P2P forums
Wrox Online Library
Hundreds of our books are available online
through Books24x7.com
Wrox Blox
Download short informational pieces and
code to keep you up to date and out of
trouble!
Chapters on Demand
Purchase individual book chapters in pdf
format
Join the Community
Sign up for our free monthly newsletter at
newsletter.wrox.com
Browse
Ready for more Wrox? We have books and
e-books available on .NET, SQL Server, Java,
XML, Visual Basic, C#/ C++, and much more!
Contact Us.
We always like to get feedback from our readers. Have a book idea?
Need community support? Let us know by e-mailing
Related Wrox Books
Beginning CSS, 2nd Edition
ISBN: 978-0-470-17708-2
Updated and revised, this book offers a hands-on look at designing standards-based, large-scale, professional-level CSS web sites.
Understand designers’ processes from start to finish and gain insight into how designers overcome a site’s unique set of challenges and
obstacles. Become comfortable with solving common problems, learn the best practices for using XHMTL with CSS, orchestrate a new
look for a blog, tackle browser-compatibility issues, and develop functional navigational structures.
Beginning JavaScript and CSS Development with jQuery
ISBN: 978-0-470-22779-4
Beginning JavaScript and CSS Development with jQuery presents the world of dynamic web applications to web developers from the
standpoint of modern standards. The author shows new JavaScript developers how working with the standard jQuery library will help
them to do more with less code and fewer errors.
Concise Guide to Dojo
ISBN: 978-0-470-45202-8
Dojo has rapidly become one of the hottest JavaScript-based web development frameworks. It provides you with the power and flexibility
to create attractive and useful dynamic web applications quickly and easily. In this fast-paced, code-intensive guide, you’ll discover how
to quickly start taking advantage of Dojo. The pages are packed with useful information and insightful examples that will help you.
JavaScript Programmer’s Reference
ISBN: 978-0-470-34472-9
Both a tutorial and a reference guide for web developers, employ this complete JavaScript reference to help you understand JavaScript data
types, variables, operators, expressions, and statements, work with JavaScript frameworks and data, and improve performance with Ajax.
Professional Ajax, 2nd Edition
ISBN: 978-0-470-10949-6
Professional Ajax, 2nd Edition is written for web application developers looking to enhance the usability of their web sites and web
applications and intermediate JavaScript developers looking to further understand the language. This second edition is updated to cover
Prototype, jQuery, FireBug, Microsoft Fiddler, ASP.NET AJAX Extensions, and much more.
Professional JavaScript for Web Developers, 2nd Edition
ISBN: 978-0-470-22780-0
This updated bestseller offers an in-depth look at the JavaScript language and covers such topics as debugging tools in Microsoft Visual
Studio, FireBug, and Drosera; client-side data storage with cookies, the DOM, and Flash; client-side graphics with JavaScript including
SVG, VML, and Canvas; and design patterns including creational, structural, and behavorial patterns.
Professional JavaScript Frameworks: Prototype, YUI, ExtJS, Dojo and MooTools
ISBN: 978-0-470-38459-6
This book offers a selection of some of the most active and most used JavaScript frameworks available, replete with practical examples
and explanations of what each framework does best. You’ll look at common web development tasks and discover how each framework
approaches that set of tasks, as well as learn how to use the features of each framework and avoid potential pitfalls.
525937_cover_2.indd 2525937_cover_2.indd 2 12/18/09 2:29 PM12/18/09 2:29 PM
Beginning JavaScript
®
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Chapter 1: Introduction to JavaScript and the Web . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Data Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 3: Decisions, Loops, and Functions . . . . . . . . . . . . . . . . . . . . . . . . . 51
Chapter 4: Common Mistakes, Debugging, and Error Handling . . . . . . . . . . . 87
Chapter 5: JavaScript — An Object-Based Language . . . . . . . . . . . . . . . . . . 133
Chapter 6: Programming the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Chapter 7: HTML Forms: Interacting with the User . . . . . . . . . . . . . . . . . . . 219
Chapter 8: Windows and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Chapter 9: String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Chapter 10: Date, Time, and Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Chapter 11: Storing Information: Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Chapter 12: Dynamic HTML and the W3C Document Object Model . . . . . . . 391
Chapter 13: Using ActiveX and Plug-Ins with JavaScript . . . . . . . . . . . . . . . 469
Chapter 14: Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Chapter 15: JavaScript Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Appendix A: Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Appendix B: JavaScript Core Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Appendix C: W3C DOM Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Appendix D: Latin-1 Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
25937ffirs.indd i25937ffirs.indd i 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
25937ffirs.indd ii25937ffirs.indd ii 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
Beginning JavaScript®
Fourth Edition
25937ffirs.indd iii25937ffirs.indd iii 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
25937ffirs.indd iv25937ffirs.indd iv 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
Beginning JavaScript®
Fourth Edition
Paul Wilton
Jeremy McPeak
25937ffirs.indd v25937ffirs.indd v 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
Beginning JavaScript
®
Fourth Edition
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-52593-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2
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 permission 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 Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at
/>Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including
without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or pro-
motional materials. The advice and strategies contained 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 Web site is referred to in this
work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses
the information the organization or Web site may provide or recommendations it may make. Further, readers should be
aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written
and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Control Number: 2009933758
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks
or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may
not be used without written permission. JavaScript is a registered trademark of Sun Microsystems Incorporated. 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.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
25937ffirs.indd vi25937ffirs.indd vi 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
In memory of my mum, June Wilton, who in 2006 lost her brave battle against cancer.
She was always very proud of me and my books and showed my books
to anyone and everyone she happened to meet however briefl y and whether they
wanted to see them or not! She’s very much missed.
— Paul Wilton
To my family: Starla, Hayden, and Evan (whom we haven’t yet met in person).
To my parents: Jerry and Judy.
Thank you all for your love and support.
— Jeremy McPeak
25937ffirs.indd vii25937ffirs.indd vii 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
25937ffirs.indd viii25937ffirs.indd viii 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
About the Authors
Paul Wilton started as a Visual Basic applications programmer at the Ministry of Defense in the UK and
then found himself pulled into the Net. Having joined an Internet development company, he spent three
years helping create Internet solutions. He’s now running his own successful and rapidly growing com-
pany developing online holiday property reservation systems.
Jeremy McPeak is a self-taught programmer who began his career by tinkering with web sites in 1998.
He is the co-author of Professional Ajax, 2nd Edition (Wiley 2007) and several online articles covering top-
ics such as XSLT, ASP.NET WebForms, and C#. He is currently employed in an energy-based company
building in-house conventional and web applications.
Jeremy can be reached through his web site
www.wdonline.com.
25937ffirs.indd ix25937ffirs.indd ix 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
25937ffirs.indd x25937ffirs.indd x 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
Credits
Acquisitions Editor
Scott Meyers
Project Editor
Maureen Spears
Technical Editor
David M. Karr
Production Editor
Rebecca Anderson
Copy Editor
C.M. Jones
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefi eld
Marketing Manager
David Mayhew
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lindsay Stanford
Compositor
Craig Johnson, Happenstance Type-O-Rama
Proofreader
Kathryn Duggan
Indexer
J & J Indexing
Cover Image
© Photographer’s Choice/Punchstock
25937ffirs.indd xi25937ffirs.indd xi 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
25937ffirs.indd xii25937ffirs.indd xii 9/22/09 11:23:54 AM9/22/09 11:23:54 AM
Acknowledgments
First, a big thank you to my partner Beci, who, now that the book’s fi nished, will get to see me for more
than 10 minutes a week.
I’d also like to say a very big thank you to Maureen Spears, who has worked very effi ciently on getting
this book into print.
Thanks also to Jim Minatel for making this book happen.
Many thanks to everyone who’s supported and encouraged me over my many years of writing books.
Your help will always be remembered.
Finally, pats and treats to my German Shepherd Dog, Katie, who does an excellent job of warding off
disturbances from door-to-door salespeople.
— Paul Wilton
First and foremost, a huge thank you to my wife for putting up with my late nights.
Just as huge thanks go to the people at Wiley Publishing: Jim Minatel and Scott Meyers for making this
happen; Maureen Spears who was absolutely wonderful to work with in getting this book into its fi nal,
printed form; and David M. Karr for keeping me honest.
Lastly, thank you Nicholas C. Zakas, author of Professional JavaScript, 2nd Edition (Wiley 2009) and
co-author of Professional Ajax, 2nd Edition (Wiley 2007), for getting me into this business.
— Jeremy McPeak
25937ffirs.indd xiii25937ffirs.indd xiii 9/22/09 11:23:55 AM9/22/09 11:23:55 AM
25937ffirs.indd xiv25937ffirs.indd xiv 9/22/09 11:23:55 AM9/22/09 11:23:55 AM
Contents
Introduction xxv
Chapter 1: Introduction to JavaScript and the Web 1
Introduction to JavaScript 1
What Is JavaScript? 1
JavaScript and the Web 2
Why Choose JavaScript? 4
What Can JavaScript Do for Me? 4
Tools Needed to Create JavaScript Web Applications 5
Where Do My Scripts Go? 7
Linking to an External JavaScript File 7
Advantages of Using an External File 8
Your First Simple JavaScript Programs 8
A Brief Look at Browsers and Compatibility Problems 15
Summary 16
Chapter 2: Data Types and Variables 17
Types of Data in JavaScript 17
Numerical Data 18
Text Data 18
Boolean Data 20
Variables — Storing Data in Memory 20
Creating Variables and Giving Them Values 21
Assigning Variables with the Value of Other Variables 24
Using Data — Calculations and Basic String Manipulation 26
Numerical Calculations 26
Operator Precedence 30
Basic String Operations 34
Mixing Numbers and Strings 35
Data Type Conversion 37
Dealing with Strings That Won’t Convert 40
Arrays 40
A Multi-Dimensional Array 45
Summary 49
Exercise Questions 50
25937ftoc.indd xv25937ftoc.indd xv 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xvi
Contents
Chapter 3: Decisions, Loops, and Functions 51
Decision Making — The if and switch Statements 51
Comparison Operators 52
The if Statement 54
Logical Operators 58
Multiple Conditions Inside an if Statement 60
else and else if 64
Comparing Strings 66
The switch Statement 67
Looping — The for and while Statements 71
The for Loop 71
The for in Loop 74
The while Loop 75
The do while loop 77
The break and continue Statements 78
Functions 79
Creating Your Own Functions 79
Variable Scope and Lifetime 83
Summary 84
Exercise Questions 86
Chapter 4: Common Mistakes, Debugging, and Error Handling 87
D’oh! I Can’t Believe I Just Did That: Some Common Mistakes 87
Undefi ned Variables 88
Case Sensitivity 89
Incorrect Number of Closing Braces 90
Incorrect Number of Closing Parentheses 90
Using Equals (=) Rather than Is Equal To (==) 91
Using a Method as a Property and Vice Versa 91
Missing Plus Signs During Concatenation 92
Error Handling 93
Preventing Errors 93
The try … catch Statements 94
Debugging 103
Debugging in Firefox with Firebug 104
Debugging in Internet Explorer 116
Debugging in Safari 123
Using Dragonfl y: Opera’s Development Tools 127
Summary 131
Exercise Questions 131
25937ftoc.indd xvi25937ftoc.indd xvi 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xvii
Contents
Chapter 5: JavaScript — An Object-Based Language 133
Object-Based Programming 133
What Are Objects? 134
Objects in JavaScript 134
Using JavaScript Objects 135
Primitives and Objects 138
JavaScript’s Native Object Types 139
String Objects 139
Array Objects 150
New Array Methods 156
The Math Object 160
Number Object 167
The toFixed() Method 167
Date Objects 168
Creating New Types of Objects (Reference Types) 177
Summary 187
Exercise Questions 188
Chapter 6: Programming the Browser 189
Introduction to the Browser’s Objects 190
The window Object 191
The history Object 193
The location Object 194
The navigator Object 194
The screen Object 195
The document Object 195
Using the document Object 195
The images Collection 198
The links Collection 199
Responding to the User’s Actions with Events 199
What Are Events? 200
Connecting Code to Events 200
Determining the User’s Browser 206
Summary 215
Exercise Questions 217
Chapter 7: HTML Forms: Interacting with the User 219
HTML Forms 219
Other Form Object Properties and Methods 222
HTML Elements in Forms 223
25937ftoc.indd xvii25937ftoc.indd xvii 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xviii
Contents
Common Properties and Methods 224
Button Elements 226
Text Elements 229
The textarea Element 236
Check Boxes and Radio Buttons 239
Selection Boxes 247
Summary 260
Exercise Questions 262
Chapter 8: Windows and Frames 263
Frames and the window Object 264
Coding Between Frames 268
Code Access Between Frames 273
Opening New Windows 284
Opening a New Browser Window 284
Scripting Between Windows 292
Moving and Resizing Windows 297
Security 298
Summary 298
Exercise Questions 299
Chapter 9: String Manipulation 301
Additional String Methods 302
The split() Method 302
The replace() Method 305
The search() Method 305
The match() Method 306
Regular Expressions 306
Simple Regular Expressions 307
Regular Expressions: Special Characters 315
Covering All Eventualities 323
Grouping Regular Expressions 323
Reusing Groups of Characters 325
The String Object — split(), replace(), search(), and match() Methods 327
The split() Method 327
The replace() Method 329
The search() Method 332
The match() Method 333
25937ftoc.indd xviii25937ftoc.indd xviii 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xix
Contents
Using the RegExp Object’s Constructor 335
Telephone Number Validation 339
Validating a Postal Code 341
Validating an E-mail Address 342
Validating a Domain Name 342
Summary 345
Exercise Questions 345
Chapter 10: Date, Time, and Timers 347
World Time 348
Setting and Getting a Date Object’s UTC Date and Time 351
Timers in a Web Page 358
One-Shot Timer 359
Setting a Timer that Fires at Regular Intervals 363
Summary 364
Exercise Questions 365
Chapter 11: Storing Information: Cookies 367
Baking Your First Cookie 367
A Fresh-Baked Cookie 368
The Cookie String 374
Creating a Cookie 377
Getting a Cookie’s Value 381
Cookie Limitations 386
A User May Disable Cookies 386
Number and Information Limitation 387
Cookie Security and IE6+ 388
Summary 389
Exercise Questions 389
Chapter 12: Dynamic HTML and the W3C Document Object Model 391
The Web Standards 392
HTML 394
XML 394
XHTML 395
ECMAScript 396
25937ftoc.indd xix25937ftoc.indd xix 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xx
Contents
The Document Object Model 397
The DOM Standard 397
Differences Between the DOM and the BOM 399
Representing the HTML Document as a Tree Structure 399
The Core DOM Objects 402
DOM Objects and Their Properties and Methods 404
Manipulating the DOM 422
Accessing Elements 422
Changing Appearances 422
Positioning and Moving Content 429
Example: Animated Advertisement 432
DOM Event Handling 437
Event Handling in Internet Explorer 443
Writing Cross-Browser DHTML 447
JavaScript and XML 453
Manipulating XML with JavaScript 454
Retrieving an XML File in IE 454
Retrieving an XML File in Firefox and Opera 457
Retrieving an XML File (Cross-Browser) 458
Example: Displaying a Daily Message 459
Summary 467
Exercise Questions 468
Chapter 13: Using ActiveX and Plug-Ins with JavaScript 469
Checking for and Embedding Plug-ins (Non-IE Browsers) 470
Adding a Plug-in to the Page 470
Checking for and Installing Plug-ins 472
Checking for and Embedding ActiveX Controls on Internet Explorer 474
Adding an ActiveX Control to the Page 475
Installing an ActiveX Control 478
Using Plug-ins and ActiveX Controls 480
Determining Plug-in/ActiveX Control Availability 481
Finishing Up 482
Testing the Disabling of the Form 484
Potential Problems 485
Summary 488
Exercise Question 489
25937ftoc.indd xx25937ftoc.indd xx 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xxi
Contents
Chapter 14: Ajax 491
What Is Ajax? 491
What Can It Do? 492
Browser Support 493
Using the XMLHttpRequest Object 494
Cross-Browser Issues 494
Using the XMLHttpRequest Object 497
Asynchronous Requests 499
Creating a Simple Ajax Module 500
Planning the HttpRequest Module 500
The HttpRequest Constructor 501
Creating the Methods 502
The Full Code 503
Validating Form Fields with Ajax 505
Requesting Information 506
The Received Data 506
Before You Begin 506
Things to Watch Out For 515
Security Issues 515
Usability Concerns 516
Summary 525
Exercise Questions 525
Chapter 15: JavaScript Frameworks 527
Picking a Framework to Work With 527
Getting Started 528
Installing the Frameworks 529
Testing the Frameworks 530
Digging Deeper Into jQuery 537
Selecting Elements 537
Changing Style 539
Creating, Appending, and Removing Elements 541
The jQuery Event Model and Handling Events 543
Rewriting the DHTML Toolbar with jQuery 545
Using jQuery for Ajax 550
Diving into Prototype 558
Retrieving Elements 558
Manipulating Style 560
Creating, Inserting, and Removing Elements 561
Using Events 562
Rewriting the DHTML Toolbar with Prototype 563
Using Ajax Support 567
25937ftoc.indd xxi25937ftoc.indd xxi 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xxii
Contents
Delving into MooTools 575
Finding Elements 575
Altering Style 576
Creating, Inserting, and Removing Elements 577
Using and Handling Events 578
Rewriting the DHTML Toolbar with MooTools 579
Ajax Support in MooTools 584
Summary 590
Exercise Questions 590
Appendix A: Answers to Exercises 591
Chapter 2 591
Chapter 3 593
Chapter 4 597
Chapter 5 600
Chapter 6 604
Chapter 7 610
Chapter 8 616
Chapter 9 624
Chapter 10 628
Chapter 11 631
Chapter 12 634
Chapter 13 641
Chapter 14 643
Chapter 15 653
Appendix B: JavaScript Core Reference 665
Browser Reference 665
Reserved Words 666
Other Identifi ers to Avoid 667
JavaScript Operators 668
Assignment Operators 668
Comparison Operators 669
Arithmetic Operators 670
Bitwise Operators 671
Bitwise Shift Operators 672
Logical Operators 672
Miscellaneous Operators 673
Operator Precedence 674
25937ftoc.indd xxii25937ftoc.indd xxii 9/22/09 11:02:27 AM9/22/09 11:02:27 AM
xxiii
Contents
JavaScript Statements 675
Block 675
Conditional 676
Declarations 676
Loop 676
Execution Control Statements 677
Exception Handling Statements 677
Other Statements 678
Top-Level Properties and Functions 678
JavaScript and Jscript Core Objects 679
ActiveXObject 679
Array 680
Boolean 682
Date 682
Function 685
Math 687
Number 688
Object 689
RegExp 690
String 693
Appendix C: W3C DOM Reference 697
DOM Core Objects 697
Low-Level DOM Objects 697
High-Level DOM Objects 700
HTML DOM Objects 705
Miscellaneous Objects: The HTML Collection 705
HTML Document Objects: The HTML Document 706
HTML Element Objects 707
HTMLButtonElement 709
DOM Event Model and Objects 719
EventTarget 719
Event 720
MouseEvent 720
Miscellaneous Events 722
Appendix D: Latin-1 Character Set 725
Index 733
25937ftoc.indd xxiii25937ftoc.indd xxiii 9/22/09 11:02:28 AM9/22/09 11:02:28 AM