Tải bản đầy đủ

Tài liệu Wrox.Professional.JavaScript.For.Web.Developers.2nd.Edition.Jan.2009.eBook-ELOHiM doc

Updates, source code, and Wrox technical support at www.wrox.com
for Web Developers
2nd Edition
Nicholas C. Zakas
Wrox Programmer to Programmer

for Web Developers, 2nd Edition
Enhance Your Knowledge
Advance Your Career
Professional JavaScript for Web Developers, 2nd Edition
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
Professional Ajax, 2nd Edition
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.
Concise Guide to Dojo
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.
Beginning JavaScript and CSS Development with jQuery
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.
Beginning JavaScript, 3rd Edition
This book aims to teach you all you need to know to start experimenting with JavaScript: what it is, how it works, and what you
can do with it. Starting from the basic syntax, you'll move on to learn how to create powerful Web applications.
Beginning CSS, 2nd Edition
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.

Professional JavaScript
for Web Developers
Introduction xxix
Chapter 1: What Is JavaScript? 1
Chapter 2: JavaScript in HTML 13
Chapter 3: Language Basics 23
Chapter 4: Variables, Scope, and Memory 79
Chapter 5: Reference Types 97
Chapter 6: Object-Oriented Programming 151
Chapter 7: Anonymous Functions 183
Chapter 8: The Browser Object Model 201
Chapter 9: Client Detection 229
Chapter 10: The Document Object Model 261
Chapter 11: DOM Levels 2 and 3 317
Chapter 12: Events 365
Chapter 13: Scripting Forms 433
Chapter 14: Error Handling and Debugging 465
Chapter 15: XML in JavaScript 515
Chapter 16: ECMAScript for XML 547
Chapter 17: Ajax and JSON 567
Chapter 18: Advanced Techniques 589
Chapter 19: Client-Side Storage 617
Chapter 20: Best Practices 635
Chapter 21: Upcoming APIs 669
Chapter 22: The Evolution of JavaScript 703
Appendix A: JavaScript Libraries 759
Appendix B: JavaScript Tools 765
Index 773

for Web Developers
2nd Edition

for Web Developers
2nd Edition
Nicholas C. Zakas
Wiley Publishing, Inc.

Professional JavaScript
for Web Developers, 2nd Edition
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-22780-0
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data
Zakas, Nicholas C.
Professional JavaScript for web developers/Nicholas C. Zakas. — 2nd ed.
p. cm.
Includes index.
ISBN 978-0-470-22780-0 (paper/website)
1. Web site development. 2. JavaScript (Computer program language) I. Title.
TK5105.8885.J38Z34 2008
005.2'762 — dc22
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 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.
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.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic books.

Dedicated to my family:
mom, dad, Greg, Yiayia, and Papou.
We may be few in numbers, but we are mighty!
Your constant love and support have made the past couple of years possible.

About the Author
Nicholas C. Zakas has a B.S. in Computer Science from Merrimack College and an M.B.A. from Endicott
College. He is the coauthor of Professional Ajax, Second Edition (Wiley, 2007) as well as dozens of online
articles. Nicholas works for Yahoo! as a principal front-end engineer on Yahoo!’s front page and a
contributor to the Yahoo! User Interface (YUI) Library. He has worked in web development for more than
eight years, during which time he has helped develop web solutions in use at some of the largest
companies in the world.
Nicholas can be reached through his web site

Acquisitions Director
Jim Minatel
Senior Development Editor
Kevin Kent
Technical Editor
Alexei Gorkov
Development Editor
Gus Miklos
Production Editor
Rebecca Coleman
Copy Editors
Foxxe Editorial Services, Candace English
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Project Coordinator, Cover
Lynsey Stanford
Kathryn Duggan
Jack Lewis

It takes many people to create a single book, and I’d like to thank some people here for their
contributions to this work.
First and foremost, thanks to everyone at Wiley for their support: Jim Minatel for once again putting his
faith in me; Kevin Kent for dealing with the hectic outline rearrangements I tend to make throughout
writing; and Alexei Gorkov, the best technical editor in the world, who makes sure that everything I say
is 100-percent accurate.
A big thanks to everyone who provided feedback on draft chapters: David Serduke, Julian Turner, Pete
Frueh, Chris Klaiber, Stoyan Stefanov, Ross Harmes, and David Golightly. Your early feedback was really
helpful in making this book what it is today.
Last, thanks to Eric Miraglia for his contribution of a foreword. Eric is the reason I ended up at Yahoo!,
and it has been a pleasure to work with him for the past two years.

