Tải bản đầy đủ (.pdf) (1,035 trang)

wrox press javascript programmer's reference (2009)

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 (6.52 MB, 1,035 trang )

ffirs.indd iiffirs.indd ii 6/25/09 8:19:20 PM6/25/09 8:19:20 PM
JavaScript® Programmer’s Reference
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Chapter 1: Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Chapter 2: JavaScript in the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
Chapter 3: JavaScript Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
Chapter 4: Expressions, Operators, and Statements . . . . . . . . . . . . . . . . . . . . . .61
Chapter 5: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
Chapter 6: The Global and Object Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Chapter 7: The String and RegExp Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
Chapter 8: The Boolean, Number, and Math Objects . . . . . . . . . . . . . . . . . . . . .199
Chapter 9: The Array and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
Chapter 10: Object Oriented Development . . . . . . . . . . . . . . . . . . . . . . . . . . . .251
Chapter 11: Windows and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271
Chapter 12: Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
Chapter 13: The Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . .341
Chapter 14: Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .375
Chapter 15: Cascading Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403
Chapter 16: Dynamic HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431
Chapter 17: JavaScript Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .475
Chapter 18: Client-Side Data and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . .487
Chapter 19: Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .511
Chapter 20: Working with XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .533
Chapter 21: Working with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .555
Chapter 22: Unique Browser Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .573
Chapter 23: Scripting Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
Chapter 24: Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .613
Chapter 25: Performance Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .631
Appendix A: Core JavaScript Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .649
Appendix B: JavaScript Global Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .723


Appendix C: JavaScript Global Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . .895
Appendix D: JavaScript Global Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .899
Appendix E: Reserved and Special Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . .909
Appendix F: Document Object Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . .911
Appendix G: Resources on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .973
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .975
ffirs.indd iffirs.indd i 6/25/09 8:19:20 PM6/25/09 8:19:20 PM
ffirs.indd iiffirs.indd ii 6/25/09 8:19:20 PM6/25/09 8:19:20 PM
JavaScript®
Programmer’s Reference
Alexei White
Wiley Publishing, Inc.
ffirs.indd iiiffirs.indd iii 6/25/09 8:19:20 PM6/25/09 8:19:20 PM
JavaScript® Programmer’s Reference
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-34472-9
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
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
http://www
.wiley.com/go/permissions.
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 specifically
disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No
warranty may be created or extended by sales or promotional 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.
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: 2009930969
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, 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
registered 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.
ffirs.indd ivffirs.indd iv 6/25/09 8:19:21 PM6/25/09 8:19:21 PM
About the Author
Alexei White is a programmer, designer, and speaker. He is the inventor of RobotReplay, a web session-
tracking technology acquired by Foresee Results, and an author of the book Enterprise Ajax, as well as the
DVD training series Enterprise Ajax LiveLessons. He has contributed to major Web projects for Microsoft

and Nintendo. His blog can be found at
. When he’s not working in Vancouver
he can be found floating on a lake somewhere in the interior of British Columbia with a fishing pole in
his hand. You may also find him on Twitter (@alexsaves).
ffirs.indd vffirs.indd v 6/25/09 8:19:21 PM6/25/09 8:19:21 PM
Acquisitions Editor
Scott Meyers
Development Editor
Ed Connor
Technical Editor
Alexei Gorkov
Production Editor
Daniel Scribner
Copy Editor
Christopher Jones
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
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
Lynsey Stanford
Proofreader

Publication Services, Inc.
Indexer
Ron Strauss
Credits
ffirs.indd viffirs.indd vi 6/25/09 8:19:21 PM6/25/09 8:19:21 PM
Acknowledgments
A book like this is never written in a vacuum, and there are many people who played an important role
in getting it done, and contributing to the overall quality. I want to thank some people in the
development community for their overall contributions to JavaScript over the years, and helping to
advance the understanding and skills of a large community of developers. This group certainly includes
Douglas Crockford, John Resig, Dustin Diaz, Valerio Proietti, Alex Russel, Eric Lawrence (thank you for
Fiddler), Joe Hewitt (of Firebug fame – and who should have schools named after him or something),
and of course many more.
Thanks also go to Nitobi, Foresee Results, Adobe, the Mozilla Foundation, Opera Software, Microsoft,
and Apple either for making my life a lot easier, or for their continuing contributions to the web
development community (keep making great tools!)
On a personal note, I want to thank my fianc é Lara for never being anything but totally supportive
whether I am working late, writing all weekend, or generally not doing any of my chores as a result.
I also want to thank my parents for giving me everything I ever needed to succeed.
ffirs.indd viiffirs.indd vii 6/25/09 8:19:21 PM6/25/09 8:19:21 PM
ffirs.indd viiiffirs.indd viii 6/25/09 8:19:21 PM6/25/09 8:19:21 PM
Contents
Introduction xxv
Chapter 1: Introduction to JavaScript 1
JavaScript History 1
Looking Ahead to ES5 and Harmony 2
Stages of a JavaScript Developer 3
Real-World JavaScript 4
In the Browser 4
Server-Side JavaScript 5

