Cover image for Learning JavaScript, 2nd Edition

Book description

If you're new to JavaScript, or an experienced web developer looking to improve your skills, Learning JavaScript provides you with complete, no-nonsense coverage of this quirky yet essential language for web development. You'll learn everything from primitive data types to complex features, including JavaScript elements involved with Ajax and dynamic page effects. By the end of the book, you'll be able to work with even the most sophisticated libraries and web applications. Complete with best practices and examples of JavaScript use, this new edition shows you how to integrate the language with the browser environment, and how to practice proper coding techniques for standards-compliant websites. This book will help you:

  • Learn the JavaScript application structure, including basic statements and control structures

  • Identify JavaScript objects -- String, Number, Boolean, Function, and more

  • Use browser debugging tools and troubleshooting techniques

  • Understand event handling, form events, and JavaScript applications with forms

  • Develop with the Browser Object Model, the Document Object Model, and custom objects you create

  • Learn about browser cookies and more modern client-side storage techniques

  • Get details for using XML or JSON with Ajax applications

Learning JavaScript follows proven learning principles to help you absorb the concepts at an easy pace, so you'll learn how to create powerful and responsive applications in any browser.

Table of Contents

  1. Learning JavaScript
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Audience
    2. Assumptions and Approach
      1. The Development Environment
    3. How the Book Is Organized
      1. A Chapter Breakdown
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  4. 1. Hello JavaScript!
    1. Hello World!
    2. Hello World! Once Again
      1. The script Tag
      2. JavaScript Versus ECMAScript Versus JScript
      3. Defining Functions in JavaScript
      4. Event Handlers
      5. The document Browser Object
      6. The property Operator
      7. The var Keyword and Scope
      8. Statements
      9. Comments
      10. What You Didn’t See: HTML Comments and CDATA Sections
    3. JavaScript Files
    4. Accessibility and JavaScript Best Practices
      1. Accessibility Guidelines
      2. noscript
  5. 2. JavaScript Data Types and Variables
    1. Identifying Variables
      1. Naming Guidelines
    2. Primitive Types
    3. The String Data Type
      1. String Escape Sequences
      2. String Encoding
      3. Converting to Strings
    4. The Boolean Data Type
    5. The Number Data Type
    6. The null and undefined Variables
    7. Constants: Named but Not Variables
    8. Test Your Knowledge: Quiz
    9. Test Your Knowledge: Answers
  6. 3. Operators and Statements
    1. The Format of a JavaScript Statement
    2. The Assignment Statement
      1. The Arithmetic Operators
      2. The Unary Operators
      3. Operator Precedence
      4. A Handy Shortcut: Assignment with Operation
      5. The Bitwise Operators
    3. Conditional Statements and Program Flow
      1. The if…else Conditional Statement
      2. The switch Conditional Statement
    4. The Conditional Operators
      1. The Equality and Identity (String Equality) Operators
      2. Other Relational Operators
      3. The One and Only JavaScript Ternary Operator
    5. The Logical Operators
    6. Advanced Statements: The Loops
      1. The while Loop
      2. The do…while Loop
      3. The for Loops
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  7. 4. The JavaScript Objects
    1. Primitive Data Types As Objects
    2. Boolean, Number, and String
      1. The Boolean Object
      2. The Number Object, Static Properties, and Instance Methods
      3. The String Object
    3. Regular Expressions and RegExp
      1. The RegExp Methods: test and exec
      2. Working with Regular Expressions
    4. The Date Object
    5. The Math Object
      1. The Math Properties
      2. The Math Methods
    6. JavaScript Arrays
      1. FIFO Queues
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  8. 5. Functions
    1. Declarative Functions
      1. Function Naming Conventions and Size
      2. Function Returns and Arguments
    2. Anonymous Functions
    3. Function Literals
      1. Functions and Recursion
      2. Nested Functions, Function Closure, and Memory Leaks
      3. Callback Functions
    4. Function Type Summary
    5. Function Scope
    6. Function As Object
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  9. 6. Troubleshooting, Debugging, and Cross-Browser Issues
    1. Simple Ways to Debug
    2. Development and Debugging Tools by Browser
      1. Firefox and Firebug
      2. Using console.log
      3. Firefox, the Web Developer Toolkit, and NoScript
      4. Opera and Dragonfly
      5. Safari/WebKit and the Web Inspector
      6. Internet Explorer
    3. Dealing with Cross-Browser Differences
      1. Object Detection
      2. Where Object Detection Fails
      3. DOCTYPE, X-UA-Compatible, and Quirks Mode
      4. Breaking Backward Compatibility: The IE8 http-equiv Meta Tag
    4. Test Your Knowledge: Quiz
    5. Test Your Knowledge: Answers
  10. 7. Catching Events
    1. The Events
    2. Level 0 Event Handling
      1. The Event Object
      2. Event Bubbling
      3. Event Handlers and this
    3. The DOM Level 2 Event Model
      1. Generating Events
    4. Test Your Knowledge: Quiz
    5. Test Your Knowledge: Answers
  11. 8. Forms, Form Events, and Validation
    1. Attaching Events to Forms: Different Approaches
      1. Cross-Browser Event Handling
      2. Canceling an Event
    2. Selection
      1. Dynamically Modifying the Selection
      2. Selection and Auto-Selection
    3. Radio Buttons and Checkboxes
    4. The text, textarea, password, and hidden Input Elements
      1. Text Validation
    5. Input Fields and Regular Expression Validation
    6. Forms, the Sandbox, and XSS
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  12. 9. Browser As Puzzle Box
    1. The Structure of the Browser at a Glance
    2. The window Object
    3. Creating and Controlling Windows
      1. The Dialogs: alert, confirm, and prompt
      2. Creating Custom Windows
      3. Modifying a Window
    4. Frames
      1. The location Object
      2. Remote Scripting with the iframe
    5. Adding and Controlling Timers
    6. The history, screen, and navigator Objects
      1. The history Object
      2. The screen Object
      3. The navigator Object
      4. The history, screen, and navigator Properties in Action
    7. The Document Object
      1. Links
      2. Images
    8. innerHTML
    9. Test Your Knowledge: Quiz
    10. Test Your Knowledge: Answers
  13. 10. Cookies and Other Client-Side Storage Techniques
    1. The JavaScript Sandbox and Cookie Security
      1. The Same-Origin Security Policy
      2. Using document.domain
    2. All About Cookies
      1. Storing and Reading Cookies
    3. Flash Shared Objects, Google Gears, and HTML5 DOM Storage
    4. Test Your Knowledge: Quiz
    5. Test Your Knowledge: Answers
  14. 11. The DOM, or Web Page As Tree
    1. A Tale of Two Interfaces
    2. The DOM HTML API
      1. The DOM HTML Objects and Their Properties
      2. DOM (HTML) Collections
    3. Understanding the DOM: The Core API
      1. The DOM Tree
      2. Node Properties and Methods
      3. The DOM Core Document Object
    4. Element and Access in Context
    5. Modifying the Tree
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  15. 12. Dynamic Pages
    1. JavaScript, CSS, and the DOM
      1. The Style Property/Attributes
    2. Fonts and Text
      1. Font Style Properties
      2. Text Properties
    3. Position and Movement
      1. Dynamic Positioning
      2. Drag-and-Drop
    4. Size and Clipping
      1. Overflow and Dynamic Content
      2. The Clipping Rectangle
    5. Display, Visibility, and Opacity
      1. The Right Tool for the Right Effect
      2. Just-in-Time Information
    6. Revisiting the DOM: Collapsing Forms, Query Selectors, and Class Names
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  16. 13. Creating Custom JavaScript Objects
    1. The JavaScript Object and Prototyping
      1. Prototyping
    2. Creating Your Own Custom JavaScript Objects
      1. Enter the Function
      2. Public and Private Properties and Where this Enters the Picture
      3. Getters and Setters
    3. Object Encapsulation
    4. Chaining Constructors and JavaScript Inheritance
    5. One-Off Objects
    6. Object Libraries: Packaging Your Objects for Reuse
    7. Advanced Error Handling Techniques (try, throw, catch)
    8. Test Your Knowledge: Quiz
    9. Test Your Knowledge: Answers
  17. 14. Moving Outside the Page with Ajax
    1. How Ajax Works
    2. Hello Ajax World!
    3. The XMLHttpRequest Object and Preparing to Send the Request
      1. Object, Object, Who Has the Object?
      2. The XMLHttpRequest Methods
    4. Processing the Web Request Return
      1. Checking the readyState and status of an Ajax Request
      2. Processing the Web Request Result
    5. Ajax: It’s Not Only Code
      1. The Dynamic Nature of Ajax
      2. Ajax Accessibility and Degrading Gracefully
      3. Security and Workarounds
    6. JavaScript and Ajax Libraries
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  18. 15. Ajax Data: XML or JSON?
    1. XML-Formatted Ajax Results
      1. The Data’s MIME Type
      2. Generating the XML on the Server
      3. Processing the XML on the Client
    2. JavaScript Object Notation
      1. A Simple JSON Application
      2. The JSON Object
    3. Test Your Knowledge: Quiz
    4. Test Your Knowledge: Answers
  19. Index
  20. About the Author
  21. Colophon
  22. Copyright