JavaScript & JQuery Java Script And J Query The Missing Manual 3rd Edition[A4]

JavaScript%20%26%20jQuery-%20The%20Missing%20Manual%2C%203rd%20Edition

Javascript%20and%20jQuery%20-%20The%20missing%20manual

JavaScript%20%26%20jQuery%20(The%20Missing%20Manual%20-%203rd%20Edition)%20-%20David%20Sawyer%20McFarland%20-%20O'Reilly

JavaScript%20%26%20jQuery-%20The%20Missing%20Manual%2C%203rd%20Edition

JavaScript%20%26%20jQuery-%20The%20Missing%20Manual%2C%203rd%20Edition

User Manual: manual pdf -FilePursuit

Open the PDF directly: View PDF PDF.
Page Count: 686

DownloadJavaScript & JQuery Java Script And J Query The Missing Manual 3rd Edition[A4]
Open PDF In BrowserView PDF
3rd Edition
“The Missing Manual series is simply the most intelligent and usable series of guidebooks…”

Answers found here!

The important stuff you need to know
n

 ake your pages come alive. Use jQuery to create interactive
M
elements that respond to visitor input.

n

 et acquainted with jQuery UI. Expand your interface with
G
tabbed panels, dialog boxes, date pickers, and other widgets.

n

 isplay good forms. Get information from visitors, help
D
shoppers buy goods, and let members post their thoughts.

n

 o beyond the browser with Ajax. Communicate with the
G
web server to update your pages without reloading.

n

 ut your new skills right to work. Create a simple application
P
step-by-step, using jQuery and jQuery UI widgets.

n

 ive into advanced concepts. Use ThemeRoller to customize
D
your widgets; avoid common errors that new programmers
often make.

David Sawyer McFarland,
president of Sawyer
McFarland Media, Inc.,
has spent nearly 20 years
building and managing
websites. Having served
as webmaster at UC
Berkeley, he’s also taught
at the UC Berkeley
Graduate School of
Journalism and the
Portland State University
multimedia program.
David lives in Portland
and has written
bestselling Missing Manual
titles on Adobe
Dreamweaver and CSS

JavaScript
& jQuery
n
3rd Editio
Covers
jQuery UI

McFarland

Web Authoring and Design

US $49.99

JavaScript & jQuery

JavaScript lets you supercharge your web pages with
animation, interactivity, and visual effects, but learning the
language isn’t easy. This fully updated and expanded guide
takes you step-by-step through JavaScript basics, then
shows you how to save time and effort with jQuery—the
library of prewritten JavaScript code—and the newest
innovations from the jQuery UI plug-in.

—KEVIN KELLY, CO-FOUNDER OF WIRED

CAN $52.99

ISBN: 978-1-491-94707-4
missingmanuals.com
twitter: @missingmanuals
facebook.com/MissingManuals

David Sawyer McFarland

3rd Edition
“The Missing Manual series is simply the most intelligent and usable series of guidebooks…”

Answers found here!

The important stuff you need to know
n

 ake your pages come alive. Use jQuery to create interactive
M
elements that respond to visitor input.

n

 et acquainted with jQuery UI. Expand your interface with
G
tabbed panels, dialog boxes, date pickers, and other widgets.

n

 isplay good forms. Get information from visitors, help
D
shoppers buy goods, and let members post their thoughts.

n

 o beyond the browser with Ajax. Communicate with the
G
web server to update your pages without reloading.

n

 ut your new skills right to work. Create a simple application
P
step-by-step, using jQuery and jQuery UI widgets.

n

 ive into advanced concepts. Use ThemeRoller to customize
D
your widgets; avoid common errors that new programmers
often make.

David Sawyer McFarland,
president of Sawyer
McFarland Media, Inc.,
has spent nearly 20 years
building and managing
websites. Having served
as webmaster at UC
Berkeley, he’s also taught
at the UC Berkeley
Graduate School of
Journalism and the
Portland State University
multimedia program.
David lives in Portland
and has written
bestselling Missing Manual
titles on Adobe
Dreamweaver and CSS

JavaScript
& jQuery
n
3rd Editio
Covers
jQuery UI

McFarland

Web Authoring and Design

US $49.99