ActionScript and Flash 7
Adobe Integrated Runtime (AIR) 7
In Other Adobe Products 7
Desktop Widgets 7
Complementary Technologies 7
Hypertext Markup Language (HTML) 8
Cascading Style Sheets (CSS) 8
The Browser Object Model (BOM) 9
The Document Object Model (DOM) 10
When to Use JavaScript 11
Major JavaScript Engines 12
ECMAScript Support by Engine 14
General Equivalence 15
Performance in JavaScript Engines 15
Basic Development Tools 17
Choosing a Text Editor 17
The Web Server 17
The Test Browser 18
Your First JavaScript Application 19
Breaking it Down 20
Summary 21
ftoc.indd ixftoc.indd ix 6/26/09 2:53:10 PM6/26/09 2:53:10 PM
Contents
x
Chapter 2: JavaScript in the Browser 23
The Document Object Model 23
The SCRIPT Element 25
Script Masking 27
The NOSCRIPT Element 27
Execution and Load Order 27

Inline Scripts 28
External Scripts 30
Deferred Scripts 30
Event-driven Scripts 31
Dynamically Loaded Scripts 33
JavaScript in URL’s 35
Cross-Browser Compatibility 36
Summary 36
Chapter 3: JavaScript Basics 39
General Characteristics 39
A Dynamic Language 39
A Prototype-Based Language 40
Character Encoding 40
Case Sensitivity 41
Whitespace and Semicolons 42
Literals 42
Statements 46
Blocks 47
Closures 47
Comments 48
Reserved Words 48
Variables 48
Declaring Variables 49
Implicit Declaration 49
Identifiers 49
Weak Typing 50
Memory and Garbage Collection 51
Data Types 51
Primitive vs. Reference Types 51
Manipulating by Value vs. Reference 52

ftoc.indd xftoc.indd x 6/26/09 2:53:11 PM6/26/09 2:53:11 PM
Contents
xi
Null and Undefined 53
Determining Type 54
Type Conversion 55
Primitives vs. Primitive Objects 58
Summary 59
Chapter 4: Expressions, Operators, and Statements 61
JavaScript Expressions 61
JavaScript Operators 62
Types of Operators 62
Comparison Operators 63
Assignment Operators 66
Logical Operators 68
Bitwise Operators 70
Combinational (Connubial) Operators 73
Other Operator Types 75
Operator Precedence and Associativity 79
JavaScript Statements 82
Conditionals 84
Loops and Iterators 87
Function Statements 95
Exception Handling Statements 96
Miscellaneous Statements 99
Summary 100
Chapter 5: Functions 103
The Function Object 103
Declaring Functions 104
Passing Arguments by Value versus Reference 106

Return Values 107
Variable Scope 107
Overloading 109
Optional Arguments 112
The arguments Object 114
Argument Hashes 115
Nested Functions 118
Closures 118
Uses of Closures 119
Closures within Loops 121
ftoc.indd xiftoc.indd xi 6/26/09 2:53:12 PM6/26/09 2:53:12 PM
Contents
xii
Circular References 123
Accidental Closures 125
Execution Context and Scope 126
Using apply() 126
Using call() 127
Summary 128
Chapter 6: The Global and Object Objects 129
Features of the Global Object 129
The Global Object in the Browser 130
Getting the Global Object 131
Global Properties 131
Global Functions 132
URI Encoding 133
Evaluated Code 134
Numeric Helpers 136
Global Objects 136
The Object Object 137

