The Book of JavaScript, 2nd Edition
thau!
Copyright 2006
No Starch Press
Dedication
I dedicate this revised edition of The Book of JavaScript to my wonderful wife Kirsten Menger-Anderson, who never failed to keep a straight face when I said, "It's almost done."
ADVANCE PRAISE FOR THE BOOK OF JAVASCRIPT, 2ND EDITION
"Thau! has been demonstrating his JavaScript wisdom since before it was cool. Now, this new edition of his classic text makes JavaScript wizardry more accessible than ever for experts and beginners alike."
-- JESSE JAMES GARRETT, CREATOR OF THE TERM "AJAX" AND AUTHOR OF The Elements of User Experience
"The fastest way to killer JavaScript chops!"
-- STEWART BUTTERFIELD, CO-FOUNDER, FLICKR.COM
"The new version of The Book of JavaScript offers the same elegant simplicity, but now includes the whiz-bang popular advances like working with the DOM and Ajax."
-- TED RHEINGOLD, TOP DOG, DOGSTER, INC. (DOGSTER.COM AND CATSTER.COM)
PRAISE FOR THE BOOK OF JAVASCRIPT, 1ST EDITION
" The Book of JavaScript shines.... [It's] an outstanding model for teaching language by example."
-- WEB TECHNIQUES (NOW NEW ARCHITECT)
"A great introduction to JavaScript for beginners."
-- JAVASCRIPT.COM
" The Book of JavaScript is ideal for people who are intimidated by the thought of writing code."
-- FOREWORD MAGAZINE
"A practical, surprisingly easy guide to use, even for relative novices."
-- THE MIDWEST BOOK REVIEW
"Thau's JavaScript tutorials at Webmonkey.com are so insanely popular, they've earned him a Plato-like following of devoted fans... Thau-mania is destined to grow to Elvis proportions."
-- EVANY THOMAS, FORMER MANAGING EDITOR, WEBMONKEY.COM
FOREWORD
The first JavaScript I remember writing was a routine to change two frames at the same time. I was the production specialist for HotWired, and it was shortly after frames and JavaScript debuted, well before there was documentation for either. Fortunately, it was also well before Internet Explorer 3.0 appeared on the scene, so I only had to make sure my JavaScript worked for Netscape 2.0. Even so, without a reference book to point out where possible pitfalls could be or answer simple questions such as how to set variables that JavaScript would like or how to make different windows talk to each other, it was one hell of a challenge. And it was deeply satisfying when I got it to work correctly.
When Dave asked me to do the technical review of the second edition of The Book of JavaScript , I couldn't have been more pleased or honored. The deep satisfaction I felt when I wrote those first JavaScripts and they worked correctly, and the deeper satisfaction I felt as more and more browsers were released and I figured out how to write cross-browser and cross-platform JavaScript, are the same feelings I got when I read Dave's explanations and examples. He describes what a piece of code is going to do and how to think about it, then lays out an example of code that makes sensewhether you're a seasoned programmer or entirely new to JavaScript. On top of all that, he takes a practical approach to programming, he's able to explain complex problems in a way that doesn't make them sound daunting, and when you're done covering each topic, you feel like you've earned the knowledge. That's rare, and it's really, really refreshing.
Since the first edition of this book was published, there have been a few advancements in JavaScript, most notably the advent of Ajax. Ajax is a concept that makes even a few professional programmers' heads spin, but (not surprisingly) Dave manages to break down what Ajax is and what it isn't, explains when it makes sense to use it, and shows you how to do it.
If you're new to JavaScript, you winyou couldn't ask for a better person to teach you how to program. If you're an old hat at JavaScript and you're looking for a refresher course or wondering how to take advantage of Ajax, you win too.
Happy coding!
Luke Knowland
Interaction Designer, Six Apart
San Francisco
FOREWORD TO THE FIRST EDITION
I learned JavaScript completely on my own. There was no one to tell me about "event handlers" or how to set cookies. No one even explained what a variable is, let alone the best ways to name them. Of course I had reference books, but they were intimidating tomes, full of cryptic lists and tables, written by programmers for programmers.
David Thau is a remarkable combination of natural teacher and seasoned programmer. As a result, The Book of JavaScript not only teaches JavaScript thoroughly and enjoyably in a friendly, unintimidating tone, but it teaches programming as elegantly as any book I've seen. In fact, I've always thought of this as Thau's ulterior motivehe pretends he's just showing you how to make a rollover or validate the text in an HTML form, but before you know it you've learned how to code!
Perhaps the most telling thing I can say is that, reading this book, I can't help but wish I was learning JavaScript for the first time. If you are, then consider yourself lucky to have Thau as a teacher. You couldn't do better.
Happy JavaScripting!
Nadav Savio
Principal, Giant Ant Design
San Francisco
ACKNOWLEDGMENTS
This second edition of The Book of JavaScript took me much longer to complete than I could have imagined. I'd like to thank the entire No Starch Press staff for putting up with all the delays and surprise extra bits. I would especially like to thank Christina Samuell for moving the process along, William Pollock, Riley Hoffman, Jerome Colburn, and Stephanie Provines for extensive edits, and Luke Knowland for making sure all the code works and offering many excellent suggestions. I'd also like to thank my neighbor, Laurentino Padilla, for sweeping our sidewalk on Thursdays. Without him, this book would have taken even longer to finish.
INTRODUCTION
You are about to begin a journey through JavaScripta programming language that adds interactivity and spark to web pages all over the Internet. This book, written primarily for nonprogrammers, provides scripts you can cut and paste for use on your website, but it also explains how they work, so you'll soon be writing your own scripts. Each chapter focuses on a few important JavaScript features, shows you how professional websites incorporate those features, and gives you examples of how you might add those features to your own web pages.
How This Book Is Organized
Before you dive in, here is a quick overview of what you'll learn as you make your way through The Book of JavaScript .
Have fun!
This chapter lays out the book's goals, introduces you to JavaScript and compares it to other tools, describes some of the nifty ways in which JavaScript can enhance your web pages, and walks you through writing your first JavaScript.
Did you know that JavaScript can figure out what day it is and write the date to a web page? This chapter will show you how. Along the way you'll also learn how JavaScript remembers things using variables and performs actions using functions.
In this chapter you'll learn how to direct someone to a web page specifically designed for his or her browser. You'll figure out which browser the visitor is using, then you'll use