You are previewing JavaScript Step by Step, Second Edition.
O'Reilly logo
JavaScript Step by Step, Second Edition

Book Description

Teach yourself the essentials of JavaScript -- one step at time. This book provides hands-on guidance for using JavaScript techniques and key features to create interactive Web applications.

Table of Contents

  1. JavaScript Step by Step, Second Edition
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Acknowledgements
  5. Introducing JavaScript Step by Step, Second Edition
    1. Who Should Read This Book?
    2. Features and Conventions of This Book
    3. What’s in the Companion Content
    4. Downloading the Companion Content
    5. Minimum System Requirements
  6. Getting Help
    1. Getting Help with This Book and the Companion Content
  7. I. JavaWhat? The Where, Why, and How of JavaScript
    1. 1. JavaScript Is More Than You Might Think
      1. A Brief History of JavaScript
        1. Enter Internet Explorer 3.0
        2. And Then Came ECMAScript
        3. So Many Standards...
        4. The DOM
      2. What’s in a JavaScript Program?
        1. Using the javascript pseudo-protocol and a function
      3. JavaScript Placement on Your Webpage
      4. What JavaScript Can Do
      5. What JavaScript Can’t Do
        1. JavaScript Can’t Be Forced on a Client
        2. JavaScript Can’t Guarantee Data Security
        3. JavaScript Can’t Cross Domains
        4. JavaScript Doesn’t Do Servers
      6. Tips for Using JavaScript
        1. Where JavaScript Fits
        2. Which Browsers Should the Site Support?
      7. What’s New in ECMAScript Version 5?
        1. New Array Methods
        2. New Controls on Object Properties
        3. New JSON Object
        4. Changes to the Date Object
        5. A New Strict Mode
        6. Browser Support
      8. Exercises
    2. 2. Developing in JavaScript
      1. JavaScript Development Options
      2. Configuring Your Environment
      3. Writing JavaScript with Visual Studio 2010
        1. Your First Web (and JavaScript) Project with Visual Studio 2010
          1. Creating a web project with JavaScript in Visual Studio 2010
        2. Using External JavaScript Files with Visual Studio 2010
          1. Creating an external file for JavaScript using Visual Studio 2010
      4. Writing JavaScript with Eclipse
        1. Your First Web (and JavaScript) Project with Eclipse
          1. Creating a web project with JavaScript in Eclipse
        2. Using External JavaScript Files with Eclipse
          1. Creating an external file for JavaScript using Eclipse
      5. Writing JavaScript Without an IDE
        1. Your First Web (and JavaScript) Project with Notepad
          1. Creating a webpage with JavaScript in Notepad
        2. Using External JavaScript Files Without an IDE
          1. Creating an external file for JavaScript using Notepad
      6. Debugging JavaScript
      7. Exercises
    3. 3. JavaScript Syntax and Statements
      1. A Bit of Housekeeping
        1. Case Sensitivity
        2. White Space
        3. Comments
        4. Semicolons
        5. Line Breaks
        6. Placing JavaScript Correctly
      2. JavaScript Statements
        1. What’s in a Statement?
        2. The Two Types of JavaScript Statements
      3. Reserved Words in JavaScript
      4. A Quick Look at Functions
        1. Placing JavaScript with a user-defined function
      5. JavaScript’s New Strict Mode
      6. Exercises
    4. 4. Working with Variables and Data Types
      1. Data Types in JavaScript
        1. Working with Numbers
          1. Performing hexadecimal math with JavaScript
          2. Numeric Functions
            1. Testing the isNaN() function
          3. The Math Object
        2. Working with Strings
          1. Escaping Quotation Marks
          2. Other Escape Characters
            1. Using escape sequences
          3. String Methods and Properties
            1. Obtaining the length of a string
        3. Booleans
        4. Null
        5. Undefined
        6. Objects
        7. Arrays
      2. Defining and Using Variables
        1. Declaring Variables
        2. Variable Types
        3. Variable Scope
          1. Examining variable scope
          2. Installing Firebug
          3. Troubleshooting with Firebug
        4. The Date Object
          1. Writing the date and time to a webpage
          2. Counting down to a certain date in the future
          3. Calculating render time
      3. Using the RegExp Object
        1. The Syntax of Regular Expressions
        2. References and Garbage Collection
      4. Learning About Type Conversions
        1. Number Conversions
        2. String Conversions
        3. Boolean Conversions
      5. Exercises
    5. 5. Using Operators and Expressions
      1. Meet the Operators
      2. Additive Operators
      3. Multiplicative Operators
      4. Bitwise Operators
      5. Equality Operators
        1. Testing the equality operators
      6. Relational Operators
        1. The in Operator
        2. The instanceof Operator
      7. Unary Operators
        1. Incrementing and Decrementing
        2. Converting to a Number with the Plus Sign
        3. Creating a Negative Number with the Minus Sign
        4. Negating with bitwise not and logical not
        5. Using the delete Operator
          1. Using the delete operator with objects
        6. Returning Variable Types with the typeof Operator
          1. Using the typeof operator
        7. The void Operator
      8. Assignment Operators
      9. The Comma Operator
      10. Exercises
  8. II. Applying JavaScript
    1. 6. Controlling Flow with Conditionals and Loops
      1. If (and How)
        1. Syntax for if Statements
        2. The prompt() Function in Internet Explorer
          1. Using if to make decisions about program flow
        3. Compound Conditions
      2. Using else if and else Statements
        1. Using multiple levels of conditionals and a regular expression
      3. Working with Ternary Conditionals
      4. Testing with switch
      5. Looping with while
        1. The while Statement
          1. Making Sure the Code Executes at Least Once
          2. Changing the Condition
        2. The do...while Statement
      6. Using for Loops
        1. The for Loop
          1. Using a for loop with an array
        2. The for...in Loop
          1. Using a for...in loop
        3. The for each...in Loop
      7. Validating Forms with Conditionals
        1. Validating input with a conditional statement
      8. Exercises
    2. 7. Working with Functions
      1. What’s in a Function?
        1. Function Arguments
        2. Variable Scoping Revisited
        3. Return Values
        4. More on Calling Functions
        5. Anonymous/Unnamed Functions (Function Literals)
        6. Closures
      2. Methods
      3. A Look at Dialog Functions
        1. Obtaining input with confirm()
      4. Exercises
    3. 8. Objects in JavaScript
      1. Object-Oriented Development
        1. Objects
        2. Properties
        3. Methods
        4. Classes
      2. Creating Objects
        1. Adding Properties to Objects
          1. Displaying Object Properties
            1. Looping through object properties
          2. Looking for a Property
        2. Adding Methods to Objects
      3. Finding Out More About Arrays
        1. The length Property
        2. Array Methods
          1. Adding and Removing Elements
            1. Adding elements with concat()
      4. Taking Advantage of Built-in Objects
        1. The Global Object
          1. Making URIs Safe
          2. Using the eval() method
      5. Exercises
    4. 9. The Browser Object Model
      1. Introducing the Browser
        1. The Browser Hierarchy
        2. Events
      2. A Sense of Self
      3. Getting Information About the Screen
        1. Determining a visitor’s screen height and width
      4. Using the navigator Object
        1. Looking at the navigator object
        2. Using the navigator object to detect Java
      5. The location Object
        1. Looking at the location object
      6. The history Object
      7. Exercises
  9. III. Integrating JavaScript into Design
    1. 10. The Document Object Model
      1. The Document Object Model Defined
        1. DOM Level 0: The Legacy DOM
        2. DOM Levels 1 and 2
        3. The DOM as a Tree
        4. Working with Nodes
      2. Retrieving Elements
        1. Retrieving Elements by ID
        2. Retrieving by Tag Name
        3. HTML Collections
        4. Working with Siblings
      3. Working with Attributes
        1. Viewing Attributes
          1. Retrieving element attributes
        2. Setting Attributes
      4. Creating Elements
        1. Adding Text
        2. Adding an Element and Setting an ID
      5. Deleting Elements
      6. Exercises
    2. 11. JavaScript Events and the Browser
      1. Understanding Window Events
        1. The Event Models
          1. Using the DOM 0 Model
          2. Newer Event Models: W3C and Internet Explorer
        2. A Generic Event Handler
      2. Detecting Visitor Information
        1. A Brief Look at the userAgent Property
        2. Feature Testing
        3. Keeping JavaScript Away from Older Browsers
        4. Other navigator Properties and Methods
      3. Opening, Closing, and Resizing Windows
        1. Window Opening Best Practices
        2. Opening Tabs: No JavaScript Necessary?
        3. Resizing and Moving Windows
      4. Timers
      5. Exercises
    3. 12. Creating and Consuming Cookies
      1. Understanding Cookies
      2. Creating Cookies with JavaScript
        1. Looking at a Simple Cookie
        2. Setting a Cookie’s Expiration Date
          1. Adding an expiration date to a cookie
        3. Setting the Cookie Path
        4. Setting the Cookie Domain
        5. Working with Secure Cookies
      3. Reading Cookies with JavaScript
      4. Removing Cookies
      5. Exercises
    4. 13. Working with Images in JavaScript
      1. Working with Image Rollovers
        1. A Simple Rollover
        2. Modern Rollovers
          1. Creating portable rollovers
      2. Preloading Images
      3. Working with Slideshows
        1. Creating a Slideshow
        2. Moving Backward
          1. Creating a Previous button
      4. Working with Image Maps
      5. Exercises
    5. 14. Using JavaScript with Web Forms
      1. JavaScript and Web Forms
      2. Obtaining Form Data
      3. Working with Form Information
        1. Working with Select Boxes
          1. Selecting an option with JavaScript
        2. Working with Check Boxes
        3. Working with Radio Buttons
      4. Prevalidating Form Data
        1. Hacking JavaScript Validation
        2. Validating a Text Field
      5. Exercises
    6. 15. JavaScript and CSS
      1. What Is CSS?
        1. Using Properties and Selectors
        2. Applying CSS
      2. The Relationship Between JavaScript and CSS
        1. Setting Element Styles by ID
          1. Using CSS and JavaScript for form validation
        2. Setting Element Styles by Type
        3. Setting CSS Classes with JavaScript
        4. Retrieving Element Styles with JavaScript
        5. Modifying Style Sheets with JavaScript
      3. Exercises
    7. 16. JavaScript Error Handling
      1. Introducing Two Ways to Handle Errors
      2. Using try/catch
        1. Using try/catch with a web form
        2. And Finally...
      3. Using the onerror Event
        1. Attaching onerror to the window Object
        2. Ignoring Errors
        3. Attaching onerror to the image Object
      4. Exercises
  10. IV. AJAX and Server-Side Integration
    1. 17. JavaScript and XML
      1. Using XML with JavaScript
        1. Looking at an Example XML Document
        2. Loading an XML Document with JavaScript
          1. Importing the Document
          2. Displaying the Document
            1. Adding column headings from an XML document
      2. Working with XML Data from Excel 2007
      3. A Preview of Things to Come
      4. Exercises
    2. 18. JavaScript Applications
      1. Components of JavaScript Applications
        1. The Big Three: Display, Behavior, Data
          1. Display: Page Layout
          2. Behaviors: Controlling What Happens When
          3. Data: Consume, Display, and Validate
      2. JavaScript and Web Interfaces
    3. 19. A Touch of AJAX
      1. Introduction to AJAX
      2. The XMLHttpRequest Object
        1. Instantiating the XMLHttpRequest Object
        2. Sending an AJAX Request
        3. Processing an AJAX Response
          1. Sending and receiving with XMLHttpRequest
        4. Processing XML Responses
        5. Working with JSON
        6. Processing Headers
        7. Using the POST Method
      3. Case Study: Live Searching and Updating
      4. Exercises
    4. 20. A Bit Deeper into AJAX
      1. Creating an HTML Table with XML and CSS
        1. Using XMLHttpRequest to retrieve and display XML data
      2. Styling the Table with CSS
        1. Changing Style Attributes with JavaScript
      3. Creating a Dynamic Drop-Down Box
      4. Accepting Input from the User and AJAX
      5. Exercises
  11. V. jQuery
    1. 21. An Introduction to JavaScript Libraries and Frameworks
      1. Understanding Programming Libraries
      2. Defining Your Own JavaScript Library
        1. Creating a library
      3. Looking at Popular JavaScript Libraries and Frameworks
        1. jQuery
        2. Yahoo! User Interface
        3. MooTools
        4. Other Libraries
      4. Exercises
    2. 22. An Introduction to jQuery
      1. jQuery Primer
      2. Using jQuery
        1. The Two jQuery Downloads
        2. Including jQuery
        3. Basic jQuery Syntax
        4. Connecting jQuery to the Load Event
          1. Using Document Ready
      3. Using Selectors
        1. Selecting Elements by ID
        2. Selecting Elements by Class
        3. Selecting Elements by Type
        4. Selecting Elements by Hierarchy
        5. Selecting Elements by Position
        6. Selecting Elements by Attribute
        7. Selecting Form Elements
        8. More Selectors
      4. Functions
        1. Traversing the DOM
        2. Working with Attributes
        3. Changing Text and HTML
        4. Inserting Elements
        5. Callback Functions
      5. Events
        1. Binding and Unbinding
        2. Mouse Events and Hover
        3. Many More Event Handlers
      6. AJAX and jQuery
        1. Using AJAX with jQuery
        2. AJAX Errors and Timeouts
        3. Sending Data to the Server
        4. Other Important Options
      7. More jQuery
      8. Exercises
    3. 23. jQuery Effects and Plug-Ins
      1. Core Features for Enhancing Usability
        1. Native Effects
          1. Show, Hide, and Toggle
            1. Adding a duration
          2. Fade In and Fade Out
          3. Sliding
      2. jQuery UI
        1. Using jQuery UI
        2. Drag and Drop
        3. Accordion
          1. Setting default state for an accordion
        4. More jQuery UI
      3. Exercises
  12. A. Answer Key to Exercises
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18
    19. Chapter 19
    20. Chapter 20
    21. Chapter 21
    22. Chapter 22
    23. Chapter 23
  13. B. About the Author
  14. Index
  15. About the Author
  16. Copyright