Updates, source code, and Wrox technical support at www.wrox.com Professional JavaScript ® for Web Developers 2nd Edition Nicholas C. Zakas Wrox Programmer to Programmer TM Professional JavaScript ® for Web Developers, 2nd Edition Enhance Your Knowledge Advance Your Career Professional JavaScript for Web Developers, 2nd Edition 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 Ajax, 2nd Edition 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. Concise Guide to Dojo 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. Beginning JavaScript and CSS Development with jQuery 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. Beginning JavaScript, 3rd Edition 978-0-470-05151-1 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 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. 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 Professional JavaScript ® for Web Developers 2nd Edition Professional JavaScript ® 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 www.wiley.com 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 2008045552 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. 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. [...]... Detection User-Agent Detection History Working with User-Agent Detection The Complete Script Usage Summary Chapter 10: The Document Object Model Hierarchy of Nodes The The The The The The The The The Node Type Document Type Element Type Text Type Comment Type CDATASection Type DocumentType Type DocumentFragment Type Attr Type DOM Extensions 224 226 227 229 229 231 232 233 240 255 258 258 261 261 263 269 279... window, document, location, navigator, and screen Chapter 9, Client Detection — Explains various approaches to detecting the client machine and its capabilities Different techniques include capability detection and user-agent string detection This chapter discusses the pros and cons as well as the situational appropriateness of each approach Chapter 10, The Document Object Model — Introduces the Document... 1: What Is JavaScript? A Short History JavaScript Implementations ECMAScript The Document Object Model (DOM) The Browser Object Model (BOM) JavaScript Versions Summary Chapter 2: JavaScript in HTML The Element Tag Placement Deferred Scripts Changes in XHTML Deprecated Syntax Inline Code versus External Files Document Modes The Element Summary Chapter 3: Language Basics Syntax Case-sensitivity... Ajax was slowly being discovered, and a new paradigm of desktop-style user interaction was emerging within the browser The DOM APIs that allowed JavaScript to manipulate the structure and content of web documents had solidified CSS, for all the contortions, omissions, and the willful insanity of its implementations by browser vendors, had progressed far enough that beauty and responsiveness could be combined... was suddenly interesting all over again JavaScript, as the only programming language of the Web, became more interesting, too Interesting, but hard to do well JavaScript and its companion APIs in the Document Object Model (DOM) and Browser Object Model (BOM) were inconsistently implemented, making cross-browser implementations vastly more difficult than they needed to be The profession of front-end... JavaScript originated and evolved into what it is today A detailed discussion of the components that make up a JavaScript implementation follows, with specific focus on standards such as ECMAScript and the Document Object Model (DOM) The differences in JavaScript implementations used in different popular web browsers are also discussed Building on that base, the book moves on to cover basic concepts of JavaScript,... 1, What Is JavaScript? — Explains the origins of JavaScript: where it came from, how it evolved, and what it is today Concepts introduced include the relationship between JavaScript and ECMAScript, the Document Object Model (DOM), and the Browser Object Model (BOM) A discussion of the relevant standards from the European Computer Manufacturer ’s Association (ECMA) and the World Wide Web Consortium (W3C)... 21: Upcoming APIs The Selectors API The querySelector() Method The querySelectorAll() Method Support and the Future HTML 5 Character Set Properties Class-Related Additions Custom Data Attributes Cross-Document Messaging Media Elements The Element Offline Support Changes to History Database Storage Drag-and-Drop The WebSocket Type The Future of HTML 5 Summary Chapter 22: The Evolution of JavaScript . Professional JavaScript ® for Web Developers, 2nd Edition Enhance Your Knowledge Advance Your Career Professional JavaScript for Web Developers, 2nd Edition 978-0-470-22780-0 This. Developers 2nd Edition Professional JavaScript ® for Web Developers 2nd Edition Nicholas C. Zakas Wiley Publishing, Inc. Professional JavaScript ® for Web Developers,