JavaScript & jQuery

JavaScript lets you supercharge your web pages with
animation, interactivity, and visual effects, but learning the
language isn’t easy. This fully updated and expanded guide
takes you step-by-step through JavaScript basics, then
shows you how to save time and effort with jQuery—the
library of prewritten JavaScript code—and the newest
innovations from the jQuery UI plug-in.

—KEVIN KELLY, CO-FOUNDER OF WIRED

CAN $52.99

ISBN: 978-1-491-94707-4
missingmanuals.com
twitter: @missingmanuals
facebook.com/MissingManuals

David Sawyer McFarland

JavaScript
& jQuery
The book that should have been in the box®

David Sawyer McFarland

Beijing | Cambridge | Farnham | Köln | Sebastopol | Tokyo

JavaScript & jQuery: The Missing Manual
by David Sawyer McFarland

Copyright © 2014 Sawyer McFarland Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc.,
1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://safaribooksonline.com). For
more information, contact our corporate/institutional sales department: (800) 9989938 or corporate@oreilly.com.
July 2008:
First Edition.
October 2011:
Second Edition.
September 2014: Third Edition.
Revision History for the Third Edition:
2014-09-10

First release

See http://oreilly.com/catalog/errata.csp?isbn=9781491947074 for release details.

The Missing Manual is a registered trademark of O’Reilly Media, Inc. The Missing
Manual logo, and “The book that should have been in the box” are trademarks of
O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to
distinguish their products are claimed as trademarks. Where those designations
appear in this book, and O’Reilly Media is aware of a trademark claim, the
designations are capitalized.
While every precaution has been taken in the preparation of this book, the publisher
assumes no responsibility for errors or omissions, or for damages resulting from the
use of the information contained in it.

ISBN-13: 978-1-491-94707-4
[LSI]

Contents
The Missing Credits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
What Is JavaScript?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
What Is jQuery? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  xv
HTML: The Barebones Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
CSS: Adding Style to Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Software for JavaScript Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  xxii
About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
The Very Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
About the Online Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  xxvii

Part One:
CHAPTER 1:

Getting Started with JavaScript
Writing Your First JavaScript Program. . . . . . . . . . . . . . . . . . . . . . 3
Introducing Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4
How to Add JavaScript to a Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6
Your First JavaScript Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Writing Text on a Web Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Attaching an External JavaScript File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Tracking Down Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

CHAPTER 2:

The Grammar of JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  25
Built-In Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  26
Types of Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  27
Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Working with Data Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  33
Tutorial: Using Variables to Create Messages. . . . . . . . . . . . . . . . . . . . . . . . . . 40
Tutorial: Asking for Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  42
Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Tutorial: Writing to a Web Page Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 51
A Quick Object Lesson . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  55
Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

iii

CHAPTER 3:

Adding Logic and Control to Your Programs. . . . . . . . . . . . . . . 61
Making Programs React Intelligently. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Tutorial: Using Conditional Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  74
Handling Repetitive Tasks with Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  78
Functions: Turn Useful Code Into Reusable Commands. . . . . . . . . . . . . . . . . 85
Tutorial: A Simple Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Part Two:

Getting Started with jQuery

CHAPTER 4:

Introducing jQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
About JavaScript Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  105
Getting jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  107
Adding jQuery to a Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Modifying Web Pages: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Understanding the Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Selecting Page Elements: The jQuery Way. . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Adding Content to a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Setting and Reading Tag Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Reading, Setting, and Removing HTML Attributes . . . . . . . . . . . . . . . . . . . . . 137
Acting on Each Element in a Selection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Automatic Pull Quotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

CHAPTER 5:

Action/Reaction: Making Pages Come Alive with Events.. 147
What Are Events?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Using Events the jQuery Way. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Tutorial: Introducing Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
More jQuery Event Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  160
Advanced Event Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Tutorial: A One-Page FAQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

CHAPTER 6:

Animations and Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
jQuery Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  183
Tutorial: Login Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  188
Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Performing an Action After an Effect Is Completed. . . . . . . . . . . . . . . . . . .  194
Tutorial: Animated Dashboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
jQuery and CSS3 Transitions and Animations . . . . . . . . . . . . . . . . . . . . . . . . 202

CHAPTER 7:

Common jQuery Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Swapping Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Tutorial: Adding Rollover Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Tutorial: Photo Gallery with Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Controlling How Links Behave. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Opening External Links in a New Window . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

iv

Contents

Creating New Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Introducing jQuery Plug-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Build a Responsive Navigation Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
CHAPTER 8:

Enhancing Web Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Understanding Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Adding Smarts to Your Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Tutorial: Basic Form Enhancements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Form Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  273
Validation Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

Part Three:
CHAPTER 9:

Getting Started with jQuery UI
Expanding Your Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
What Is jQuery UI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Why Use jQuery UI?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Using jQuery UI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Adding Messages with Dialog Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Providing Information with Tooltips. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Adding Tabbed Panels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Saving Space with Accordions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Adding Menus to a Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

CHAPTER 10:

Forms Revisited.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Picking Dates with Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Stylish Select Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Styling Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Improve Radio Buttons and Checkboxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Providing Hints with Autocomplete. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
jQuery UI Form Widget Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

CHAPTER 11:

Customizing the Look of jQuery UI.. . . . . . . . . . . . . . . . . . . . . . . 385
Introducing ThemeRoller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Downloading and Using Your New Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Overriding jQuery UI Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392

CHAPTER 12:

jQuery UI Interactions and Effects. . . . . . . . . . . . . . . . . . . . . . . . 399
The Draggable Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
The Droppable Widget. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Drag-and-Drop Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Sorting Page Items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
jQuery UI Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .438

Contents

v

Part Four:

Advanced jQuery and JavaScript

CHAPTER 13:

Introducing Ajax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
What Is Ajax?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Ajax: The Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Ajax the jQuery Way. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
JSON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Introducing JSONP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Adding a Flickr Feed to Your Site. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Tutorial: Adding Flickr Images to Your Site. . . . . . . . . . . . . . . . . . . . . . . . . . . 488

CHAPTER 14:

Building a To-Do List Application. . . . . . . . . . . . . . . . . . . . . . . . . 495
An Overview of the Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495
Add a Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Add a Dialog Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Adding Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Marking Tasks as Complete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Deleting Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

Part Five:

Tips, Tricks, and Troubleshooting

CHAPTER 15:

Getting the Most from jQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Useful jQuery Tips and Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Using the jQuery Docs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Traversing the DOM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
More Functions for Manipulating HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

CHAPTER 16:

Going Further with JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  541
Finding Patterns in Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Working with Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Dates and Times. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Writing More Efficient JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  575
Putting It All Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582

CHAPTER 17:

Troubleshooting and Debugging. . . . . . . . . . . . . . . . . . . . . . . . . 587
Top JavaScript Programming Mistakes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Debugging with the Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Debugging Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609

vi

Contents

Part Six:
APPENDIX A:

Appendix
JavaScript Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  619
Basic JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
jQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Advanced JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623

Contents

vii

The Missing Credits
ABOUT THE AUTHOR
David Sawyer McFarland is president of Sawyer McFarland Media, Inc.,
a web development and training company in Portland, Oregon. He's
been building websites since 1995, when he designed his first site—an
online magazine for communication professionals. He's served as webmaster at the University of California at Berkeley and the Berkeley
Multimedia Research Center, and oversaw a complete CSS-driven redesign of Macworld.com.
In addition to building websites, David is also a writer, trainer, and instructor. He's
taught web design at UC Berkeley Graduate School of Journalism, the Center for
Electronic Art, the Academy of Art College, Ex'Pressions Center for New Media,
and Portland State University. He's written articles about the web for Practical Web
Design, MX Developer's Journal, Macworld magazine, and CreativePro.com.
He welcomes feedback about this book by email: missing@sawmac.com. (If you're
seeking technical help, however, please refer to the sources listed in Appendix A.)