Object Prototypes 139
Determining if a Property Exists 140
valueOf() and toString() 141
Useful Utility Functions for Objects 142
Summary 147
Chapter 7: The String and RegExp Objects 149
String Object Overview 149
String Basics 151
String Literals 151
String Encoding 151
Line Breaks in Strings 154
String Immutability 155
String Length 155
Primitives or Objects 156
Extending the String Object 158
String Concatenation 159
Strings and Numbers 161
Converting to Strings 161
Comparing Strings 163
Working with Strings 164
The Formatting Methods 165
Working with Case 167
ftoc.indd xiiftoc.indd xii 6/26/09 2:53:12 PM6/26/09 2:53:12 PM
Contents
xiii
Searching and Replacing 168
Slicing and Dicing 192
Strings and Arrays 196
Encoding Strings for URL’s 197
Summary 197

Chapter 8: The Boolean, Number, and Math Objects 199
The Boolean Object 199
Boolean Object Overview 200
Working with Booleans 200
Converting to Boolean 202
Adding XOR to the Boolean Object 202
The Number Object 203
Number Object Overview 203
Integer and Floating Point Values 204
Number Literals 204
Numbers and Strings 205
Converting to a Number 207
NaN and Infinity 208
Minimum and Maximum Values 209
The Math Object 210
Math Object Overview 210
Mathematical Constants 211
Math Utility Methods 212
Rounding Numbers 213
Random Numbers 213
Simplifying Repeated Math Calls 214
Summary 215
Chapter 9: The Array and Date Objects 217
The Array Object 217
Array Object Overview 217
Creating Arrays 218
Indexed Arrays 219
Multi-dimensional Arrays 220
Detecting Arrays 220
Array Size 223

Iterating over an Array 223
Adding Elements to an Array 224
Combining Arrays 226
Removing Elements from Arrays 227
ftoc.indd xiiiftoc.indd xiii 6/26/09 2:53:12 PM6/26/09 2:53:12 PM
Contents
xiv
Associative Arrays (Hashes) 229
Arrays as Reference Types 231
Arrays and Strings 233
The Date Object 234
Date Object Overview 235
Creating Dates 236
An Overview of World Time 237
Parsing Date Strings 238
Reading and Writing Dates 240
Measuring Time Differences 244
Timers and Intervals 245
Summary 248
Chapter 10: Object Oriented Development 251
Object Oriented JavaScript 251
Creating Objects 253
Deleting Properties and Objects 253
Objects as Reference Types 254
Objects as Arrays 255
Comparing Objects 256
Object Cloning 257
Static Members 258
Constructors 258
The constructor Property 259

Prototypes 259
The this Keyword 261
Private Members 261
Privileged Members 262
Getters and Setters 263
Inheritence in JavaScript 264
Prototype-Based Subclassing 265
The Problem with Prototypes 267
Alternate Subclassing Approaches 268
Summary 270
Chapter 11: Windows and Frames 271
Overview of the Browser Object Model 271
The window Object 272
Working with Frames 273
Creating Frames 273
ftoc.indd xivftoc.indd xiv 6/26/09 2:53:13 PM6/26/09 2:53:13 PM
Contents
xv
The Frame Object Model 274
Referencing Frames 274
Manipulating Windows 275
The Status Bar 276
Opening and Closing Windows 276
Setting Window Location 284
Encoding Strings for URL’s 285
Window History 288
Moving and Resizing 288
Scrolling 289
Dialogues and Alerts 289
Obtaining Browser and OS Information 291

Basics of Browser Detection 291
The navigator Object 292
Detecting Language 293
The screen Object 294
A Browser and OS Detection Class 295
Window Events 301
Summary 302
Chapter 12: Events 305
The Basic Event Model 306
Basic Event Registration 308
The this Keyword 309
Preventing Default Behavior 310
Unobtrusive JavaScript 311
Unobtrusive Event Registration 312
Inspecting Event Listeners 314
The event Object 314
A Cross Browser Event Utility 318
Event Propagation 319
Capture Mode for IE Mouse Events 322
Default Handlers 323
Preventing Event Propagation 323
Replicating Events 324
Common Event Bindings 326
Detecting Keystrokes 326
Mouse Position 327
The scroll Event 329
The resize Event 330
The load and unload Events 330
ftoc.indd xvftoc.indd xv 6/26/09 2:53:13 PM6/26/09 2:53:13 PM
Contents

xvi
The domready Event 331
The mouseenter and mouseleave Events 333
Event Compatibility 336
Custom Events 338
Summary 340
Chapter 13: The Document Object Model 341
The History of the DOM 341
The Legacy Object Model 342
Basic Model Plus Images 342
Navigator 4 Extensions 343
Internet Explorer 4 Extensions 343
Internet Explorer 5 Extensions 344
The W3C DOM 344
Document Types 345
What Happens in Quirks Mode 346
Checking the DOCTYPE 347
The Document Tree 347
Node Types 348
Node Properties 350
Node Methods 351
The implementation Object 352
Traversing the DOM 353
Element Attributes 356
Building a DOM Inspector 358
Finding Specific Elements 359
Element Collections 359
getElementsByName 360
getElementsByTagName 361
getElementById 361

