You are previewing JavaScript® 24-Hour Trainer.
O'Reilly logo
JavaScript® 24-Hour Trainer

Book Description

A unique book-and-Video package offers a new level of learning JavaScript

JavaScript has matured from making Web pages dynamic to making them interactive, providing users with a rich and memorable Web experience. This unique book-and-Video package prepares you for the new generation of Web browser changes that are occurring on the ever-evolving Web and shows you how JavaScript is an essential component of those changes. Veteran author Jeremy McPeak provides you with helpful lessons in the text, and the Video offers instructional demonstrations so you can see how JavaScript works in the real world.

  • Begins with the fundamentals of programming with the JavaScript language

  • Builds on basic concepts and applies them to creating Web applications and programming the browser with JavaScript

  • Offers invaluable advice for avoiding common mistakes, debugging code, and handling errors

  • Includes enhanced coverage of each lesson on the Video, which also offers detailed examples

  • Explores JavaScript best practices

With the book-and-Video package of JavaScript 24-Hour Trainer, you'll benefit from a total learning experience!!

Table of Contents

  1. Copyright
  2. CREDITS
  3. ABOUT THE AUTHOR
  4. ABOUT THE TECHNICAL EDITOR
  5. ACKNOWLEDGMENTS
  6. PREFACE
    1. WHOM THIS BOOK IS FOR
    2. WHAT THIS BOOK COVERS
    3. HOW THIS BOOK IS STRUCTURED
    4. INSTRUCTIONAL VIDEOS ON DVD
    5. CONVENTIONS
    6. SUPPORTING PACKAGES AND CODE
    7. ERRATA
    8. P2P.WROX.COM
  7. I. INTRODUCTION TO JAVASCRIPT
    1. 1. Adding JavaScript to a Web Page
      1. 1.1. THE <SCRIPT/> ELEMENT
        1. 1.1.1. Tag Placement
      2. 1.2. TRY IT
        1. 1.2.1. Lesson Requirements
        2. 1.2.2. Step-by-Step
    2. 2. Variables and JavaScript Syntax
      1. 2.1. VARIABLES
        1. 2.1.1. Primitive Data Types in JavaScript
          1. 2.1.1.1. Numbers
            1. 2.1.1.1.1. Using Arithmetic Operators
            2. 2.1.1.1.2. When a Number Is Not a Number
          2. 2.1.1.2. Strings
            1. 2.1.1.2.1. Modifying Strings
            2. 2.1.1.2.2. Converting Numbers to Strings
            3. 2.1.1.2.3. Converting Strings to Numbers
          3. 2.1.1.3. Boolean
            1. 2.1.1.3.1. Truthy and Falsy Values
          4. 2.1.1.4. Undefined and Null
        2. 2.1.2. Determining a Variable's Data Type
      2. 2.2. KEYWORDS AND RESERVED WORDS
      3. 2.3. TRY IT
        1. 2.3.1. Lesson Requirements
        2. 2.3.2. Step-by-Step
    3. 3. Functions
      1. 3.1. FUNCTION DECLARATIONS
      2. 3.2. FUNCTIONS AS VALUES
      3. 3.3. PASSING FUNCTIONS TO OTHER FUNCTIONS
      4. 3.4. RETURNING FUNCTIONS
      5. 3.5. TRY IT
        1. 3.5.1. Lesson Requirements
        2. 3.5.2. Step-by-Step
    4. 4. Making Decisions
      1. 4.1. CONDITIONAL STATEMENTS
        1. 4.1.1. Comparison Operators
          1. 4.1.1.1. Operator Precedence
        2. 4.1.2. Logical Operators
          1. 4.1.2.1. Logical AND
          2. 4.1.2.2. Logical OR
          3. 4.1.2.3. Logical NOT
        3. 4.1.3. Assigning the Results of Conditions
      2. 4.2. EXECUTING CODE BASED ON CONDITIONS
        1. 4.2.1. The if Statement
        2. 4.2.2. The switch Statement
        3. 4.2.3. The Ternary Operator
      3. 4.3. TRY IT
        1. 4.3.1. Lesson Requirements
        2. 4.3.2. Step-by-Step
    5. 5. Loops
      1. 5.1. LOOP BASICS
      2. 5.2. THE FOR LOOP
      3. 5.3. THE WHILE LOOP
      4. 5.4. THE DO-WHILE LOOP
      5. 5.5. TRY IT
        1. 5.5.1. Lesson Requirements
        2. 5.5.2. Step-by-Step
    6. 6. Scope
      1. 6.1. GLOBAL SCOPE
      2. 6.2. FUNCTIONAL SCOPE
      3. 6.3. NO BLOCK-LEVEL SCOPES
      4. 6.4. VARIABLE DECLARATIONS
      5. 6.5. IDENTIFIER LOOKUP
      6. 6.6. TRY IT
        1. 6.6.1. Lesson Requirements
        2. 6.6.2. Step-by-Step
    7. 7. Objects and Built-In Types
      1. 7.1. WHAT ARE OBJECTS?
      2. 7.2. USING JAVASCRIPT OBJECTS
      3. 7.3. BUILT-IN DATA TYPES
        1. 7.3.1. Array
          1. 7.3.1.1. Joining Arrays
          2. 7.3.1.2. Converting Arrays to Strings
          3. 7.3.1.3. Sorting Elements
          4. 7.3.1.4. Reversing Elements
        2. 7.3.2. Date
      4. 7.4. NUMBER
        1. 7.4.1. String
          1. 7.4.1.1. Searching for Substrings
          2. 7.4.1.2. Extracting Substrings
          3. 7.4.1.3. Converting to an Array
          4. 7.4.1.4. Replacing Text
      5. 7.5. TRY IT
        1. 7.5.1. Lesson Requirements
        2. 7.5.2. Step-by-Step
    8. 8. Custom Objects
      1. 8.1. THE OBJECT DATA TYPE
        1. 8.1.1. Outputting Objects
        2. 8.1.2. Objects as Input
      2. 8.2. CREATING CUSTOM DATA TYPES
      3. 8.3. TRY IT
        1. 8.3.1. Lesson Requirements
        2. 8.3.2. Step-by-Step
    9. 9. Prototypes and Inheritance (The Function Object)
      1. 9.1. USING THE PROTOTYPE OBJECT
      2. 9.2. INHERITING MEMBERS FROM OTHER OBJECTS
      3. 9.3. TRY IT
        1. 9.3.1. Lesson Requirements
        2. 9.3.2. Step-by-Step
  8. II. Programming the Browser
    1. 10. The window Object
      1. 10.1. THE GLOBAL SCOPE
        1. 10.1.1. Variables and Functions Are Properties and Methods
        2. 10.1.2. The this Variable
      2. 10.2. SHOWING AND USING DIALOG BOXES
        1. 10.2.1. The Alert Dialog Box
        2. 10.2.2. The Prompt Dialog Box
        3. 10.2.3. The Confirm Dialog Box
      3. 10.3. THE LOCATION OBJECT
        1. 10.3.1. Reloading the Page
        2. 10.3.2. Navigating to Another Page
      4. 10.4. TRY IT
        1. 10.4.1. Lesson Requirements
        2. 10.4.2. Step-by-Step
    2. 11. Scripting Windows
      1. 11.1. OPENING NEW WINDOWS
      2. 11.2. MANIPULATING WINDOWS
      3. 11.3. CODING BETWEEN WINDOWS
      4. 11.4. SECURITY
      5. 11.5. TRY IT
        1. 11.5.1. Lesson Requirements
        2. 11.5.2. Step-by-Step
    3. 12. Scripting Frames
      1. 12.1. FRAMES AND THE WINDOW OBJECT
      2. 12.2. TRY IT
        1. 12.2.1. Lesson Requirements
        2. 12.2.2. Step-by-Step
    4. 13. The Document Object Model
      1. 13.1. WHAT IS THE DOM?
      2. 13.2. NAVIGATING THE DOM
      3. 13.3. SELECTING ELEMENT NODES
      4. 13.4. TRY IT
        1. 13.4.1. Lesson Requirements
        2. 13.4.2. Step-by-Step
    5. 14. Adding HTML with JavaScript
      1. 14.1. DOM CREATION METHODS
        1. 14.1.1. Creating Elements
        2. 14.1.2. Adding Elements to the Document
        3. 14.1.3. Creating and Adding Text Nodes
      2. 14.2. USING THE INNERHTML PROPERTY
      3. 14.3. TRY IT
        1. 14.3.1. Lesson Requirements
        2. 14.3.2. Step-by-Step
    6. 15. Modifying Element Style
      1. 15.1. USING THE STYLE PROPERTY
      2. 15.2. USING THE CLASSNAME PROPERTY
      3. 15.3. TRY IT
        1. 15.3.1. Lesson Requirements
        2. 15.3.2. Step-by-Step
    7. 16. Introduction to Events
      1. 16.1. MOUSE EVENTS
        1. 16.1.1. Movement-Based Events
        2. 16.1.2. Button-Based Events
      2. 16.2. KEYBOARD EVENTS
      3. 16.3. BASIC EVENTS
      4. 16.4. TRY IT
    8. 17. Assigning Event Handlers with HTML Attributes
      1. 17.1. WIRING UP EVENT HANDLERS
      2. 17.2. CANCELLING DEFAULT BEHAVIOR
      3. 17.3. TRY IT
        1. 17.3.1. Lesson Requirements
        2. 17.3.2. Step-by-Step
    9. 18. Using Early DOM Event Handlers
      1. 18.1. ASSIGNING EVENT HANDLERS
      2. 18.2. TRY IT
        1. 18.2.1. Lesson Requirements
        2. 18.2.2. Step-by-Step
    10. 19. Using Standard DOM and IE Event Handlers
      1. 19.1. STANDARD EVENT HANDLERS
      2. 19.2. IE EVENT HANDLERS
      3. 19.3. TRY IT
        1. 19.3.1. Lesson Requirements
        2. 19.3.2. Step-by-Step
    11. 20. Writing Cross-Browser Event Handlers
      1. 20.1. WRITING AN EVENT UTILITY OBJECT
      2. 20.2. MODERNIZING YOUR EVENT UTILITY
      3. 20.3. TRY IT
        1. 20.3.1. Lesson Requirements
        2. 20.3.2. Step-by-Step
    12. 21. The Standard Event Object
      1. 21.1. ACCESSING THE EVENT OBJECT
      2. 21.2. EVENT OBJECTS
        1. 21.2.1. Determining the Event That Occurred
        2. 21.2.2. Accessing the Event Target
      3. 21.3. TRY IT
        1. 21.3.1. Lesson Requirements
        2. 21.3.2. Step-by-Step
    13. 22. Internet Explorer's Event Object
      1. 22.1. ACCESSING THE EVENT OBJECT
      2. 22.2. THE EVENT OBJECT
        1. 22.2.1. Determining the Event That Occurred
        2. 22.2.2. Accessing the Event Target
      3. 22.3. TRY IT
        1. 22.3.1. Lesson Requirements
        2. 22.3.2. Step-by-Step
    14. 23. Writing Cross-Browser Event Code
      1. 23.1. ACQUIRING THE EVENT TARGET
      2. 23.2. CANCELING DEFAULT BEHAVIOR REDUX
      3. 23.3. TRY IT
        1. 23.3.1. Lesson Requirements
        2. 23.3.2. Step-by-Step
    15. 24. Dragging and Dropping
      1. 24.1. GETTING THE MOUSE POINTER'S LOCATION
        1. 24.1.1. Client Coordinates
        2. 24.1.2. Element Coordinates
      2. 24.2. EVENTS USED IN DRAG-AND-DROP
      3. 24.3. TRY IT
        1. 24.3.1. Lesson Requirements
        2. 24.3.2. Step-by-Step
    16. 25. Timers and Animating Elements
      1. 25.1. SETTING A DELAY — THE SETTIMEOUT() METHOD
      2. 25.2. SETTING AN INTERVAL — THE SETINTERVAL() METHOD
      3. 25.3. TRY IT
        1. 25.3.1. Lesson Requirements
        2. 25.3.2. Step-by-Step
    17. 26. Forms ― A Primer
      1. 26.1. FORMS AND THE DOM
      2. 26.2. FORM CONTROL ELEMENTS
        1. 26.2.1. The name Property
        2. 26.2.2. The value Property
        3. 26.2.3. The form Property
        4. 26.2.4. The type Property
        5. 26.2.5. The disabled Property
        6. 26.2.6. The focus() and blur() Methods
      3. 26.3. TRY IT
        1. 26.3.1. Lesson Requirements
        2. 26.3.2. Step-by-Step
    18. 27. Scripting Buttons
      1. 27.1. TRY IT
        1. 27.1.1. Lesson Requirements
        2. 27.1.2. Step-by-Step
    19. 28. Scripting Text Elements
      1. 28.1. THE TEXTBOX
      2. 28.2. THE PASSWORD TEXTBOX
      3. 28.3. THE HIDDEN TEXTBOX
      4. 28.4. THE MULTILINE TEXTBOX
      5. 28.5. TRY IT
        1. 28.5.1. Lesson Requirements
        2. 28.5.2. Step-by-Step
    20. 29. Scripting Selection Boxes
      1. 29.1. REMOVING OPTIONS
      2. 29.2. ADDING OPTIONS
      3. 29.3. TRY IT
        1. 29.3.1. Lesson Requirements
        2. 29.3.2. Step-by-Step
    21. 30. Scripting Checkboxes and Radio Buttons
      1. 30.1. SCRIPTING CHECKBOXES
      2. 30.2. SCRIPTING RADIO BUTTONS
      3. 30.3. TRY IT
        1. 30.3.1. Lesson Requirements
        2. 30.3.2. Step-by-Step
    22. 31. An Introduction to Ajax
      1. 31.1. WHAT IS AJAX?
        1. 31.1.1. Google Suggest
        2. 31.1.2. Google Maps
      2. 31.2. REQUIREMENTS FOR AJAX
    23. 32. Using XMLHttpRequest Objects
      1. 32.1. CREATING XHR OBJECTS
        1. 32.1.1. Browsers with Native Support
        2. 32.1.2. Browsers that Support XmlHttp
      2. 32.2. USING XHR OBJECTS
      3. 32.3. DETERMINING THE SUCCESS OF A REQUEST
      4. 32.4. SECURITY
      5. 32.5. TRY IT
        1. 32.5.1. Lesson Requirements
        2. 32.5.2. Step-by-Step
    24. 33. Making Ajax Requests Using GET
      1. 33.1. MAKING GET REQUESTS
      2. 33.2. ASYNCHRONOUS PROGRAMMING
      3. 33.3. TRY IT
        1. 33.3.1. Lesson Requirements
        2. 33.3.2. Step-by-Step
    25. 34. Making Ajax Requests Using POST
      1. 34.1. ASSEMBLING DATA
      2. 34.2. MAKING POST REQUESTS
      3. 34.3. TRY IT
        1. 34.3.1. Lesson Requirements
        2. 34.3.2. Step-by-Step
    26. 35. JSON
      1. 35.1. XML RULES THE WORLD...OR DOES IT?
      2. 35.2. JAVASCRIPT OBJECT NOTATION SAVES THE WORLD
      3. 35.3. CONVERTING TO AND FROM JSON
      4. 35.4. TRY IT
        1. 35.4.1. Lesson Requirements
        2. 35.4.2. Step-by-Step
  9. III. HANDLING ERRORS, DEBUGGING, AND BEST PRACTICES
    1. 36. Common Coding Mistakes
      1. 36.1. UNDEFINED VARIABLES
      2. 36.2. CASE-SENSITIVITY
      3. 36.3. CLOSING BRACES AND PARENTHESES
      4. 36.4. OPERATOR MISUSE
    2. 37. Handling Errors
      1. 37.1. PREVENTING ERRORS
      2. 37.2. THE TRY...CATCH STATEMENT
      3. 37.3. THROWING EXCEPTIONS
      4. 37.4. TRY IT
        1. 37.4.1. Lesson Requirements
        2. 37.4.2. Step-by-Step
    3. 38. Debugging with Firebug
      1. 38.1. GETTING FAMILIAR WITH THE UI
        1. 38.1.1. Setting Breakpoints
        2. 38.1.2. Stepping through Code
      2. 38.2. TRY IT
        1. 38.2.1. Lesson Requirements
        2. 38.2.2. Step-by-Step
    4. 39. Coding Guidelines
      1. 39.1. USE WHITESPACE
      2. 39.2. VARIABLE DECLARATIONS
      3. 39.3. AVOID THE GLOBAL SCOPE
      4. 39.4. USE LITERALS
    5. 40. Separating JavaScript and HTML
      1. 40.1. TRY IT
        1. 40.1.1. Lesson Requirements
        2. 40.1.2. Step-by-Step
    6. 41. Separating JavaScript and CSS
    7. 42. Avoiding Global Scope
      1. 42.1. USING FUNCTIONS
      2. 42.2. EMULATING NAMESPACES
      3. 42.3. TRY IT
        1. 42.3.1. Lesson Requirements
        2. 42.3.2. Step-by-Step
    8. 43. Optimizing Your Code
      1. 43.1. REFACTORING CODE
        1. 43.1.1. Refactoring Duplicate Code
        2. 43.1.2. Reducing Statements
      2. 43.2. OPTIMIZING DOM CODE
        1. 43.2.1. Using Document Fragments
        2. 43.2.2. Using innerHTML
      3. 43.3. USE EVENT DELEGATION
      4. 43.4. TRY IT
        1. 43.4.1. Lesson Requirements
        2. 43.4.2. Step-by-Step
    9. A. What's on the DVD?
      1. A.1. SYSTEM REQUIREMENTS
      2. A.2. USING THE DVD ON A PC
      3. A.3. USING THE DVD ON A MAC
      4. A.4. WHAT'S ON THE DVD
      5. A.5. TROUBLESHOOTING
      6. A.6. CUSTOMER CARE