You are previewing JavaScript: The Missing Manual.
O'Reilly logo
JavaScript: The Missing Manual

Book Description

JavaScript is essential for creating modern, interactive Web sites. But, unlike HTML and CSS, JavaScript is a true programming language with complex rules that are challenging for most Web designers to learn. In JavaScript: The Missing Manual, bestselling author David McFarland teaches you how to use JavaScript in sophisticated ways -- even if you have little or no programming experience. In a clear, entertaining way, the book starts out by teaching you how to build a basic JavaScript program. Then, once you've mastered the structure and terminology, you'll learn how to use advanced JavaScript tools to add useful interactivity to your sites quickly and painlessly, rather than scripting everything from scratch. To jump-start your progress, the book offers several "living examples" -- step-by-step tutorials for building Web site components with JavaScript using raw materials, such as graphics and half-completed Web pages, that you can download from the book's companion Web site. In this book, you will learn:

  • How to get started. The book introduces the building blocks of JavaScript, and general tips on computer programming. Learn to add scripts to a Web page; store and manipulate information; communicate with the browser window; respond to events like mouse clicks and form submissions; and identify and modify HTML.

  • How to build Web Page Features. McFarland provides real-world examples of JavaScript in action. Learn to create pop-up navigation bars, enhance HTML tables, build an interactive photo gallery, and make Web forms more usable. Create interesting user interfaces with tabbed panels, accordion panels and pop-up dialog boxes.

  • How to troubleshoot and debug. The book will teach you how to avoid the ten most common errors new programmers make, and how to find and fix bugs.

  • How to communicate with the Web server. In addition to basic JavaScript, this manual covers Ajax, the approach that made JavaScript glamorous. Learn to use JavaScript to communicate with a server so that your Web pages can receive information without having to reload.

If you want to put JavaScript to work right away without getting tangled up in code, JavaScript: The Missing Manual is the best book available.