Foreword xxvii
Introduction xxix
Chapter 1: What Is JavaScript? 1
A Short History 1
JavaScript Implementations 3
ECMAScript 3
The Document Object Model (DOM) 7
The Browser Object Model (BOM) 9
JavaScript Versions 10
Summary 11
Chapter 2: JavaScript in HTML 13
The <script> Element 13
Tag Placement 15
Deferred Scripts 16
Changes in XHTML 17
Deprecated Syntax 18
Inline Code versus External Files 19
Document Modes 19
The <noscript> Element 21
Summary 22
Chapter 3: Language Basics 23
Syntax 23
Case-sensitivity 23
Identifiers 24
Comments 24
Statements 25
Keywords and Reserved Words 25
Variables 26
Data Types 28
The typeof Operator 28
The Undefined Type 28
The Null Type 30


The Boolean Type 30
The Number Type 31
The String Type 37
The Object Type 40
Operators 41
Unary Operators 41
Bitwise Operators 45
Boolean Operators 51
Multiplicative Operators 54
Additive Operators 56
Relational Operators 58
Equality Operators 60
Conditional Operator 62
Assignment Operators 62
Comma Operator 63
Statements 63
The if Statement 63
The do-while Statement 64
The while Statement 65
The for Statement 65
The for-in Statement 66
Labeled Statements 67
The break and continue Statements 67
The with Statement 69
The switch Statement 70
Functions 72
Understanding Arguments 74
No Overloading 76
Summary 76
Chapter 4: Variables, Scope, and Memory 79
Primitive and Reference Values 79
Dynamic Properties 80
Copying Values 81
Argument Passing 82
Determining Type 84
Execution Context and Scope 84
Scope Chain Augmentation 87
No Block-Level Scopes 88

Garbage Collection 90
Mark-and-Sweep 91
Reference Counting 91
Performance 93
Managing Memory 93
Summary 94
Chapter 5: Reference Types 97
The Object Type 97
The Array Type 100
Conversion Methods 102
Stack Methods 104
Queue Methods 105
Reordering Methods 106
Manipulation Methods 108
The Date Type 109
Inherited Methods 111
Date-Formatting Methods 112
Date/Time Component Methods 113
The RegExp Type 115
RegExp Instance Properties 117
RegExp Instance Methods 118
RegExp Constructor Properties 120
Pattern Limitations 122
The Function Type 122
No Overloading (Revisited) 123
Function Declarations vs. Function Expressions 124
Functions as Values 125
Function Internals 126
Function Properties and Methods 128
Primitive Wrapper Types 130
The Boolean Type 131
The Number Type 132
The String Type 134
Built-in Objects 142
The Global Object 142
The Math Object 146
Summary 149

Chapter 6: Object-Oriented Programming 151
Creating Objects 151
The Factory Pattern 152
The Constructor Pattern 152
The Prototype Pattern 155
Combination Constructor/Prototype Pattern 166
Dynamic Prototype Pattern 166
Parasitic Constructor Pattern 167
Durable Constructor Pattern 169
Inheritance 170
Prototype Chaining 170
Constructor Stealing 175
Combination Inheritance 176
Prototypal Inheritance 177
Parasitic Inheritance 178
Parasitic Combination Inheritance 179
Summary 182
Chapter 7: Anonymous Functions 183
Recursion 184
Closures 185
Closures and Variables 188
The this Object 189
Memory Leaks 190
Mimicking Block Scope 191
Private Variables 193
Static Private Variables 195
The Module Pattern 196
The Module-Augmentation Pattern 198
Summary 199
Chapter 8: The Browser Object Model 201
The window Object 201
The Global Scope 201
Window Relationships and Frames 202
Window Position 205
Window Size 206
Navigating and Opening Windows 207
Intervals and Timeouts 211
System Dialogs 213

The location Object 216
Query String Arguments 216
Manipulating the Location 217
The navigator Object 219
Detecting Plug-ins 221
Registering Handlers 223
The screen Object 224
The history Object 226
Summary 227
Chapter 9: Client Detection 229
Capability Detection 229
Quirks Detection 231
User-Agent Detection 232
History 233
Working with User-Agent Detection 240
The Complete Script 255
Usage 258
Summary 258
Chapter 10: The Document Object Model 261
Hierarchy of Nodes 261
The Node Type 263
The Document Type 269
The Element Type 279
The Text Type 289
The Comment Type 292
The CDATASection Type 293
The DocumentType Type 294
The DocumentFragment Type 294
The Attr Type 296
DOM Extensions 297
Rendering Modes 297
Scrolling 298
The children Property 298
The contains() Method 299
Content Manipulation 300
Working with the DOM 307
Dynamic Scripts 307
Dynamic Styles 309