ABOUT THE CREATIVE TEAM
Nan Barber (editor) is associate editor for the Missing Manual series. She lives in
Massachusetts with her husband and various electronic devices. Email: nanbarber@
gmail.com.
Melanie Yarbrough (production editor) works and plays in Cambridge, Massachusetts, where she bakes up whatever she can imagine and bikes around the city.
Email: myarbrough@oreilly.com.
Jennifer Davis (technical reviewer) is an engineer with years of experience improving platform development efficiency. As a Chef Automation engineer, she helps
companies discover their own best practices to improving workflow reducing mean
time to deploy. She is an event organizer for Reliability Engineering, the Bay Area
Chef user group.
Alex Stangl (technical reviewer) has developed software professionally for 25+ years,
using a myriad of languages and technologies. He enjoys challenging problems and
puzzles, learning new languages (currently Clojure), doing technical reviews, and
being a good dad and husband. Email: alex@stangl.us.
Jasmine Kwityn (proofreader) is a freelance copyeditor and proofreader. She lives
in New Jersey with her husband, Ed, and their three cats, Mushki, Axle, and Punky.
Email: jasminekwityn@gmail.com.

THE MISSING CREDITS

ix

Bob Pfahler (indexer) is a freelance indexer who indexed this book on behalf of
Potomac Indexing, LLC, an international indexing partnership at www.potomacindexing.com. Besides the subject of computer technology, he specializes in business,
management, biography, and history. Email: bobpfahler@hotmail.com.

ACKNOWLEDGMENTS
Many thanks to all those who helped with this book, including Jennifer Davis and
Alex Stangl, whose watchful eyes saved me from potentially embarrassing mistakes.
Thanks also to my many students at Portland State University who have sat through
my long JavaScript lectures and struggled through my programming assignments—
especially the members of Team Futzbit (Combination Pizza Hut and Taco Bell)
for testing the tutorials: Julia Hall, Amber Brucker, Kevin Brown, Josh Elliott, Tracy
O'Connor, and Blake Womack. Also, we all owe a big debt of gratitude to John Resig
and the jQuery team for creating the best tool yet for making JavaScript fun.
Finally, thanks to David Pogue for getting me started; Nan Barber for making my
writing sharper and clearer; my wife, Scholle, for putting up with an author's crankiness; and thanks to my kids, Graham and Kate, because they're just awesome.

—David Sawyer McFarland

THE MISSING MANUAL SERIES
Missing Manuals are witty, superbly written guides to computer products that don't
come with printed manuals (which is just about all of them). Each book features a
handcrafted index and cross-references to specific pages (not just chapters). Recent
and upcoming titles include:

Access 2010: The Missing Manual by Matthew MacDonald
Access 2013: The Missing Manual by Matthew MacDonald
Adobe Edge Animate: The Missing Manual by Chris Grover
Buying a Home: The Missing Manual by Nancy Conner
Creating a Website: The Missing Manual, Third Edition by Matthew MacDonald
CSS3: The Missing Manual, Third Edition by David Sawyer McFarland
David Pogue's Digital Photography: The Missing Manual by David Pogue
Dreamweaver CS6: The Missing Manual by David Sawyer McFarland
Dreamweaver CC: The Missing Manual by David Sawyer McFarland and Chris Grover
Excel 2010: The Missing Manual by Matthew MacDonald
Excel 2013: The Missing Manual by Matthew MacDonald
Facebook: The Missing Manual, Third Edition by E. A. Vander Veer
FileMaker Pro 13: The Missing Manual by Susan Prosser and Stuart Gripman

x

THE MISSING CREDITS

Flash CS6: The Missing Manual by Chris Grover
Galaxy Tab: The Missing Manual by Preston Gralla
Galaxy S4: The Missing Manual by Preston Gralla
Galaxy S5: The Missing Manual by Preston Gralla
Google+: The Missing Manual by Kevin Purdy
HTML5: The Missing Manual, Second Edition by Matthew MacDonald
iMovie '11 & iDVD: The Missing Manual by David Pogue and Aaron Miller
iPad: The Missing Manual, Sixth Edition by J.D. Biersdorfer
iPhone: The Missing Manual, Seventh Edition by David Pogue
iPhone App Development: The Missing Manual by Craig Hockenberry
iPhoto '11: The Missing Manual by David Pogue and Lesa Snider
iPod: The Missing Manual, Eleventh Edition by J.D. Biersdorfer and David Pogue
Kindle Fire HD: The Missing Manual by Peter Meyers
Living Green: The Missing Manual by Nancy Conner
Microsoft Project 2010: The Missing Manual by Bonnie Biafore
Microsoft Project 2013: The Missing Manual by Bonnie Biafore
Motorola Xoom: The Missing Manual by Preston Gralla
NOOK HD: The Missing Manual by Preston Gralla
Office 2010: The Missing Manual by Nancy Conner and Matthew MacDonald
Office 2011 for Macintosh: The Missing Manual by Chris Grover
Office 2013: The Missing Manual by Nancy Conner and Matthew MacDonald
OS X Mountain Lion: The Missing Manual by David Pogue
OS X Mavericks: The Missing Manual by David Pogue
OS X Yosemite: The Missing Manual by David Pogue
Personal Investing: The Missing Manual by Bonnie Biafore
Photoshop CS6: The Missing Manual by Lesa Snider
Photoshop CC: The Missing Manual by Lesa Snider
Photoshop Elements 12: The Missing Manual by Barbara Brundage
PHP & MySQL: The Missing Manual, Second Edition by Brett McLaughlin
QuickBooks 2014: The Missing Manual by Bonnie Biafore
QuickBooks 2015: The Missing Manual by Bonnie Biafore
THE MISSING CREDITS