Table of Contents

  1. Special Upgrade Offer
  2. A Note Regarding Supplemental Files
  3. The Missing Credits
    1. About the Author
    2. About the Creative Team
    3. Acknowledgements
    4. The Missing Manual Series
  4. Introduction
    1. What Is JavaScript?
      1. A Bit of History
      2. JavaScript Is Everywhere
      3. JavaScript Doesn’t Stand Alone
    2. HTML: The Barebones Structure
      1. How HTML Tags Work
    3. CSS: Adding Style to Web Pages
      1. Anatomy of a Style
    4. Software for JavaScript Programming
      1. Free Programs
      2. Commercial Software
    5. About This Book
      1. This Book’s Approach to JavaScript
      2. About the Outline
      3. Living Examples
      4. About MissingManuals.com
    6. The Very Basics
      1. About → These → Arrows
      2. Safari® Books Online
  5. I. Getting Started with JavaScript
    1. 1. Writing Your First JavaScript Program
      1. Introducing Programming
        1. What’s a Computer Program?
      2. How to Add JavaScript to a Page
        1. External JavaScript Files
      3. Your First JavaScript Program
      4. Writing Text on a Web Page
      5. Attaching an External JavaScript File
      6. Tracking Down Errors
        1. The Firefox JavaScript Console
        2. Displaying the Internet Explorer Error Dialog Box
        3. Accessing the Safari Error Console
    2. 2. The Grammar of JavaScript
      1. Statements
      2. Commands
      3. Types of Data
        1. Numbers
        2. Strings
        3. Booleans
      4. Variables
        1. Creating a Variable
        2. Using Variables
      5. Working with Data Types and Variables
        1. Basic Math
        2. The Order of Operations
        3. Combining Strings
        4. Combining Numbers and Strings
        5. Changing the Values in Variables
      6. Tutorial: Using Variables to Create Messages
      7. Tutorial: Asking for Information
      8. Arrays
        1. Creating an Array
        2. Accessing Items in an Array
        3. Adding Items to an Array
          1. Adding an item to the end of an array
          2. Adding an item to the beginning of an array
          3. Choosing how to add items to an array
        4. Deleting Items from an Array
        5. Adding and Deleting with splice()
          1. Deleting items with splice()
          2. Adding items with splice()
          3. Replacing items with splice()
      9. Tutorial: Writing to a Web Page Using Arrays
      10. Comments
        1. When to Use Comments
        2. Comments in this Book
    3. 3. Adding Logic and Control to Your Programs
      1. Making Programs React Intelligently
        1. Conditional Statement Basics
        2. Adding a Backup Plan
        3. Testing More Than One Condition
        4. More Complex Conditions
          1. Making sure more than one condition is true
          2. Making sure at least one condition is true
          3. Negating a condition
        5. Nesting Conditional Statements
        6. Tips for Writing Conditional Statements
      2. Tutorial: Using Conditional Statements
      3. Handling Repetitive Tasks with Loops
        1. While Loops
        2. Loops and Arrays
        3. For Loops
        4. Do/While Loops
      4. Functions: Turn Useful Code Into Reusable Commands
        1. Mini-Tutorial
        2. Giving Information to Your Functions
        3. Retrieving Information from Functions
        4. Keeping Variables from Colliding
      5. Tutorial: A Simple Quiz
    4. 4. Working with Words, Numbers, and Dates
      1. A Quick Object Lesson
      2. Strings
        1. Determining the Length of a String
        2. Changing the Case of a String
        3. Searching a String: indexOf() Technique
        4. Extracting Part of a String with slice()
      3. Finding Patterns in Strings
        1. Creating and Using a Basic Regular Expression
        2. Building a Regular Expression
        3. Grouping Parts of a Pattern
        4. Useful Regular Expressions
          1. U.S. Zip code
          2. U.S. phone number
          3. Email address
          4. Date
          5. Web address
        5. Matching a Pattern
          1. Matching every instance of a pattern
        6. Replacing Text
        7. Trying Out Regular Expressions
      4. Numbers
        1. Changing a String to a Number
        2. Testing for Numbers
        3. Rounding Numbers
        4. Formatting Currency Values
        5. Creating a Random Number
          1. Randomly selecting an array element
          2. A function for selecting a random number
      5. Dates and Times
        1. Getting the Month
        2. Getting the Day of the Week
        3. Getting the Time
          1. Changing hours to a.m. and p.m.
          2. Padding single digits
        4. Creating a Date Other Than Today
          1. Creating a date that’s one week from today
      6. Tutorial
        1. Overview
        2. Writing the Function
    5. 5. Dynamically Modifying Web Pages
      1. Modifying Web Pages: An Overview
      2. Understanding the Document Object Model
        1. Selecting a Page Element
          1. getElementById()
          2. getElementsByTagName()
          3. Selecting nearby nodes
        2. Adding Content to a Page
        3. The Moon Quiz Revisited
        4. The Problem with the DOM
      3. Introducing JavaScript Libraries
        1. Getting Started with jQuery
      4. Selecting Page Elements (Revisited)
        1. Basic Selectors
          1. ID selectors
          2. Element selectors
          3. Class selectors
        2. Advanced Selectors
        3. jQuery Filters
        4. Understanding jQuery Selections
          1. Automatic loops
          2. Chaining functions
      5. Adding Content to a Page
        1. Replacing and Removing Selections
      6. Setting and Reading Tag Attributes
        1. Classes
        2. Reading and Changing CSS Properties
        3. Changing Multiple CSS Properties at Once
      7. Reading, Setting, and Removing HTML Attributes
      8. Creative Headlines
      9. Acting on Each Element in a Selection
        1. Anonymous Functions
        2. this and $(this)
      10. Automatic Pull Quotes
        1. Overview
        2. Programming
    6. 6. Action/Reaction: Making Pages Come Alive with Events
      1. What Are Events?
        1. Mouse Events
        2. Document/Window Events
        3. Form Events
        4. Keyboard Events
      2. Using Events with Functions
        1. Inline Events
        2. The Traditional Model
        3. The Modern Way
        4. The jQuery Way
      3. Tutorial: Highlighting Table Rows
      4. More jQuery Event Concepts
        1. Waiting for the HTML to Load
        2. jQuery Events
          1. The hover() event
          2. The toggle() Event
        3. The Event Object
        4. Stopping an Event’s Normal Behavior
        5. Removing Events
      5. Advanced Event Management
      6. Tutorial: A One-Page FAQ
        1. Overview of the Task
        2. The Programming
    7. 7. Improving Your Images
      1. Swapping Images
        1. Changing an Image’s src Attribute
        2. Preloading Images
        3. Rollover Images
      2. Tutorial: Adding Rollover Images
        1. Overview of the Task
        2. The Programming
      3. jQuery Effects
        1. Basic Showing and Hiding
        2. Fading Elements In and Out
        3. Sliding Elements
        4. Animation
      4. Tutorial: Photo Gallery with Effects
        1. Overview of Task
        2. The Programming
      5. Advanced Gallery with jQuery lightBox
        1. The Basics
        2. Customizing lightBox
          1. lightBox options
          2. lightBox images
          3. lightBox CSS
      6. Tutorial: lightBox Photo Gallery
      7. Animated Slideshows with Cycle
        1. The Basics
        2. Customizing the Cycle Plug-in
          1. Effects
          2. Speed
          3. Navigating slides
          4. Starting and stopping the slideshow
      8. Tutorial: An Automated Slideshow
  6. II. Building Web Page Features
    1. 8. Improving Navigation
      1. Some Link Basics
        1. Selecting Links with JavaScript
        2. Determining a Link’s Destination
        3. Don’t Follow That Link
      2. Opening External Links in a New Window
      3. Creating New Windows
        1. Window Properties
          1. Use the window reference
          2. Events that can open a new window
      4. Opening Pages in a Window on the Page
        1. Customizing the Look of a Greybox Window
        2. Tutorial: Opening a Page Within a Page
      5. Tutorial: Making Bigger Links
        1. Overview
        2. The Programming
      6. Animated Navigation Menus
        1. The HTML
        2. The CSS
        3. The JavaScript
        4. The Tutorial
    2. 9. Enhancing Web Forms
      1. Understanding Forms
        1. Selecting Form Elements
        2. Getting and Setting the Value of a Form Element
        3. Determine Whether Buttons and Boxes Are Checked
        4. Form Events
          1. Submit
          2. Focus
          3. Blur
          4. Click
          5. Change
      2. Adding Smarts to Your Forms
        1. Focus the First Field in a Form
        2. Disabling and Enabling Fields
        3. Hiding and Showing Form Options
      3. Tutorial: Basic Form Enhancements
        1. Focusing a Field
        2. Disabling Form Fields
        3. Hiding Form Fields
      4. Form Validation
        1. jQuery Validation Plug-in
        2. Basic Validation
          1. Adding validation rules
          2. Adding error messages
        3. Advanced Validation
          1. Advanced rules
          2. Advanced error messages
        4. Styling Error Messages
      5. Validation Tutorial
        1. Basic Validation
        2. Advanced Validation
        3. Validating Checkboxes and Radio Buttons
        4. Formatting the Error Messages
    3. 10. Expanding Your Interface
      1. Hiding Information with Accordion Panels
        1. Customizing an Accordion
        2. Accordion Tutorial
      2. Organizing Information in Tabbed Panels
        1. Formatting Tabs and Panels
          1. A required class style
          2. The tab group
          3. Tabs
          4. Panels
        2. Customizing the Tabs Plug-in
          1. Selecting a tab when the page loads
          2. Using a different event to open a panel
          3. Automating the display of panels
        3. Tabbed Panels Tutorial
      3. Tooltips
        1. Tooltips Using the Title Attribute
        2. Tooltips Using Another Web Page
        3. Tooltips Using Hidden Content
        4. Controlling the Display of Tooltips
        5. Formatting Tooltips
        6. Cluetip Tutorial
          1. Adding a tooltip using the title attribute
          2. Adding a tooltip using another Web page
          3. Adding a tooltip using HTML on the page
      4. Creating Sortable Tables
        1. Styling the Table
          1. Using the Tablesorter plug-in to stripe tables
        2. Tablesorter Tutorial
  7. III. Ajax: Communicating with the Web Server
    1. 11. Introducing Ajax
      1. What Is Ajax?
      2. Ajax: The Basics
        1. Pieces of the Puzzle
        2. Talking to the Web Server
      3. Ajax the jQuery Way
        1. Using the load() Function
        2. Tutorial: The load() Function
          1. Overview
          2. The programming
        3. The get() and post() Functions
        4. Formatting Data to Send to the Server
          1. Query string
          2. Object literal
          3. jQuery’s serialize() function
        5. Processing Data from the Server
        6. Tutorial: Using the post() Function
          1. Overview
          2. The programming
      4. JSON
        1. Accessing JSON Data
        2. Complex JSON Objects
    2. 12. Basic Ajax Programming
      1. Tabs Plug-in
        1. Changing the Loading Text and Icon
          1. Turning off the “Loading” message
        2. Ajax Tabs Tutorial
      2. Adding Google Maps to Your Site
        1. Setting a Location for the Map
        2. Other jMap Options
        3. Adding Markers and HTML Bubbles
        4. Get Driving Directions
        5. jMaps Tutorial
  8. IV. Troubleshooting, Tips, and Tricks
    1. 13. Troubleshooting and Debugging
      1. Top JavaScript Programming Mistakes
        1. Non-Closed Pairs
        2. Quotation Marks
        3. Using Reserved Words
        4. Single Equals in Conditional Statements
        5. Case-Sensitivity
        6. Incorrect Path to External JavaScript File
        7. Incorrect Paths Within External JavaScript Files
        8. Disappearing Variables and Functions
      2. Debugging with Firebug
        1. Installing and Turning On Firebug
        2. Viewing Errors with Firebug
        3. Using console.log() to Track Script Progress
        4. Tutorial: Using the Firebug Console
        5. More Powerful Debugging
          1. Controlling your script with the debugger
          2. Watching your script
      3. Debugging Tutorial
    2. 14. Going Further with JavaScript
      1. Putting It All Together
        1. Using External JavaScript Files
      2. Writing More Efficient JavaScript
        1. Put Preferences in Variables
        2. Ternary Operator
        3. The Switch Statement
        4. Using the jQuery Object Efficiently
      3. Creating Fast-Loading JavaScript
        1. Using YUI Compressor for Windows
        2. Using YUI Compressor for Mac
  9. V. Appendix
    1. A. JavaScript Resources
      1. References
        1. Web Sites
        2. Books
      2. Basic JavaScript
        1. Articles and Presentations
        2. Web Sites
        3. Books
      3. jQuery
        1. Articles
        2. Web Sites
        3. Books
      4. The Document Object Model
        1. Articles and Presentations
        2. Web Sites
        3. Books
      5. Ajax
        1. Web Sites
        2. Books
      6. Advanced JavaScript
        1. Articles and Presentations
        2. Web Sites
        3. Books
      7. CSS
        1. Web Sites
        2. Books
      8. JavaScript Software
  10. Index
  11. About the Author
  12. Colophon
  13. Special Upgrade Offer
  14. Copyright