XPath 362
Creating and Deleting Nodes 362
Adding New Nodes 363
Repaints and Reflows 365
Document Fragments 365
Performance Comparison of Mutators 366
Removing Nodes 367
Swapping Nodes 367
DOM Ranges 368
Ranges from the DOM 368
Range Boundaries 369
Changing the Content 370
ftoc.indd xviftoc.indd xvi 6/26/09 2:53:14 PM6/26/09 2:53:14 PM
Contents
xvii
Collapsing the Range 371
User Selection Ranges 371
Summary 373
Chapter 14: Forms 375
The Form Object 375
Form Elements 379
Basic Form Manipulation 381
Submitting and Resetting Forms 381
Using the onsubmit Event 382
Preventing Submissions on Enter 383
Enabling and Disabling Fields 383
Preventing Double-Submit 384
Setting Focus to Fields 385
Working with Inputs 385
Buttons 386

Checkboxes 387
Radio Buttons 387
Select and Multiselect 389
Textboxes, Textareas, and Passwords 391
Hidden Fields 395
file Input Fields 396
Rich Text Fields (WYSIWYG) 396
Summary 401
Chapter 15: Cascading Style Sheets 403
Overview 403
Embedding CSS in a Document 403
Versions 405
How Styles Cascade 407
CSS and the DOM 407
styleSheet and Style Objects 416
Imported Style Sheets 418
Iterating Over All Stylesheets 419
Adding and Removing Style Sheets 422
Iterating over All Rules 423
Searching for a Rule 424
Reading and Writing Style Properties 424
Adding and Removing Rules 426
Computed Styles 428
ftoc.indd xviiftoc.indd xvii 6/26/09 2:53:14 PM6/26/09 2:53:14 PM
Contents
xviii
IE’s filter Object 429
Summary 430
Chapter 16: Dynamic HTML 431
The Role of CSS 432

Window and Document Geometry 433
Getting Scrollbar Thickness 435
Element Dimensions 437
Image Swapping and Rollovers 438
Rollovers and Mouseenter and Mouseleave 441
Positioning 443
Absolute and Relative Positions 443
Scripting Z-Index 446
Get the Absolute Position of an Element 447
Animation 451
Pseudo-Threading with Timers 452
Nonlinear Animation and Tweening 453
Color and Opacity 457
Color 457
Yellow-Fade 457
Opacity 458
Internet Explorer and 32Bit Images 459
Modal Dialogues 460
Form Tooltips 467
Summary 473
Chapter 17: JavaScript Security 475
Security Models 475
Same Origin Policy 476
Exceptions with document.domain 477
Cross-Site Scripting 477
Cross-Site Request Forgery 478
Piggy-Back Transmissions 479
Signed Scripts 479
Mozilla Features Requiring Expanded Privileges 479
Signed Scripts in Internet Explorer 480

Security Policies and Zones 480
Mozilla Security Policies 480
Internet Explorer Security Zones 482
Miscellaneous Issues 483
ftoc.indd xviiiftoc.indd xviii 6/26/09 2:53:15 PM6/26/09 2:53:15 PM
Contents
xix
New Windows 483
Denial of Service 483
Data Security 484
ActiveX 484
Flash 485
JSON and eval() 485
Summary 486
Chapter 18: Client-Side Data and Persistence 487
Methods of Persisting Data 488
Cookies 489
Creating and Reading Cookies 489
Deleting Cookies 492
UserData in Internet Explorer 492
Initializing UserData 492
Reading and Writing UserData 493
W3C DOM Storage 495
Reading and Writing to DOM Storage 496
Using DOM Storage Events 496
HTML5 Client-Side Database 498
Creating a SQLite Database 498
Reading and Writing SQLite Data 500
The Safari SQLite Database Browser 502
Flash Local Shared Object 502