xi

Switching to the Mac: The Missing Manual, Mavericks Edition by David Pogue
Switching to the Mac: The Missing Manual, Yosemite Edition by David Pogue
Windows 7: The Missing Manual by David Pogue
Windows 8: The Missing Manual by David Pogue
WordPress: The Missing Manual, Second Edition by Matthew MacDonald
Your Body: The Missing Manual by Matthew MacDonald
Your Brain: The Missing Manual by Matthew MacDonald
Your Money: The Missing Manual by J.D. Roth
For a full list of all Missing Manuals in print, go to www.missingmanuals.com/library.
html.

xii

THE MISSING CREDITS

Introduction

T

he Web was a pretty boring place in its early days. Web pages were constructed
from plain old HTML, so they could display information, and that was about all.
Folks would click a link and then wait for a new web page to load. That was
about as interactive as it got.
These days, most websites are almost as responsive as the programs on a desktop
computer, reacting immediately to every mouse click. And it’s all thanks to the
subjects of this book—JavaScript and its sidekick, jQuery.

What Is JavaScript?
JavaScript is a programming language that lets you supercharge your HTML with
animation, interactivity, and dynamic visual effects.
JavaScript can make web pages more useful by supplying immediate feedback. For
example, a JavaScript-powered shopping cart page can instantly display a total cost,
with tax and shipping, the moment a visitor selects a product to buy. JavaScript can
produce an error message immediately after someone attempts to submit a web
form that’s missing necessary information.
JavaScript also lets you create fun, dynamic, and interactive interfaces. For example,
with JavaScript, you can transform a static page of thumbnail images into an animated slideshow. Or you can do something more subtle like stuff more information
on a page without making it seem crowded by organizing content into bite-size
panels that visitors can access with a simple click of the mouse (page 326). Or add
something useful and attractive, like pop-up tooltips that provide supplemental
information for items on your web page (page 321).
xiii

WHAT IS
JAVASCRIPT?