Manipulating Tables 311
Using NodeLists 314
Summary 314
Chapter 11: DOM Levels 2 and 3 317
DOM Changes 317
XML Namespaces 318
Other Changes 322
Styles 326
Accessing Element Styles 326
Working with Style Sheets 331
Element Dimensions 336
Traversals 342
NodeIterator 344
TreeWalker 347
Ranges 349
Ranges in the DOM 349
Ranges in Internet Explorer 358
Summary 362
Chapter 12: Events 365
Event Flow 365
Event Bubbling 366
Event Capturing 367
DOM Event Flow 367
Event Handlers or Listeners 368
HTML Event Handlers 368
DOM Level 0 Event Handlers 369
DOM Level 2 Event Handlers 370
Internet Explorer Event Handlers 372
Cross-Browser Event Handlers 373
The Event Object 375
The DOM Event Object 375
The Internet Explorer Event Object 379
Cross-Browser Event Object 381
Event Types 383
UI Events 383
Mouse Events 383
Keyboard Events 392
HTML Events 397

Mutation Events 402
Proprietary Events 407
Mobile Safari Events 417
Memory and Performance 422
Event Delegation 422
Removing Event Handlers 424
Simulating Events 425
DOM Event Simulation 426
Internet Explorer Event Simulation 430
Summary 432
Chapter 13: Scripting Forms 433
Form Basics 433
Submitting Forms 434
Resetting Forms 435
Form Fields 436
Scripting Text Boxes 441
Text Selection 442
Input Filtering 445
Automatic Tab Forward 449
Scripting Select Boxes 450
Options Selection 452
Adding Options 454
Removing Options 455
Moving and Reordering Options 455
Form Serialization 456
Rich Text Editing 458
Interacting with Rich Text 459
Rich Text Selections 462
Rich Text in Forms 463
Summary 464
Chapter 14: Error Handling and Debugging 465
Browser Error Reporting 465
Internet Explorer 465
Firefox 467
Safari 469
Opera 470
Chrome 472

Error Handling 473
The try-catch Statement 474
Throwing Errors 477
The error Event 480
Error-Handling Strategies 481
Identify Where Errors Might Occur 481
Distinguishing between Fatal and Nonfatal Errors 486
Log Errors to the Server 487
Debugging Techniques 488
Logging Messages to a Console 488
Logging Messages to the Page 491
Throwing Errors 491
Common Internet Explorer Errors 492
Operation Aborted 493
Invalid Character 494
Member Not Found 494
Unknown Runtime Error 495
Syntax Error 495
The System Cannot Locate the Resource Specified 496
Debugging Tools 496
Internet Explorer Debugger 496
Firebug 502
Drosera 507
Opera JavaScript Debugger 510
Other Options 513
Summary 513
Chapter 15: XML in JavaScript 515
XML DOM Support in Browsers 515
DOM Level 2 Core 515
The DOMParser Type 516
The XMLSerializer Type 517
DOM Level 3 Load and Save 518
Serializing XML 523
XML in Internet Explorer 523
Cross-Browser XML Processing 528
XPath Support in Browsers 530
DOM Level 3 XPath 530
XPath in Internet Explorer 535
Cross-Browser XPath 536

XSLT Support in Browsers 539
XSLT in Internet Explorer 539
The XSLTProcessor Type 543
Cross-Browser XSLT 545
Summary 546
Chapter 16: ECMAScript for XML 547
E4X Types 547
The XML Type 547
The XMLList Type 549
The Namespace Type 550
The QName Type 551
General Usage 552
Accessing Attributes 553
Other Node Types 555
Querying 556
XML Construction and Manipulation 558
Parsing and Serialization Options 560
Namespaces 561
Other Changes 563
Enabling Full E4X 564
Summary 564
Chapter 17: Ajax and JSON 567
The XHR Object 568
XHR Usage 569
HTTP Headers 571
GET Requests 573
POST Requests 574
Browser Differences 575
Security 577
Cross-Domain Requests 578
The XDomainRequest Object 578
Cross-Domain XHR 580
JSON 581
Using JSON with Ajax 583
Security 586
Summary 587

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay