You are previewing JavaScript: The Complete Reference, Third Edition.
O'Reilly logo
JavaScript: The Complete Reference, Third Edition

Book Description

This fully updated definitive JavaScript guide covers the latest features—including its uses in Ajax and HTML5-based Web applications

JavaScript: The Complete Reference, Third Edition is completely revised to cover the newest changes to JavaScript up to version 1.9, the latest browser-specific features for Internet Explorer, Firefox, and Chrome, as well popular JavaScript frameworks like jQuery. All examples presented focus on the emergence of HTML5 and its various APIs including canvas, storage, and more.

This comprehensive guide shows you how to build dynamic, cross-browser Web sites and applications using JavaScript. You’ll learn not only the syntax of language, but its related object models, and combine this knowledge to develop practical applications.

JavaScript: The Complete Reference, Third Edition

• Covers the newest versions of JavaScript, DOM, and Ajax (XMLHttpRequest) specifications

• Updated for latest browsers and specifications including HTML5

• Includes the newest features of JavaScript

• Addresses modern JavaScript coding techniques popularized since the previous edition

Comprehensive coverage:

Introduction to JavaScript; JavaScript Core Features; Data Types & Variables; Operators, Expressions & Statements; Functions; Objects; Array, Date, Math; Regular Expressions; JavaScript Object Models; The Standard DOM; Event Handling; CNT, Windows & Frames; Handling Documents; Form Handling; DHTML and Beyond; Canvas; JavaScript UIs; Browser Detection; Cookies & Persistence; JS & Embedded Objects; Ajax and Remote JS; JavaScript and XML; Browser Specific JS; JavaScript Security; JavaScript Practices

Table of Contents

  1. Cover Page
  2. JavaScript: The Complete Reference, Third Edition
  3. Copyright Page
  4. Contents at a Glance
  5. Contents
  6. Acknowledgments
  7. Introduction
  8. Part I Introduction
    1. Chapter 1 Introduction to JavaScript
      1. Hello JavaScript World
        1. Hello Errors
      2. Adding JavaScript to XHTML Documents
        1. The <script> Element
        2. Event Handlers
        3. Linked Scripts
        4. JavaScript Pseudo-URL
      3. JavaScript Usage Considerations
        1. Script Masking
        2. The <noscript> Element
        3. Language Versions
        4. Cross-Browser Concerns
        5. Living with Other Scripts
      4. JavaScript: A Real Programming Language
        1. History of JavaScript
        2. Common Uses of JavaScript
        3. The Rise of JavaScript Libraries
        4. The Future of JavaScript
      5. Summary
    2. Chapter 2 Overview of JavaScript Core Language Features
      1. Basic Definitions
      2. Execution Order
      3. Case Sensitivity
        1. HTML and Case Sensitivity
      4. Whitespace
      5. Statements
        1. Semicolons
        2. Blocks
      6. Variables
      7. Basic Data Types
        1. Weak Typing
        2. Type Conversion
      8. Composite Types
        1. Objects
        2. Arrays
        3. Functions as Data Types
        4. Regular Expression Literals and Object
      9. Expressions
        1. Arithmetic Operators
        2. Assignment Operators
        3. Logical Operators
        4. Conditional Operator
        5. Type Operators
        6. Comma Operator
        7. Relational Operators
        8. Operator Precedence and Associativity
      10. Flow Control
      11. Loops
        1. Loop Control
      12. Input and Output in JavaScript
      13. Functions
      14. Scoping Rules
      15. Regular Expressions
      16. Exceptions
      17. Comments
      18. ECMAScript 5 Changes
        1. “use strict”;
        2. Native JSON Support
        3. Function.prototype.bind( )
        4. ISO Dates
        5. Native Array Additions
        6. String.prototype.trim( )
        7. Object/Property Additions
        8. Emerging Features
      19. Summary
  9. Part II Core Language
    1. Chapter 3 Data Types and Variables
      1. Key Concepts
        1. Weak Typing
      2. JavaScript’s Primitive Types
        1. Numbers
        2. Strings
        3. Booleans
        4. Undefined and Null
      3. Composite Types
        1. Objects
        2. Arrays
        3. Functions
        4. The typeof Operator
      4. Type Conversion
        1. Conversion Rules for Primitive Types
        2. Promotion of Primitive Data to Objects
        3. Explicit Type Conversion
      5. Variables
        1. Identifiers
        2. Variable Declaration
        3. Implicit Variable Declaration
        4. Variable Scope
      6. Constants
      7. Summary
    2. Chapter 4 Operators, Expressions, and Statements
      1. Statement Basics
        1. Whitespace
        2. Termination: Semicolons and Returns
        3. Blocks
      2. Operators
        1. Assignment Operator
        2. Arithmetic Operators
        3. Bitwise Operators
        4. Bitwise Shift Operators
        5. Combining Arithmetic and Bitwise Operations with Assignment
        6. Increment and Decrement
        7. Comparison Operators
        8. Logical Operators
        9. ? Operator
        10. Comma Operator
        11. void Operator
        12. typeof
        13. Object Operators
        14. Operator Precedence and Associativity
      3. Core JavaScript Statements
        1. if Statements
        2. switch
        3. while Loops
        4. do-while Loops
        5. for Loops
        6. Loop Control with continue and break
        7. Object-Related Statements
        8. Other Statements
      4. Summary
    3. Chapter 5 Functions
      1. Function Basics
        1. Parameter Passing Basics
        2. Return Statements
        3. Parameter Passing: In and Out
      2. Global and Local Variables
        1. Variable Naming for Scope
        2. Inner Functions
      3. Closures
      4. Functions as Objects
        1. Function Literals
        2. Static Variables
        3. Advanced Parameter Passing
        4. Advanced Function Properties and Methods
      5. Recursive Functions
      6. Summary
    4. Chapter 6 Objects
      1. Objects in JavaScript
      2. Object Fundamentals
        1. Object Creation
        2. Object Destruction and Garbage Collection
        3. Properties
        4. Objects Are Reference Types
        5. Common Properties and Methods
      3. Object-Oriented JavaScript
        1. Prototype-Based Objects
        2. Constructors
        3. Prototypes
        4. Class Properties
        5. Inheritance via the Prototype Chain
        6. Overriding Default Methods and Properties
      4. ECMAScript 5 Object-Related Changes
      5. JavaScript’s Object-Oriented Reality
      6. Summary
    5. Chapter 7 Array, Date, Math, and Type-Related Objects
      1. Array
        1. Accessing Array Elements
        2. Adding and Changing Array Elements
        3. Removing Array Elements
        4. The length Property
        5. Arrays as Stacks and Queues
        6. Manipulating Arrays
        7. Multidimensional Arrays
        8. Extending Arrays with Prototypes
        9. ECMAScript 5 Array Additions
      2. Boolean
      3. Date
        1. Creating Dates
        2. Manipulating Dates
        3. ECMAScript 5 Date Additions
      4. Global
      5. Math
        1. Random Numbers
      6. Number
      7. String
        1. Examining Strings
        2. Manipulating Strings
        3. Marking Up Strings as Traditional HTML
        4. ECMAScript 5 String Changes
      8. Summary
    6. Chapter 8 Regular Expressions
      1. The Need for Regular Expressions
      2. Introduction to JavaScript Regular Expressions
        1. Creating Patterns
      3. RegExp Object
        1. test( )
        2. Subexpressions
        3. compile( )
        4. exec( )
        5. RegExp Properties
      4. String Methods for Regular Expressions
        1. search( )
        2. split( )
        3. replace( )
        4. match( )
      5. Advanced Regular Expressions
        1. Multiline Matching
        2. Noncapturing Parentheses
        3. Lookahead
        4. Greedy Matching
      6. Limitations of Regular Expressions
      7. Summary
    7. Chapter 9 JavaScript Object Models
      1. Object Model Overview
      2. The Initial JavaScript Object Model
      3. The Document Object
        1. Accessing Document Elements by Position
        2. Accessing Document Elements by Name
      4. Simple Event Handling
        1. Setting Event Handlers Inline
        2. Direct Assignment of Event Handlers
        3. Setting Event Listeners
        4. Invoking Event Handlers
      5. JavaScript + DOM + Selection + Events = Program
      6. The Evolution of the JavaScript Object Model
        1. The Early Evolution of Browser Object Models
        2. DHTML-Oriented Object Models
        3. Internet Explorer 4’s DHTML Object Model
        4. Beyond the DHTML Object Models
      7. Summary
    8. Chapter 10 The Standard Document Object Model
      1. DOM Flavors
        1. What Browsers Say Is Implemented
      2. Document Trees
      3. Basic Element Access: getElementById( )
        1. Tree Traversal Basics
      4. Other Element Access Methods
        1. getElementsByName( )
        2. Common JavaScript Collections
        3. getElementsByTagName( )
        4. Common Tree Traversal Starting Points
        5. document.getElementsByClassName( )
        6. querySelector( ) and querySelectorAll( )
      5. Creating Nodes
      6. Appending and Inserting Nodes
        1. Text Nodes and normalize( ) Method
        2. insertBefore( ) method
        3. Other Insertion Methods
      7. Dynamic Markup Insertion Realities
        1. innerHTML
        2. outerHTML
      8. innerText and outerText
        1. insertAdjacentHTML( ) and insertAdjacentText( )
        2. document.write( ) and document.writeln( )
      9. Copying Nodes
      10. Deleting and Replacing Nodes
        1. Modifying Text Nodes
      11. Manipulating Attributes
        1. Attribute Nodes
      12. Other Node Methods
      13. Namespaces
      14. The DOM and HTML Elements
        1. HTML Element Mappings
      15. DOM Table Manipulation
      16. The DOM and Css
        1. Inline Style Manipulation
        2. Dynamic Style Using Classes and Collections
        3. Computed Style
        4. Accessing Complex Style Rules
      17. DOM Traversal API
      18. DOM Range Selections
      19. Continued DOM Evolution
      20. Summary
    9. Chapter 11 Event Handling
      1. Overview of Events and Event Handling
      2. The Traditional Event Model
        1. Event Binding with HTML Attributes
        2. Binding Event Handler Attributes with JavaScript
        3. Event Handler Scope Details
        4. Return Values
        5. Firing Events Manually
      3. Overview of Modern Event Models
      4. Internet Explorer’s Proprietary Model
        1. attachEvent( ) and detachEvent( )
        2. Event Object
        3. Event Bubbling
        4. Simulating Event Routing
        5. Event Creation
      5. DOM Event Model
        1. Traditional Binding Changes
        2. addEventListener( ) and removeEventListener( )
        3. Event Object
        4. Preventing Default Actions
        5. Controlling Event Propagation
        6. Event Creation
      6. Event Types
        1. Mouse Events
        2. Ui Events
        3. Focus Events
        4. Keyboard Events
        5. Text Events
        6. Mutation Events
        7. Nonstandard Events
        8. Custom Events
        9. Browser State and Loading Events
      7. Event Model Issues
      8. Summary
  10. Part III Applied JavaScript
    1. Chapter 12 Windows, Frames, and Overlays
      1. Introduction to the Window Object
      2. Dialogs
        1. alert( )
        2. confirm( )
        3. prompt( )
      3. Emerging and Proprietary Dialog Methods
        1. showModalDialog( )
        2. showModelessDialog( )
        3. createPopup( )
      4. Opening and Closing Generic Windows
        1. Window Features
      5. Detecting and Controlling Window Chrome
      6. Practicalities of Spawned Windows
        1. Building Window Contents
        2. Overlays Instead of Windows
      7. Controlling Windows
        1. focus( ) and blur( )
        2. stop( )
        3. print( )
        4. find( )
        5. Moving Windows
        6. Resizing Windows
        7. Scrolling Windows
        8. Accessing and Setting a Window’s Location
        9. Hash Values in URLs
      8. Manipulating a Window’s History
        1. pushstate( ) and replacestate( )
        2. Trying to Control the Window’s Status Bar
      9. Setting Window Timeouts and Intervals
      10. Window Events
      11. Interwindow Communication Basics
        1. Interwindow Message Passing with postMessage( )
      12. Frames: A Special Case of Windows
        1. Inline Frames
        2. Applied Frames
      13. Summary
    2. Chapter 13 Form Handling
      1. The Need for JavaScript Form Checking
      2. Form Basics
        1. Accessing Forms and Fields
      3. Form Fields
        1. Common Input Element Properties
        2. Buttons
        3. Traditional Text Fields
        4. HTML5 Semantic Text Fields
        5. Text Areas
        6. Checkboxes and Radio Buttons
        7. Select Menus
        8. Date Pickers
        9. Color
        10. Sliders
        11. File Upload Fields
        12. Hidden Fields
        13. Other Form Features: Label, Fieldset, Output, and Legend
      4. Form Usability and JavaScript
        1. First Field Focus
        2. Labels and Field Selection
        3. Advisory and Status Messages
        4. Data Lists
        5. Disabled and Read-Only Fields
      5. Form Validation
        1. Abstracting Form Validation
        2. Error Messages
        3. onchange Handlers
        4. Keyboard Masking
      6. HTML5 Validation Improvements
        1. Validation Attributes
        2. Validation Properties and Methods
        3. novalidate Attribute
      7. Miscellaneous Html5 Form Changes
      8. Internationalization
      9. Summary
    3. Chapter 14 User Interface Elements
      1. Adding JavaScript
        1. Exploring Progressive Enhancement
        2. Graceful Degradation Approaches
      2. HTML5 Support for Rich Interaction
        1. Menus and Context Menus
        2. Drag-and-Drop Functionality
        3. Content Editing
        4. Revealing Content on Demand
        5. User Feedback
      3. Summary
    4. Chapter 15 Ajax and Remote JavaScript
      1. Ajax Defined
      2. Hello Ajax World
      3. XML HttpRequest Object
      4. XHR Instantiation and Cross-Browser Concerns
        1. ActiveX XHR Fallback Details
        2. A Cross-Browser XHR Wrapper
      5. XHR Request Basics
      6. Synchronous Requests
      7. Asynchronous Requests
      8. Sending Data via GET
      9. Sending Data via Post
      10. Using Other HTTP Methods
      11. Setting Request Headers
      12. Response Basics
        1. readyState Explored
        2. status and statusText
        3. responseText
        4. responseXML
        5. response and responseType
        6. JSON
        7. Script Responses
        8. Response Headers
      13. Controlling Requests
      14. Authentication with XHRs
      15. Propriety and Emerging XHR Features
        1. Managing MIME Types
        2. Multipart Responses
        3. onload, onloadstart, and onloadend
        4. onprogress and Partial Responses
        5. onerror
      16. Form Serialization
        1. File Uploads with iframes
      17. Cross-Domain Ajax Requests
        1. Internet Explorer’s XDRs
      18. Non-XHR Communication Methods
        1. Image Tag
        2. Script Tag
      19. Comet and Sockets
        1. Polling: Fast or Long
        2. The Long Slow Load
        3. WebSockets
      20. Ajax Implications and Challenges
      21. Summary
    5. Chapter 16 Browser Management
      1. Browser Detection Basics
      2. The Navigator Object
      3. What to Detect
        1. Technology Detection
        2. Visual Detection: Screen Object
        3. User Characteristics
      4. Network State and Performance
        1. Simple Page Load Metrics
        2. window.performance.timing
      5. Browser Control
        1. Simulating Browser Button Presses
      6. State Management
        1. Cookies in JavaScript
        2. Using Cookies for User State Management
        3. Cookie Limitations
        4. Storage
        5. IndexedDB
        6. AppCache
      7. Script Execution
        1. Web Workers
      8. Summary
    6. Chapter 17 Media Management
      1. Image Handling
        1. Applied Images
      2. DHTML, DOMEffects, and Animation
      3. Client-Side Bitmap Graphics with <canvas>
        1. Drawing and Styling Lines and Shapes
        2. Drawing Arcs and Curves
        3. Scaling, Rotating, and Translating Drawings
        4. Using Bitmaps in Drawings
        5. Text Support for <canvas>
        6. Compositing
        7. Saving State
        8. <canvas> Considerations
      4. Client-Side Vector Graphics with SVG
        1. Including SVG
        2. Drawing with SVG
        3. SVG Interaction and Animation
      5. HTML5 Media Handling
        1. <video>
        2. <audio>
        3. Media Events
      6. Plug-Ins
        1. Embedding Content for Plug-Ins
        2. MIME Types
        3. Detecting Specific Plug-Ins
        4. Interacting with Plug-Ins
      7. ActiveX
        1. Including ActiveX Controls
        2. Interacting with ActiveX Controls
      8. Java Applets
        1. Including Applets
        2. Java Detection
        3. Accessing Applets in JavaScript
        4. Accessing JavaScript with Applets
      9. The Uncertain Future
      10. Summary
    7. Chapter 18 Trends and Practices
      1. Writing Quality JavaScript
        1. Coding Style
        2. Comments and Documentation
        3. Understanding Errors
        4. Debugging
        5. Defensive Programming
        6. Utilizing Frameworks and Libraries
      2. Security
        1. JavaScript Protection
      3. JavaScript’s Security Policy
        1. Same-Origin Policy
        2. Trusted External Scripts
        3. Cross-Site Scripting
        4. Cross-Site Request Forgery (CSRF)
      4. Performance
        1. Performance Measurements and Tools
        2. Page-Load Performance
        3. Runtime Performance
      5. Trends
        1. JavaScript Everywhere
        2. “Fixing” and Hiding JavaScript
        3. Accepting JavaScript
      6. Summary
  11. Appendix JavaScript Reserved Words
    1. Reserved Quirks
  12. Index