Copyright © 2012 by The McGraw-Hill Companies. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. ISBN: 978-0-07174121-7 MHID: 0-07-1741216 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-174120-0, MHID: 0-07-174120-8. McGraw-Hill, the McGraw-Hill Publishing logo, The Complete Reference™, and related trade dress are trademarks or registered trademarks of The McGraw- Hill Companies and/or its affiliates in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. The McGraw-Hill Companies is not associated with any product or vendor mentioned in this book. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at [email protected]. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw- Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. About the Authors Thomas A. Powell ([email protected]) is the founder and CEO of PINT, Inc., a nationally known Web development and consulting firm. He is also the founder of a number of long-running software firms, including ZingChart (zingchart.com), a JavaScript charting and visualization library vendor; and Port80 Software (port80software.com), a maker of Web server security and performance software. He has written numerous books, including HTML & CSS: The Complete Reference™, Ajax: The Complete Reference™, Web Design: The Complete Reference™, Web Site Engineering, and numerous others. Mr. Powell also teaches Web development classes for the University of California, San Diego Computer Science and Engineering Department, as well as for the Information Technologies program at UCSD Extension. He holds a B.S. from UCLA and an M.S. in Computer Science from UCSD. Fritz Schneider is a Staff Software Engineer at Google where he leads web serving and UI infrastructure projects. His past work includes a social web startup acquired by Google, contributions to anonymizing web proxies, and a prior stint at Google where he improved browser security by founding the Safe Browsing project. He holds a B.S. in Computer Engineering from Columbia and an M.S. in Computer Science from UCSD. About the Technical Editor Christie Sorenson is a senior software engineer at ZingChart. She has worked on JavaScript-based systems in analytics, content management, and business applications since 1997 and has been fascinated with the evolution of the language and its users. She has collaborated with Thomas on numerous other projects, including Ajax: The Complete Reference ™ and HTML & CSS: The Complete Reference ™. She has a B.S. in Computer Science from UC San Diego and now lives in San Francisco with her husband Luke and daughters, Ali and Keira. As a lifelong fan of the San Francisco Giants, any references in this book to the team are solely due to her influence. Contents at a Glance Part I Introduction 1 Introduction to JavaScript 2 Overview of JavaScript Core Language Features Part II Core Language 3 Data Types and Variables 4 Operators, Expressions, and Statements 5 Functions 6 Objects 7 Array, Date, Math, and Type-Related Objects 8 Regular Expressions 9 JavaScript Object Models 10 The Standard Document Object Model 11 Event Handling Part III Applied JavaScript 12 Windows, Frames, and Overlays 13 Form Handling 14 User Interface Elements 15 Ajax and Remote JavaScript 16 Browser Management 17 Media Management 18 Trends and Practices Appendix JavaScript Reserved Words Index Contents Acknowledgments Introduction Part I Introduction Chapter 1 Introduction to JavaScript Hello JavaScript World Hello Errors Adding JavaScript to XHTML Documents The <script> Element Event Handlers Linked Scripts JavaScript Pseudo-URL JavaScript Usage Considerations Script Masking The <noscript> Element Language Versions Cross-Browser Concerns Living with Other Scripts JavaScript: A Real Programming Language History of JavaScript Common Uses of JavaScript The Rise of JavaScript Libraries The Future of JavaScript Summary Chapter 2 Overview of JavaScript Core Language Features Basic Definitions Execution Order Case Sensitivity HTML and Case Sensitivity Whitespace Statements Semicolons Blocks Variables Basic Data Types Weak Typing Type Conversion Composite Types Objects Arrays Functions as Data Types Regular Expression Literals and Object Expressions Arithmetic Operators Assignment Operators Logical Operators Conditional Operator Type Operators Comma Operator Relational Operators Operator Precedence and Associativity Flow Control Loops Loop Control Input and Output in JavaScript Functions Scoping Rules Regular Expressions Exceptions Comments ECMAScript 5 Changes “use strict”; Native JSON Support Function.prototype.bind( ) ISO Dates Native Array Additions String.prototype.trim( ) Object/Property Additions Emerging Features Summary Part II Core Language Chapter 3 Data Types and Variables Key Concepts Weak Typing JavaScript’s Primitive Types Numbers Strings Booleans Undefined and Null Composite Types Objects Arrays Functions The typeof Operator Type Conversion Conversion Rules for Primitive Types Promotion of Primitive Data to Objects Explicit Type Conversion Variables Identifiers Variable Declaration Implicit Variable Declaration Variable Scope Constants Summary Chapter 4 Operators, Expressions, and Statements Statement Basics Whitespace Termination: Semicolons and Returns Blocks Operators Assignment Operator Arithmetic Operators Bitwise Operators Bitwise Shift Operators Combining Arithmetic and Bitwise Operations with Assignment Increment and Decrement Comparison Operators Logical Operators ? Operator Comma Operator void Operator typeof Object Operators Operator Precedence and Associativity Core JavaScript Statements if Statements switch while Loops do-while Loops for Loops Loop Control with continue and break Object-Related Statements
Description: