Statistics
25
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-12-21
Support
Share

AuthorAlexei White

As a reference, this book works really well. One can pick up the book and quickly find anything from information about basic syntax, the DOM, to advanced features like Ajax and JSON. The examples stand alone from previous chapters, so there is no need to read the book linearly from cover to cover. But if one *were* to read the book from cover to cover, it makes a great introduction to JavaScript for designers or others who are already familiar with HTML and CSS. White's writing style is pitch-perfect for such a use--the information is easy to follow while not being "dumbed-down." The bottom line: great reference, but don't be afraid to use this to learn JavaScript as it is used today.

Tags
javascript
ISBN: 0470344725
Publisher: Wiley Pub., Inc
Publish Year: 2009
Language: 英文
Pages: 1035
File Format: PDF
File Size: 6.5 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
ffirs.indd ii 6/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 i 6/25/09 8:19:20 PM
ffirs.indd ii 6/25/09 8:19:20 PM
JavaScript® Programmer’s Reference Alexei White Wiley Publishing, Inc. ffirs.indd iii 6/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 iv 6/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 http://ambiguiti.es. 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 v 6/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 vi 6/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 vii 6/25/09 8:19:21 PM
ffirs.indd viii 6/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 ix 6/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 x 6/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 xi 6/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 xii 6/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 xiii 6/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 xiv 6/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 xv 6/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 xvi 6/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 xvii 6/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 xviii 6/26/09 2:53:15 PM
The above is a preview of the first 20 pages. Register to read the complete e-book.