Storage Using window.name 505
Summary 509
Chapter 19: Ajax 511
XMLHttpRequest 512
Opening a Connection 514
Request and Response Headers 518
Security 518
Using GET Requests to Change Data 519
Cross-Domain Ajax 519
Method Comparison 520
document.domain 521
Server Proxy 521
iFrames 522
Image Injection 522
<SCRIPT> Injection 523
ftoc.indd xixftoc.indd xix 6/26/09 2:53:15 PM6/26/09 2:53:15 PM
Contents
xx
The Flash Approach 523
Cross-Domain XMLHttpRequest 524
History and Bookmarking 525
Summary 531
Chapter 20: Working with XML 533
Loading XML 533
Deserializing Text 533
Loading External XML Documents 536
Handling Errors 538
Serializing XML to Text 539
Working with the XML DOM API 540
Elements and Nodes 540

Traversing the DOM 542
Performing XPath Queries 544
Transforming Data with XSLT 548
Applying XSL Templates 550
E4X 552
Summary 553
Chapter 21: Working with JSON 555
From JavaScript Literals to JSON 556
Labels and Encoding 557
JSON as Evaluated Code 558
Security Issues 559
JSON versus XML 559
Serializing Objects to JSON 560
Custom toJSON() Methods 562
Using the Replacer 564
Loading JSON Data 564
Custom Revivers 565
Handling Errors 567
JSON and Ajax 567
JSONP 569
Summary 570
Chapter 22: Unique Browser Features 573
Accelerators 574
Canvas 575
Animation 576
ftoc.indd xxftoc.indd xx 6/26/09 2:53:15 PM6/26/09 2:53:15 PM
Contents
xxi
Conditional Compilation 577
CSS Transforms 578

Geolocation 580
Detecting Support 581
Getting the Coordinates 581
Google Gears 582
Detecting and Installing Gears 582
Using Database 583
Using Geolocation 584
Using WorkerPool 584
Search Providers 586
Vector Markup Language 587
Web Workers 588
Terminating a Worker 589
Summary 590
Chapter 23: Scripting Plugins 591
Java Applets 591
Flash Movies 594
Setting up your Flash Movie 595
Embedding with SWFObject 598
Accessing Methods and Properties 599
Silverlight Movies 601
Setting up a Silverlight Application 601
Embedding a Silverlight Movie with JavaScript 603
Introduction to RegisterScriptableObject 603
JavaScript and Silverlight Communication 604
QuickTime 606
Detecting QuickTime 606
Embedding QuickTime Movies 607
Controlling Movies from JavaScript 608
Movie Events 610
Summary 612

Chapter 24: Debugging 613
Types of Errors 613
Error Object Overview 614
Throwing Errors 615
Error Handlers 616
Getting the Stack Trace 617
ftoc.indd xxiftoc.indd xxi 6/26/09 2:53:15 PM6/26/09 2:53:15 PM
Contents
xxii
Debugging Tools 619
Firebug for Firefox 620
Firebug Lite 623
Internet Explorer Developer Toolbar 624
Dragonfly for Opera 626
Fiddler 626
Charles Proxy Debugger 627
Safari Web Inspector 627
Testing 628
Summary 628
Chapter 25: Performance Tuning 631
Reducing Page Weight 632
Post-loading JavaScript 634
Cacheing 634
Spriting 634
JavaScript Minification and Concatenation 634
gZip Compression 635
Content Delivery Networks 636
Code Profiling 636
Profiling with Firebug 637
The IE8 JScript Profiler 637

Getting the ‘Big Picture’ with YSlow 638
Code Optimization 638
Delete Unused Objects 638
Avoid Evaluated Code 639
Local versus Global Variable Lookup 640
Object and Function Pointers 640
Avoid the with Statement 641
Avoid try . . . catch in Repeated Operations 642
Repeated for in Loops 642
Tune Your Loops 643
DHTML Optimization 644
Repaints and Reflows 644
Changing Hidden Elements 645
Grouping DOM Changes 645
Grouping Style Changes 646
Measuring Elements 646
ftoc.indd xxiiftoc.indd xxii 6/26/09 2:53:16 PM6/26/09 2:53:16 PM
Contents
xxiii
Using Document Fragments 646
Threading for Long-Running Tasks 647
Summary 648
Appendix A: Core Javascript Language 649
Appendix B: JavaScript Global Objects 723
Appendix C: JavaScript Global Properties 895
Appendix D: JavaScript Global Functions 899
Appendix E: Reserved and Special Words 909
Appendix F: Document Object Reference 911
Appendix G: Resources on the Web 973
Index 975

ftoc.indd xxiiiftoc.indd xxiii 6/26/09 2:53:16 PM6/26/09 2:53:16 PM

×