Another one of JavaScript’s main selling points is its immediacy. It lets web pages
respond instantly to actions like clicking a link, filling out a form, or merely moving
the mouse around the screen. JavaScript doesn’t suffer from the frustrating delay
associated with server-side programming languages like PHP, which rely on communication between the web browser and the web server. Because it doesn’t rely on
constantly loading and reloading web pages, JavaScript lets you create web pages
that feel and act more like desktop programs than web pages.
If you’ve visited Google Maps (http://maps.google.com), you’ve seen JavaScript in
action. Google Maps lets you view a map of your town (or pretty much anywhere
else for that matter), zoom in to get a detailed view of streets and bus stops, or
zoom out to get a bird’s-eye view of how to get across town, the state, or the nation.
While there were plenty of map sites before Google, they always required reloading
multiple web pages (usually a slow process) to get to the information you wanted.
Google Maps, on the other hand, works without page refreshes—it responds immediately to your choices.
The programs you create with JavaScript can range from the really simple (like popping up a new browser window with a web page in it) to full-blown web applications
like Google Docs (http://docs.google.com), which lets you create presentations,
edit documents, and build spreadsheets using your web browser with the feel of a
program running directly on your computer.

A Bit of History
Invented in 10 days by Brendan Eich at Netscape back in 1995, JavaScript is nearly
as old as the Web itself. While JavaScript is well respected today, it has a somewhat
checkered past. It used to be considered a hobbyist’s programming language, used
for adding less-than-useful effects such as messages that scroll across the bottom
of a web browser’s status bar like a stock ticker, or animated butterflies following
mouse movements around the page. In the early days of JavaScript, it was easy to
find thousands of free JavaScript programs (also called scripts) online, but many of
those scripts didn’t work in all web browsers, and at times even crashed browsers.
NOTE

JavaScript has little to do with the Java programming language. JavaScript was originally named
LiveScript, but a quick deal by marketers at Netscape eager to cash in on the success of Sun Microsystem’s then-hot
programming language led to this long-term confusion. Don’t make the mistake of confusing the two…especially
at a job interview!

In the early days, JavaScript also suffered from incompatibilities between the two
prominent browsers, Netscape Navigator and Internet Explorer. Because Netscape
and Microsoft tried to outdo each other’s browsers by adding newer and (ostensibly) better features, the two browsers often acted in very different ways, making it
difficult to create JavaScript programs that worked well in both.
NOTE

After Netscape introduced JavaScript, Microsoft introduced jScript, their own version of JavaScript
included with Internet Explorer.

xiv

JAVASCRIPT & JQUERY: THE MISSING MANUAL

Fortunately, the worst of those days is nearly gone and contemporary browsers like
Firefox, Safari, Chrome, Opera, and Internet Explorer 11 have standardized much of
the way they handle JavaScript, making it easier to write JavaScript programs that
work for most everyone. (There are still a few incompatibilities among current web
browsers, so you’ll need to learn a few tricks for dealing with cross-browser problems.
You’ll learn how to overcome browser incompatibilities in this book.)

WHAT IS
JQUERY?

In the past several years, JavaScript has undergone a rebirth, fueled by high-profile
websites like Google, Yahoo!, and Flickr, which use JavaScript extensively to create
interactive web applications. There’s never been a better time to learn JavaScript.
With the wealth of knowledge and the quality of scripts being written, you can add
sophisticated interaction to your website—even if you’re a beginner.
NOTE

JavaScript is also known by the name ECMAScript. ECMAScript is the “official” JavaScript specification,
which is developed and maintained by an international standards organization called Ecma International: www.
ecmascript.org.

JavaScript Is Everywhere
JavaScript isn’t just for web pages, either. It’s proven to be such a useful programming language that if you learn JavaScript you can create Yahoo! Widgets and
Google Apps, write programs for the iPhone, and tap into the scriptable features
of many Adobe programs like Acrobat, Photoshop, Illustrator, and Dreamweaver.
In fact, Dreamweaver has always offered clever JavaScript programmers a way to
add their own commands to the program.
In the Yosemite version of the Mac OS X operating system, Apple lets users automate
their Macs using JavaScript. In addition, JavaScript is used in many helpful front end
web development tools like Gulp.js (which can automatically compress images and
CSS and JavaScript files) and Bower (which makes it quick and easy to download
common JavaScript libraries like jQuery, jQuery UI, or AngularJS to your computer).
JavaScript is also becoming increasingly popular for server-side development. The
Node.js platform (a version of Google’s V8 JavaScript engine that runs JavaScript
on the server) is being embraced eagerly by companies like Walmart, PayPal, and
eBay. Learning JavaScript can even lead to a career in building complex serverside applications. In fact, the combination of JavaScript on the frontend (that is,
JavaScript running in a web browser) and the backend (on the web server) is known
as full stack JavaScript development.
In other words, there’s never been a better time to learn JavaScript!

What Is jQuery?
JavaScript has one embarrassing little secret: writing it can be hard. While it’s simpler than many other programming languages, JavaScript is still a programming
language. And many people, including web designers, find programming difficult.


xv

HTML: THE
BAREBONES
STRUCTURE

To complicate matters further, different web browsers understand JavaScript differently, so a program that works in, say, Chrome may be completely unresponsive
in Internet Explorer 9. This common situation can cost many hours of testing on
different machines and different browsers to make sure a program works correctly
for your site’s entire audience.
That’s where jQuery comes in. jQuery is a JavaScript library intended to make
JavaScript programming easier and more fun. A JavaScript library is a complex
set of JavaScript code that both simplifies difficult tasks and solves cross-browser
problems. In other words, jQuery solves the two biggest JavaScript headaches:
complexity and the finicky nature of different web browsers.
jQuery is a web designer’s secret weapon in the battle of JavaScript programming.
With jQuery, you can accomplish tasks in a single line of code that could take hundreds of lines of programming and many hours of browser testing to achieve with
your own JavaScript code. In fact, an in-depth book solely about JavaScript would
be at least twice as thick as the one you’re holding; and, when you were done reading it (if you could manage to finish it), you wouldn’t be able to do half of the things
you can accomplish with just a little bit of jQuery knowledge.
That’s why most of this book is about jQuery. It lets you do so much, so easily.
Another great thing about jQuery is that you can add advanced features to your
website with thousands of easy-to-use jQuery plug-ins. For example, the jQuery UI
plug-in (which you’ll meet on page 299) lets you create many complex user interface
elements like tabbed panels, drop-down menus, pop-up date-picker calendars—all
with a single line of programming!
Unsurprisingly, jQuery is used on millions of websites (http://trends.builtwith.com/
javascript/jQuery). It’s baked right into popular content management systems like
Drupal and WordPress. You can even find job listings for “jQuery Programmers”
with no mention of JavaScript. When you learn jQuery, you join a large community
of fellow web designers and programmers who use a simpler and more powerful
approach to creating interactive, powerful web pages.

HTML: The Barebones Structure
JavaScript isn’t much good without the two other pillars of web design—HTML and
CSS. Many programmers talk about the three languages as forming the “layers” of
a web page: HTML provides the structural layer, organizing content like pictures and
words in a meaningful way; CSS (Cascading Style Sheets) provides the presentational
layer, making the content in the HTML look good; and JavaScript adds a behavioral
layer, bringing a web page to life so it interacts with web visitors.
In other words, to master JavaScript, you need to have a good understanding of
both HTML and CSS.

xvi

JAVASCRIPT & JQUERY: THE MISSING MANUAL

NOTE

For a full-fledged introduction to HTML and CSS, check out Head First HTML with CSS and XHTML by
Elisabeth Robson and Eric Freeman. For an in-depth presentation of the tricky subject of Cascading Style Sheets,
pick up a copy of CSS3: The Missing Manual by David Sawyer McFarland (both from O’Reilly).

HTML: THE
BAREBONES
STRUCTURE

HTML (Hypertext Markup Language) uses simple commands called tags to define the
various parts of a web page. For example, this HTML code creates a simple web page:




Hey, I am the title of this web page.


Hey, I am some body text on this web page.



It may not be exciting, but this example has all the basic elements a web page needs.
This page begins with a single line—the document type declaration, or doctype for
short—that states what type of document the page is and which standards it conforms
to. HTML actually comes in different versions, and you use a different doctype with
each. In this example, the doctype is for HTML5; the doctype for an HTML 4.01 or
XHTML document is longer and also includes a URL that points the web browser to
a file on the Internet that contains definitions for that type of file.
In essence, the doctype tells the web browser how to display the page. The doctype can even affect how CSS and JavaScript work. With an incorrect or missing
doctype, you may end up banging your head against a wall as you discover lots of
cross-browser differences with your scripts. If for no other reason, always include
a doctype in your HTML.
Historically, there have been many doctypes—HTML 4.01 Transitional, HTML 4.01
Strict, XHTML 1.0 Transitional, XHTML 1.0 Strict—but they required a long line of
confusing code that was easy to mistype. HTML5’s doctype——is
short, simple, and the one you should use.

How HTML Tags Work
In the example in the previous section, as in the HTML code of any web page, you’ll
notice that most instructions appear in pairs that surround a block of text or other
commands. Sandwiched between brackets, these tags are instructions that tell a web
browser how to display the web page. Tags are the “markup” part of the Hypertext
Markup Language.
The starting (opening) tag of each pair tells the browser where the instruction begins,
and the ending tag tells it where the instruction ends. Ending or closing tags always
include a forward slash (/) after the first bracket symbol (<). For example, the tag



xvii

HTML: THE
BAREBONES
STRUCTURE

marks the start of a paragraph, while

marks its end. Some tags don’t have closing tags, like , , and
tags, which consist of just a single tag. For a web page to work correctly, you must include at least these three tags: • The tag appears once at the beginning of a web page (after the doctype) and again (with an added slash) at the end. This tag tells a web browser that the information contained in this document is written in HTML, as opposed to some other language. All of the contents of a page, including other tags, appear between the opening and closing tags. If you were to think of a web page as a tree, the tag would be its root. Springing from the root are two branches that represent the two main parts of any web page—the head and the body. • The head of a web page, surrounded by tags, contains the title of the page. It may also provide other, invisible information (such as search keywords) that browsers and web search engines can exploit. In addition, the head can contain information that’s used by the web browser for displaying the web page and for adding interactivity. You put Cascading Style Sheets, for example, in the head of the document. The head of the document is also where you often include JavaScript programming and links to JavaScript files. • The body of a web page, as set apart by its surrounding tags, contains all the information that appears inside a browser window: headlines, text, pictures, and so on. Within the tag, you commonly find tags like the following: • You tell a web browser where a paragraph of text begins with a

(opening paragraph tag), and where it ends with a

(closing paragraph tag). • The tag emphasizes text. If you surround some text with it and its partner tag, , you get boldface type. The HTML snippet Warning! tells a web browser to display the word “Warning!” in bold type. • The tag, or anchor tag, creates a hyperlink in a web page. When clicked, a hyperlink—or link—can lead anywhere on the Web. You tell the browser where the link points by putting a web address inside the tags. For instance, you might type Click here!. The browser knows that when your visitor clicks the words “Click here!” it should go to the Missing Manuals website. The href part of the tag is called an attribute and the URL (the uniform resource locator or web address) is the value. In this example, http://www.missingmanuals.com is the value of the href attribute. xviii JAVASCRIPT & JQUERY: THE MISSING MANUAL CSS: ADDING STYLE TO WEB PAGES UP TO SPEED Validating Web Pages As mentioned on page xvii, a web page’s doctype identifies which type of HTML or XHTML you used to create the web page. The rules differ subtly depending on type: For example, unlike HTML 4.01, XHTML doesn’t let you have an unclosed

tag, and requires that all tag names and attributes be lowercase ( not , for example). HTML5 includes new tags and lets you use either HTML or XHTML syntax. Because different rules apply to each variant of HTML, you should always validate your web pages. An HTML validator is a program that makes sure a web page is written correctly. It checks the page’s doctype and then analyzes the code in the page to see whether it matches the rules defined by that doctype. For example, the validator flags mistakes like a misspelled tag name or an unclosed tag. The World Wide Web Consortium (W3C), the organization that’s responsible for many of the technologies used on the Web, has a free online validator at http://validator.w3.org. You can copy your HTML and paste it into a web form, upload a web page, or point the validator to an already existing page on the Web; the validator then analyzes the HTML and reports back whether the page is valid or not. If there are any errors, the validator tells you what the error is and on which line of the HTML file it occurs. Valid HTML isn’t just good form—it also helps to make sure your JavaScript programs work correctly. A lot of JavaScript involves manipulating a web page’s HTML: identifying a particular form field, for example, or placing new HTML (like an error message) in a particular spot. In order for JavaScript to access and manipulate a web page, the HTML must be in proper working order. Forgetting to close a tag, using the same ID name more than once, or improperly nesting your HTML tags can make your JavaScript code behave erratically or not at all. CSS: Adding Style to Web Pages At the beginning of the Web, HTML was the only language you needed to know. You could build pages with colorful text and graphics and make words jump out using different sizes, fonts, and colors. But today, web designers turn to Cascading Style Sheets to add visual sophistication to their pages. CSS is a formatting language that lets you make text look good, build complex page layouts, and generally add style to your site. Think of HTML as merely the language you use to structure a page. It helps identify the stuff you want the world to know about. Tags like

and

denote headlines and assign them relative importance: A heading 1 is more important than a heading 2. The

tag indicates a basic paragraph of information. Other tags provide further structural clues: for example, a