CYAN MAGENTA
YELLOW BLACK PANTONE 123 CV
BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®
THE EXPERT’S VOICE ® IN WEB DEVELOPMENT Companion eBook Available
Beginning JavaScript with DOM Scripting and Ajax: From Novice to Professional
Coauthor of
Web Accessibility: Web Standards and Regulatory Compliance
FOR PROFESSIONALS BY PROFESSIONALS ™
Companion eBook
Regards, Christian Heilmann
THE APRESS ROADMAP Beginning CSS Web Development Pro CSS Techniques
See last page for details on $10 eBook version
Beginning XML with DOM and Ajax
DOM Scripting
forums.apress.com
Beginning
JavaScript with DOM Scripting and Ajax
with and Ajax
Join online discussions:
This is the only book you’ll need to learn the ins and outs of modern JavaScript development. This means concentrating on unobtrusive cross-browser techniques that enhance the user experience for the vast majority of us, but don’t break the page when the user is using a screenreader or has JavaScript disabled. It also means shying away from outdated DHTML hacks. When I wrote it, I had three goals in mind: to write a book that teaches you the language, uses realworld examples, and concentrates on techniques that will stand the test of time. Writing a beginner’s book about JavaScript is a tricky subject, but I’ve been careful to achieve a balance between basics and useful functionality—you’ll start slow enough to not get out of your depth and progress up to coding working examples of Ajax and DOM scripting. If you’re a JavaScript novice, this book will teach you how to write clean and maintainable code. If you’re already an experienced JavaScripter, it’ll help you brush up on JavaScript and say goodbye to outdated practices. The book is packed with real-world examples to learn from and use in your own projects, saving you hours of development time. The examples have been developed with certain ideals in mind: being platform and browser agnostic, accessible, web standards compliant, and very easy to maintain by others. I’ve experienced a lot in my eight years of developing JavaScript, and I’m not shy about discussing the mistakes I’ve made in the past to help you avoid making the same. I’m confident that you’ll find a wealth of useful information within these pages.
JavaScript
Dear Reader,
Beginning
From Novice to Professional The ultimate guide to modern JavaScript development!
Pro JavaScript Techniques
SOURCE CODE ONLINE
ISBN 1-59059-680-3
www.apress.com
53999 US $39.99
Heilmann
Beginning JavaScript with DOM Scripting and Ajax
Christian Heilmann Foreword by Simon Willison, Technology Development at Yahoo!
Shelve in JavaScript/ Web Development User level: Beginner–Intermediate
6
89253 59680
7
9 781590 596807
this print for content only—size & color not accurate
spine = 0.968" 512 page count
6803.book Page i Friday, June 23, 2006 9:05 AM
Beginning JavaScript with DOM Scripting and Ajax From Novice to Professional
■■■
Christian Heilmann
6803.book Page ii Friday, June 23, 2006 9:05 AM
Beginning JavaScript with DOM Scripting and Ajax: From Novice to Professional Copyright © 2006 by Christian Heilmann All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-680-7 ISBN-10 (pbk): 1-59059-680-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editors: Charles Brown, Chris Mills Technical Reviewer: Jon Stephens Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Beth Christmas Copy Edit Manager: Nicole LeClerc Copy Editor: Ami Knox Assistant Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Pat Christenson Proofreader: Lori Bring Indexer: Broccoli Information Management Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
[email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail
[email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.beginningjavascript.com and http:// www.apress.com.
6803.book Page iii Friday, June 23, 2006 9:05 AM
To Ioanna, who can sleep and look like an angel while some geek next to her hacks on the keyboard of a laptop and constantly mutters “Why won’t you work” under his breath.
6803.book Page v Friday, June 23, 2006 9:05 AM
Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1
Getting Started with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2
Data and Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
■CHAPTER 3
From DHTML to DOM Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
■CHAPTER 4
HTML and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
■CHAPTER 5
Presentation and Behavior (CSS and Event Handling) . . . . . . . . . . 123
■CHAPTER 6
Common Uses of JavaScript: Images and Windows . . . . . . . . . . . 183
■CHAPTER 7
JavaScript and User Interaction: Navigation and Forms . . . . . . . . . . 241
■CHAPTER 8
Back-End Interaction with Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
■CHAPTER 9
Data Validation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
■CHAPTER 10
Modern JavaScript Case Study: A Dynamic Gallery . . . . . . . . . . . . 387
■CHAPTER 11
Using Third-Party JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
■APPENDIX
Debugging JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
v
6803.book Page vii Friday, June 23, 2006 9:05 AM
Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1
Getting Started with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
The Why of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What Is JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Problems and Merits of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Why Use JavaScript If It Cannot Be Relied On? . . . . . . . . . . . . . . . . . . . . . . . 6 JavaScript in a Web Page and Essential Syntax . . . . . . . . . . . . . . . . . . . . . . 7 JavaScript Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Code Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 An Aside About Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Simple JavaScript Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
■CHAPTER 2
Data and Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
Data, Data Types, and Data Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . JavaScript Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Converting Different Types of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . The Composite Data Types: Array and Object . . . . . . . . . . . . . . . . . . . . . . . Objects JavaScript Supplies You with: String, Date, and Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Array Object’s Methods and Properties . . . . . . . . . . . . . . . . . .
18 18 21 23 26 30 30 39 42
vii
6803.book Page viii Friday, June 23, 2006 9:05 AM
viii
■C O N T E N T S
Making Decisions in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Logical and Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing Multiple Values: the switch Statement . . . . . . . . . . . . . . . . Repeating Things: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 3
■CHAPTER 4
From DHTML to DOM Scripting
45 45 49 52 54 59
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
JavaScript As “the Behavior Layer” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Object Detection vs. Browser Dependence . . . . . . . . . . . . . . . . . . . . Progressive Enhancement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . JavaScript and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Good Coding Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Code Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Short Code via Ternary Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sorting and Reuse of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Variable and Function Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Keeping Scripts Safe with the Object Literal . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63 65 68 69 71 71 72 74 76 79 80 80 81 83
HTML and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
85
The Anatomy of an HTML Document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Providing Feedback in Web Pages via JavaScript: The Old School Ways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Using window Methods: prompt(), alert(), and confirm() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Accessing the Document via the DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Of Children, Parents, Siblings, and Values . . . . . . . . . . . . . . . . . . . . . . . . . . 99 From the Parents to the Children . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 From the Children to the Parents . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Among Siblings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Changing Attributes of Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
6803.book Page ix Friday, June 23, 2006 9:05 AM
■C O N T E N T S
Creating, Removing, and Replacing Elements . . . . . . . . . . . . . . . . . . . . . . Avoiding NOSCRIPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shortening Your Scripts via InnerHTML. . . . . . . . . . . . . . . . . . . . . . . DOM Summary: Your Cheat Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . DOMhelp: Our Own Helper Library . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 5
Presentation and Behavior (CSS and Event Handling)
. . 123
Changing the Presentation Layer via JavaScript . . . . . . . . . . . . . . . . . . . . Helping the CSS Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing the Document’s Behavior via Event Handling . . . . . . . . . . . . . Events in the W3C-Compliant World . . . . . . . . . . . . . . . . . . . . . . . . . Fixing Events for the Non-W3C-Compliant World . . . . . . . . . . . . . . Never Stop Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Ugly Page Load Problem and Its Ugly Solutions . . . . . . . . . . . . Reading and Filtering Keyboard Entries . . . . . . . . . . . . . . . . . . . . . . The Dangers of Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 6
Common Uses of JavaScript: Images and Windows
109 113 115 116 118 122
123 131 153 156 165 172 173 174 179 180
. . . . 183
Images and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basics of Image Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preloading Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Rollover Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Slide Shows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary of Images and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . Windows and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Window Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Window Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary: Windows and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
183 184 186 187 196 211 212 213 215 238 239
ix
6803.book Page x Friday, June 23, 2006 9:05 AM
x
■C O N T E N T S
■CHAPTER 7
■CHAPTER 8
■CHAPTER 9
JavaScript and User Interaction: Navigation and Forms. . .
241
Navigation and JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Fear of the Page Reload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basics of Navigation and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . Browser Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . In-Page Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Site Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pagination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary of Navigation with JavaScript . . . . . . . . . . . . . . . . . . . . . . Forms and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basics of Forms with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . Form Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interactive Forms: Hiding and Showing Dependent Elements. . . . Custom Form Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary of Forms and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
241 242 242 245 246 255 263 272 272 274 275 291 297 297 297
Back-End Interaction with Ajax
. . . . . . . . . . . . . . . . . . . . . . . . . . 299
Household Cleaning Liquid, Football Club, or Flash Gordon’s Spacecraft: What Is Ajax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Et Tu, Cache? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Putting the X Back into Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing XML with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Server-Side Scripts to Reach Third-Party Content . . . . . . . . XHR on Slow Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Larger Ajax Example: Connected Select Boxes . . . . . . . . . . . . . . Optional Dynamic Ajax Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
300 309 309 314 316 320 323 331 340
Data Validation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
343
Pros and Cons of Client-Side JavaScript Validation . . . . . . . . . . . . . . . . . 343 A Quick Reminder About Protecting Content with JavaScript . . . . . . . . . 344 The One-Size-Fits-All Validation Myth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
6803.book Page xi Friday, June 23, 2006 9:05 AM
■C O N T E N T S
■CHAPTER 10
Basic JavaScript Validation with String and Numeric Methods . . . . . . . String Validation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Numeric Validation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Syntax and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wildcard Searches, Constraining Scope, and Alternatives . . . . . . Restricting the Number of Characters with Quantifiers . . . . . . . . . Word Boundaries, Whitespace, and Other Shortcuts . . . . . . . . . . . Methods Using Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . The Power of Parenthesis Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . Regular Expression Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary of Validation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Form Validation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Designating Mandatory Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Hidden Field Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Indicator Element Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The CSS Classes Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Custom Attribute Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Failures of These Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sharing Validation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Giving Users Validation Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Showing a List of Erroneous Fields . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing the Main Form with a Clickable Error Message. . . . . . . Highlighting Erroneous Fields Individually . . . . . . . . . . . . . . . . . . . . Instant Validation Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other Dynamic Validation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
346 346 352 357 357 358 359 360 361 361 363 363 364 364 364 365 366 366 367 367 369 369 374 376 379 381 385
Modern JavaScript Case Study: A Dynamic Gallery . . . . .
387
Basics of Thumbnail Galleries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is a Thumbnail Gallery and What Should It Do? . . . . . . . . . . . . . . . Static Thumbnail Galleries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Faking Dynamic Galleries with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . Displaying Captions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dynamic Thumbnail Galleries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Image Badge from a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
387 388 388 389 396 401 406 414
xi
6803.book Page xii Friday, June 23, 2006 9:05 AM
xii
■C O N T E N T S
■CHAPTER 11
■APPENDIX
Using Third-Party JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
415
What the Web Offers You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Code Snippets, RSS Feeds, APIs, and Libraries . . . . . . . . . . . . . . . . . . . . RSS Feeds and REST APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Examples of REST APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a Library: Short, Shorter, jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dangers of jQuery and Other Libraries Using Their Own Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using an API: Adding a Map to Your Site with Google Maps . . . . . . . . . . Full Service: The Yahoo Developer Network and User Interface Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bouncy Headlines Using YUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing Pop-Up Windows Using the YUI Connection Manager and Container Components . . . . . . . . . . . . . . . . . . . . . . Yahoo User Interface Library Summary . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
415 416 417 418 419
Debugging JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
451
Common JavaScript Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Misspellings and Case-Sensitivity Issues . . . . . . . . . . . . . . . . . . . . . Trying to Access Undefined Variables . . . . . . . . . . . . . . . . . . . . . . . . Incorrect Number of Closing Braces and Parentheses . . . . . . . . . . Concatenation Gone Wrong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Assigning Instead of Testing the Value of a Variable . . . . . . . . . . . Tracing Errors with alert() and “Console” Elements . . . . . . . . . . . . . Error Handling with try and catch() . . . . . . . . . . . . . . . . . . . . . . . . . . . Sequential Uncommenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error Reporting in Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Microsoft Internet Explorer 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Safari . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opera 8.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Firefox 1.5.0.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . JSLint and JSUNIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
451 451 452 454 456 458 458 459 462 463 463 464 466 466 470 470
426 427 438 439 444 449 450
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
6803.book Page xiii Friday, June 23, 2006 9:05 AM
Foreword T
here has never been a more exciting time to learn JavaScript. It may be a cliché, but it’s certainly true. After years spent banished in a wilderness of browser wars and false promises, JavaScript has finally emerged as an essential part of any web developer’s toolbox. No longer just a vehicle for ads and annoyances, it is now a valuable component of the next generation of web applications. What has caused this sudden rush of interest? The first reason is purely practical: browser improvements have finally made it viable to write cross-browser JavaScript with some certainty that it will actually work. The second is more revolutionary: Ajax, an old technique with a new name that enables client-side code to communicate directly with a server without refreshing the whole page. This simple ability has thrown web application development wide open, enabling new, innovative interfaces and dramatically altering user expectations of how web interfaces can behave. This adoption has been helped by the growing realization that JavaScript is not a toy language. Despite its numerous warts, beneath its deceptively simple exterior lie a host of powerful features not seen in many mainstream languages: closures, prototypal inheritance, and extensive support for the functional programming style. That such a flexible language is now installed on hundreds of millions of computers is a cause for celebration in itself. Just because you can do something doesn’t mean that you should. Not all browsers are born equal, and accessibility (both for people and for alternative devices) remains an essential aspect of developing for the Web. Understanding the issues and techniques around progressive enhancement is an important part of the JavaScript learning curve. The set of challenges posed by JavaScript development is huge. Browsers frequently deviate from what standard specifications there are, and pseudostandards are common and frequently unavoidable. Ambitious new applications are uncovering browser bugs that have laid dormant for years. The complexity of those applications is itself a problem, introducing new problems related to maintaining large code bases. Thankfully, the global JavaScript community has risen to the challenge. A wealth of code and resources awaits the intrepid developer, but the value of this treasure trove can be unlocked only through a solid understanding of the underlying platform. This book will provide you with that knowledge. As a long-standing teacher and leader in that community, Christian is the ideal guide to this intricate world. This book comprises accumulated wisdom obtainable only through years of experience. Christian will teach you to apply JavaScript tastefully, responsibly, and elegantly in a way that will delight your users and impress your peers. Simon Willison Technology Development at Yahoo! xiii
6803.book Page xv Friday, June 23, 2006 9:05 AM
About the Author
■CHRISTIAN HEILMANN grew up in Germany and, after a year working with people with disabilities for the Red Cross, spent a year as a radio producer. From 1997 onwards, he worked for several agencies in Munich as a web developer. In 2000, he moved to the US to work for eToys and, after the dot-com crash, he moved to the UK where he led the web development department at Agilisys. In April 2006, he joined Yahoo! UK as a web developer. He publishes an almost daily blog at http://wait-till-i.com and runs an article repository at http://icant.co.uk. He is a member of the Web Standards Project’s DOM Scripting Task Force.
xv
6803.book Page xvii Friday, June 23, 2006 9:05 AM
About the Technical Reviewer
■JON STEPHENS has contributed to numerous books on web and open source technologies as an author, reviewer, and editor. He’s particularly keen on JavaScript, PHP, Linux, and MySQL. Jon coauthored Professional JavaScript, Second Edition (Wrox Press, 2001), Beginning MySQL Database Design and Optimization: From Novice to Professional (Apress, 2004), and PHP 5 Recipes: A Problem-Solution Approach (Apress, 2005). He was also the technical reviewer for Jeremy Keith’s DOM Scripting: Web Design with JavaScript and the Document Object Model (friends of ED, 2005). Jon’s day job (we use the term “day” loosely here) is with MySQL AB as a technical writer, where he’s learned to appreciate the finer points of MySQL Cluster, DocBook XML, and very large fish caught in the Bay of Naples. His origins are lost in the depths of time and space, but he is rumored to have a 4-digit user ID on Slashdot.org and a daughter in Australia. (In recent years, we’ve posted Jon’s checks to an address in Brisbane, and someone claiming to be him has cashed them.) Jon has also been sighted from time to time shoveling down really extremely dangerously spicy Thai food purchased from street vendors in Bangkok and Ayutthaya.
xvii
6803.book Page xix Friday, June 23, 2006 9:05 AM
Acknowledgments I
’d like to acknowledge all who helped in getting this book done—Chris, Beth, Ami, Katie, and Charles at Apress, and Jon Stephens. I learned a lot, especially that writing a book is much more work than I thought. I’d also like to thank those who helped me by solving problems and asking for more and more features—my fellow WaSP DOM Scripting Task Force Members, Stuart Colville, Matt Warden, Jens Grochtdreis, Ingo Chao, Volkan Ozcelik, and many others on the evolt list, CSSdiscuss, and my blog comments. Thanks to my former colleagues at Agilisys and my current colleagues at Yahoo for testing help and support, and to Good for Food, The Spence, Pizzadelique, and Belle Epoque for keeping me in shape by providing nourishment (and also to the neighbors of these places for not protecting their wireless access points). And last but not least, I’d like to thank you, as buying this book shows that there are people who really want to learn JavaScript properly, instead of just copying and pasting in scripts. If this is a pirated PDF, just remember that I can trace all copies back to your computer, and I will know where you live.
xix
6803.book Page xxi Friday, June 23, 2006 9:05 AM
Introduction I
f you want to learn about JavaScript from scratch—what it is, what to use it for, and how to make it work with other technologies like CSS and HTML—you have picked up the right book. If you already have a considerable amount of experience with JavaScript, but want to bring your knowledge up to date, you have also picked up the right book—a lot has changed in JavaScript development in recent years. When JavaScript first started being used for web development back in the mid-to-late 1990s (it was first supported in Netscape 2, back in 1996), it quickly became much maligned, for many reasons—browser support was mediocre at best, and at worst, you actually had different JavaScript functions being implemented in different ways by different browsers (Netscape 4 and Internet Explorer 4 were major culprits, at the height of the so-called browser wars). This led to developers having to write completely different versions of web sites or indulge in messy code forking, if they wanted to attempt to have cross-browser support. And that was the consciencious ones—JavaScript’s bad reputation was just as much the fault of the developers as the browser manufacturers. Developers back in those days tended to use JavaScript for all manner of flashy effects that looked cool, but caused all manner of problems in terms of usability and accessibility (the days of DHTML—another marketing buzzword back in the day, which referred to the application of JavaScript, CSS, and HTML to produce dynamic effects). Pages would break completely if JavaScript was unavailable for any reason or if the user was trying to use a screenreader. And a lot of web developers would copy and paste scripts into their web sites without understanding how they actually worked, causing more untold usability and code maintenance nightmares. But as I said earlier, things have changed. Browser support is now at a manageable level, with the modern browsers largely using the same implementation of the Document Object Model and other constructs, and modern techniques are a lot more considerate of accessibility, etc. As you’ll learn through the course of this book, modern techniques like DOM scripting are built around the premise of separating out not only the structure (in your markup) and the presentation (in your CSS), but also the behavior in your JavaScript files (not interspersed through your markup documents). JavaScript does not have to be evil—you can code your web sites so that the JavaScript enhancements you are using can add to the experience of users who can take advantage of them, but they don’t break when JavaScript is not available. This is called unobtrusive JavaScript—JavaScript enhancements should be seen as a bonus for those who can use them, not an essential feature for the running of a site. If you’ve used JavaScript before, then be prepared to take on a new mindset as you step forward through the book. If you are completely new to JavaScript, then breathe a sign of relief and consider yourself lucky that you never had to suffer the early days of JavaScript development that I just touched on!
xxi
6803.book Page xxii Friday, June 23, 2006 9:05 AM
xxii
■I N T R O D U C T I O N
What This Book Will Cover JavaScript is probably simultaneously the most underrated and the most misused language in web development, but use it properly, and it can be a very valuable tool in your toolbox. In the following pages, we’ll look at the basics of JavaScript and modern JavaScript techniques, including DOM scripting for dynamic behavior and styling, and event handling. We’ll then look at essential applications of JavaScript, including data validation, image and window manipulation, and dynamic enhancements for forms and navigation menus. Next, I turn your attention to probably the biggest JavaScript-related buzzword of the moment—Ajax. Ajax standards for “Asynchronous JavaScript and XML,” which is a bit of a misnomer, as the technique doesn’t necessarily have to involve XML, and is probably used with HTML more often. But don’t concern yourself about that statement—it basically refers to creating dynamic functionality on web pages that works because small parts of the web page can be updated without having to refresh the whole page, for example, contact information in an online e-mail application (Gmail being the most obvious example that comes to mind). The most common way to do this right now is to use the XMLHttpRequest (XHR) object. It is very popular because it allows us to create web applications that have rich functionality and almost look and work like desktop applications. But Ajax does come with its own set of unique problems, which I’ll touch on in this book. Next follows a case study for you to work through, showing a full-blown modern JavaScriptenhanced web application. Finally, Chapter 11 looks at another essential facet of modern JavaScript development— using third-party JavaScript solutions. When you’re developing JavaScript applications, you don’t need to code everything yourself from scratch every time. As well as creating reusable objects and functions of your own, which will be covered in the earlier chapters of the book, there are also countless third-party resources available on the Web for you to download and use in your own applications, from libraries of functions, to full-blown APIs (application programming interfaces) for you to hook into. Specifically, we look at jQuery, the Google Maps API, the Yahoo! APIs, and much more besides.
Community and Support When you ask developers with different technology or design-oriented backgrounds what JavaScript is and what you should use it for, you will most probably get totally different answers. This book tries to teach you how to become a JavaScript developer who can work with each of these developers, and maybe get to change some views these people have by proving that you can use JavaScript to enhance a web site, build a web application, or even extend a piece of software without forcing the user to change his ways or hardware setup. This is a book to work along with—all the code examples provided can be downloaded and tried at http://www.beginningjavascript.com; you will also find extra information, bug fixes, and other examples there (my publishers will also keep a copy of the errata and code download at http://www.apress.com).
6803.book Page xxiii Friday, June 23, 2006 9:05 AM
■I N T R O D U C T I O N
But what happens when you get a problem? You have many options available to you. First, try getting ahold of me via my web site (http://wait-till-i.com) or mailing Apress about the issue (contact details available at http://www.apress.com). Second, try visiting any of the JavaScript forums available on the Web. Some of the best are • evolt’s thelist: http://lists.evolt.org/mailman/listinfo/thelist • The Mozilla JavaScript forums: http://developer.mozilla.org/en/docs/JavaScript • Webdeveloper.com JavaScript forum: http://www.webdeveloper.com/forum/ forumdisplay.php?f=3 • The comp.lang.javascript FAQ: http://jibbering.com/faq/ These forums are frequented by many knowledge seekers, such as yourself, but also many very experienced JavaScripters who are happy to help out the community with their problems to further the cause of modern JavaScript. Make sure you ask questions intelligently, and don’t just paste in your code and ask, “What’s wrong with this?” Also look over the other posts on the forums; you may find that your question has been asked by someone else, and answered already. Last of all, read blogs! A lot of the most talented JavaScripters out there like to share their ideas, innovations, and experiments with the rest of the world through their blogs, myself included (OK, so I couldn’t resist getting another plug in there!). This is a great way to pick up new ideas. I’d recommend reading the words of • Jeremy Keith: http://www.adactio.com • Simon Willison: http://simon.incutio.com/ • The WaSP DOM scripting task force: http://www.webstandards.org/action/dstf/ • Stuart Langridge: http://kryogenix.org/days/ • Robert Nyman: http://robertnyman.com/ • Jon Snook: http://www.snook.ca/jonathan/ Look at it like this—you’re now part of a very vibrant community. As well as learning a lot of useful stuff, you’ll meet a variety of interesting people, and have some fun along the way! Let’s get on with the fun—keep reading . . .
xxiii
6803.book Page 1 Friday, June 9, 2006 3:19 PM
CHAPTER 1 ■■■
Getting Started with JavaScript T
his book is about a scripting language called JavaScript and how to use it in a practical manner. After you read it, you’ll be able to • Understand JavaScript syntax and structures. • Create scripts that are easy to understand and maintain. • Write scripts that do not interfere with other JavaScripts. • Write scripts that make web sites easier to use without blocking out non-JavaScript users. • Write scripts that are independent of the browser or user agent trying to understand them—which means that in some years they will still be usable and won’t rely on obsolete technology. • Enhance a web site with JavaScript and allow developers without any scripting knowledge to change the look and feel. • Enhance a web document with JavaScript and allow HTML developers to use your functionality by simply adding a CSS class to an element. • Use progressive enhancement to make a web document nicer only when and if the user agent allows for it. • Use Ajax to bridge the gap between back end and client side, thus creating sites that are easier to maintain and appear much slicker to the user. • Use JavaScript as part of a web methodology that enables you to maintain it independently without interfering with the other development streams. What you will not find here are • Instructions on how to create effects that look flashy but do nothing of value for the visitor • JavaScript applications that are browser specific • JavaScripts that are only there to prove that they can be used and do not enhance the visitor’s experience • JavaScripts that promote unwanted content, such as pop-up windows or other flashy techniques like tickers or animation for animation’s sake
1
6803.book Page 2 Friday, June 9, 2006 3:19 PM
2
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
It is my credo that JavaScript has a place in modern web development, but we cannot take it for granted that the visitor will be able to use or even experience all the effects and functionality we can achieve with JavaScript. JavaScript allows us to completely change the web page by adding and removing or showing and hiding elements. We can offer users richer interfaces like dragand-drop applications or multilevel drop-down menus. However, some visitors cannot use a drag-and-drop interface because they can only use a keyboard or rely on voice recognition to use our sites. Other visitors might be dependent on hearing our sites rather than seeing them (via screen readers) and will not necessarily be notified of changes achieved via JavaScript. Last but not least, there are users who just cannot have JavaScript enabled, for example, in high-security environments like banks. Therefore, it is necessary to back up a lot of the things we do in JavaScript with solutions on the server side. Sadly, JavaScript also has a history of being used as a way to force information onto the visitor that was not requested (pop-up windows are a good example). This practice is frowned on by me, as well as many professional web designers. It is my hope that you will not use the knowledge gained from this book to such an end.
■Note Web design has matured over the years—we stopped using FONT tags and deprecated visual attributes like bgcolor and started moving all the formatting and presentational attributes to a CSS file. The same cleaning process has to happen to JavaScript should it remain a part of web development. We separated content, structure, and presentation, and now it is time to separate the behavior of web sites from the other layers. Web development now is for business and for helping the user rather than for the sake of putting something out there and hoping it works in most environments.
It is high time we see JavaScript as a part of an overall development methodology, which means that we develop it not to interfere with other technologies like HTML or CSS, but to interact with them or complement them. To that end, we see the emergence of a new technology (or at least a new way of using existing technologies) called Ajax, which we will discuss in Chapter 8. Web development has come quite a way since the 1990s, and there is not much sense in creating web sites that are static and fixed in their size. Any modern web design should allow for growth as needed. It should also be accessible to everyone (which does not mean that everybody gets the same appearance—a nice multicolumn layout, for example, might make sense on a high-resolution monitor but is hard to use on a mobile phone or a PDA)—and ready for internationalization. We cannot afford any longer to build something and think it’ll last forever. Since the Web is about content and change, it’ll become obsolete if we don’t upgrade our web products constantly and allow other data sources to feed into it or get information from it. Enough introductions—you got this book to learn about JavaScript, so let’s start by talking quickly about JavaScript’s history and assets before diving right into it.
6803.book Page 3 Friday, June 9, 2006 3:19 PM
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
In this chapter you’ll learn • What JavaScript is and what it can do for you • The advantages and disadvantages of JavaScript • How to add JavaScript to a web document and its essential syntax • Object-oriented programming (OOP) in relation to JavaScript • How to write and run a simple JavaScript program Chances are that you have already come across JavaScript, and already have an idea of what it is and what it can do, so we’ll move quite swiftly through some basics of the language and its capabilities first. If you know JavaScript well already, and you simply want to know more about the newer and more accessible features and concepts, you might skip to Chapter 3. I won’t hold it against you—however, there might be some information you’ve forgotten, and a bit of a review doesn’t hurt, either.
The Why of JavaScript In the beginning of the Web, there was HTML and the Common Gateway Interface (CGI). HTML defines the parts of a text document and instructs the user agent (usually the web browser) how to show it—for example, text surrounded by the tags
becomes a paragraph. Within that paragraph you may have
tags that define the main page heading. Notice that for most opening tags, there is a corresponding closing tag that begins with
3
6803.book Page 4 Friday, June 9, 2006 3:19 PM
4
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
Some information, such as calculations and verifying the information on a form, may not need to come from the server. JavaScript is executed by the user agent (normally a browser) on the visitor’s computer. We call this client-side code. This could result in fewer trips to the server and faster-running web sites.
What Is JavaScript? JavaScript started life as LiveScript, but Netscape changed the name—possibly because of the excitement being generated by Java—to JavaScript. The name is confusing though, as there is no real connection between Java and JavaScript—although some of the syntax looks similar. Java is to JavaScript what Car is to Carpet ——From a JavaScript discussion group on Usenet Netscape created the JavaScript language in 1996 and included it in their Netscape Navigator (NN) 2.0 browser via an interpreter that read and executed the JavaScript added to .html pages. The language has steadily grown in popularity since then, and is now supported by the most popular browsers. The good news is that this means JavaScript can be used in web pages for all major modern browsers. The not-quite-so-good news is that there are differences in the way the different browsers implement JavaScript, although the core JavaScript language is much the same. However, JavaScript can be turned off by the user—and many companies and other institutions require their users to do so for security reasons. We will discuss this further shortly, as well as throughout this book. The great thing about JavaScript is that once you’ve learned how to use it for browser programming, you can move on to use it in other areas. Microsoft’s server—IIS—uses JavaScript to program server-side web pages (ASP), PDF files now use JavaScript, and even Windows administration tasks can be automated with JavaScript code. A lot of applications such as Dreamweaver and Photoshop are scriptable with JavaScript. Operating system add-ons like the Apple Dashboard or Konfabulator on Linux and Windows even allow you to write small helper applications in JavaScript. Lately a lot of large companies also offer application programming interfaces (APIs) that feature JavaScript objects and methods you can use in your own pages—Google Maps being one of them. You can offer a zoomable and scrollable map in your web site with just a few lines of code. Even better is the fact that JavaScript is a lot easier to develop than higher programming languages or server-side scripting languages. It does not need any compilation like Java or C++, or to be run on a server or command line like Perl, PHP, or Ruby: all you need to write, execute, debug, and apply JavaScript is a text editor and a browser—both of which are supplied with any operating system. There are, of course, tools that make it a lot easier for you, examples being JavaScript debuggers like Mozilla Venkman, Microsoft Script Debugger, or kjscmd.
6803.book Page 5 Friday, June 9, 2006 3:19 PM
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
Problems and Merits of JavaScript As I mentioned at the outset of this chapter, JavaScript has been an integral part of web development over the last few years, but it has also been used wrongly. As a result, it has gotten a bad reputation. The reason for this is gratuitous JavaScript effects, like moving page elements and pop-up windows, which might have been impressive the first time you saw them but soon turned out to be just a “nice to have” and in some cases even a “nice to not have any longer.” A lot of this comes from the days of DHTML (more on this in Chapter 3). The term user agent and the lack of understanding what a user agent is can also be a problem. Normally, the user agent is a browser like Microsoft Internet Explorer (MSIE), Netscape, Mozilla (Moz), Firefox (Fx), Opera, or Safari. However, browsers are not the only user agents on the Web. Others include • Assistive technology that helps users to overcome the limitations of a disability—like text-to-speech software or Braille displays • Text-only agents like Lynx • Web-enabled applications • Game consoles • Mobile/cell phones • PDAs • Interactive TV set-top boxes • Search engines and other indexing programs • And many more This large variety of user agents, of different technical finesse (and old user agents that don’t get updated), is also a great danger for JavaScript. Not all visitors to your web site will experience the JavaScript enhancements you applied to it. A lot of them will also have JavaScript turned off—for security reasons. JavaScript can be used for good and for evil. If the operating system—like unpatched Windows—allows you to, you can install viruses or Trojan Horses on a computer via JavaScript or read out user information and send it to another server.
■Note There is no way of knowing what the visitor uses or what his computer is capable of. Furthermore, you never know what the visitor’s experience and ability is like. This is one of the beautiful aspects of the Web—everyone can participate. However, this can introduce a lot of unexpected consequences for the JavaScript programmer.
5
6803.book Page 6 Friday, June 9, 2006 3:19 PM
6
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
In many cases, you might want to have a server-side backup plan. It would test to see whether the user agent supports the functionality desired and, if it doesn’t, the server takes over. Independence of scripting languages is a legal requirement for web sites, defined in the Digital Discrimination Act for the UK, section 508 in the US law, and many more localized legal requirements throughout the world. This means that if the site you developed cannot be used without JavaScript, or your JavaScript enhancements are expecting a certain ability of the users or their user agent without a fallback, your client could be sued for discrimination. However, JavaScript is not evil or useless, and it is a great tool to help your visitor to surf web sites that are a lot slicker and less time-consuming.
Why Use JavaScript If It Cannot Be Relied On? As I just mentioned, just because it may not always be available doesn’t mean that JavaScript shouldn’t be used at all. It should simply not be the only means of user interaction. The merits of using JavaScript are • Less server interaction: You can validate user input before sending the page off to the server. This saves server traffic, which means saving money. • Immediate feedback to the visitors: They don’t have to wait for a page reload to see if they have forgotten to enter something • Automated fixing of minor errors: For example, if you have a database system that expects a date in the format dd-mm-yyyy and the visitor enters it in the form dd/mm/yyyy, a clever JavaScript script could change this minor mistake prior to sending the form to the server. If that was the only mistake the visitor made, you can save her an error message— thus making it less frustrating to use the site. • Increased usability by allowing visitors to change and interact with the user interface without reloading the page: For example, by collapsing and expanding sections of the page or offering extra options for visitors with JavaScript. A classic example of this would be select boxes that allow immediate filtering, such as only showing the available destinations for a certain airport, without making you reload the page and wait for the result. • Increased interactivity: You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard. This is partly possible with CSS and HTML as well, but JavaScript offers you a lot wider—and more widely supported— range of options. • Richer interfaces: If your users allow for it, you can use JavaScript to include such items as drag-and-drop components and sliders—something that originally was only possible in thick client applications your users had to install, such as Java applets or browser plug-ins like Flash.
6803.book Page 7 Friday, June 9, 2006 3:19 PM
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
• Lightweight environment: Instead of downloading a large file like a Java applet or a Flash movie, scripts are small in file size and get cached (held in memory) once they have been loaded. JavaScript also uses the browser controls for functionality rather than its own user interfaces like Flash or Java applets do. This makes it easier for users, as they already know these controls and how to use them. Modern Flash and Macromedia Flex applications do have the option to stream media and—being vector based—are visually scalable, something JavaScript and HTML controls aren’t. On the other hand, they require the plug-in to be installed.
JavaScript in a Web Page and Essential Syntax Applying JavaScript to a web document is very easy; all you need to do is to use the script tag: <script type="text/javascript"> // Your code here For older browsers, or if you want to use strict XHTML (the newest version of HTML) instead of transitional, you’ll need to comment out the code to make sure the user agent does not display it inside the page or tries to render it as HTML markup. There are two different syntaxes for commenting out code. For HTML documents and transitional XHTML, you use the normal HTML comments: <script type="text/javascript"> In strict XHTML, you will need to use the CDATA commenting syntax to comment out your code—however, it is best not to add any JavaScript inside strict XHTML documents, but keep it in its own document. More on this in Chapter 3. <script type="text/javascript"> Technically it is possible to include JavaScript anywhere in the HTML document, and browsers will interpret it. However, there are reasons in modern scripting why this is a bad idea. For now though, we will add JavaScript examples to the body of the document to allow you to see immediately what your first scripts are doing. This will help you get familiar with JavaScript a lot easier than the more modern and advanced techniques awaiting you in Chapter 3.
■Note There is also an “opposite” to the script tag—noscript—which allows you to add content that will only be displayed when JavaScript is not available. However, noscript is deprecated in XHTML and strict HTML, and there is no need for it—if you create JavaScript that is unobtrusive.
7
6803.book Page 8 Friday, June 9, 2006 3:19 PM
8
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
JavaScript Syntax Before we go any further, we should discuss some JavaScript syntax essentials: • // indicates that the rest of the current line is a comment and not code to be executed, so the interpreter doesn’t try to run it. Comments are a handy way of putting notes in the code to remind us what the code is intended to do, or to help anyone else reading the code see what’s going on. • /* indicates the beginning of a comment that covers more than one line. • */ indicates the end of a comment that covers more than one line. Multiline comments are also useful if you want to stop a certain section of code from being executed but don’t want to delete it permanently. If you were having problems with a block of code, for example, and you weren’t sure which lines were causing the problem, you could comment one portion of it at a time in order to isolate the problem. • Curly braces ({ and }) are used to indicate a block of code. They ensure that all the lines inside the braces are treated as one block. You will see more of these when we discuss structures such as if or for, as well as functions. • A semicolon or a newline defines the end of a statement, and a statement is a single command. Semicolons are in fact optional, but it’s still a good idea to use them to make clear where statements end, because doing so makes your code easier to read and debug. (Although you can put many statements on one line, it’s best to put them on separate lines in order to make the code easier to read.) You don’t need to use semicolons after curly braces. Let’s put this syntax into a working block of code: <script type="text/JavaScript"> // One-line comments are useful for reminding us what the code is doing /* This is a multiline comment. It's useful for longer comments and also to block out segments of code when you're testing */ /* Script starts here. We're declaring a variable myName, and assigning to it the value of whatever the user puts in the prompt box (more on that in Chapter 2), finishing the instruction with a semicolon because it is a statement */
6803.book Page 9 Friday, June 9, 2006 3:19 PM
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
var myName = prompt ("Enter your name",""); // If the name the user enters is Chris Heilmann if (myName == "Chris Heilmann") { // then a new window pops up saying hello alert("Hello Me"); } // If the name entered isn't Chris Heilmann else { // say hello to someone else alert("hello someone else"); } Some of the code may not make sense yet, depending on your previous JavaScript experience. All that matters for now is that it’s clear how comments are used, what a code block is, and why there are semicolons at the end of some of the statements. You can run this script if you like—just copy it into an HTML page, save the document with the file extension .html, and open it in your browser. Although statements like if and else span more than one line and contain other statements, they are considered single statements and don’t need a semicolon after them. The JavaScript interpreter knows that the lines linked with an if statement should be treated as one block because of the curly braces, {}. While not mandatory, it is a good idea to indent the code within the curly braces. This makes reading and debugging much easier. We’ll be looking at variables and conditional statements (if and else) in the next chapter.
Code Execution The browser reads the page from top to bottom, so the order in which code executes depends on the order of the script blocks. A script block is the code between the <script> and tags. (Also note that it’s not just the browser that can read our code; the user of a web site can view your code, too, so it’s not a good idea to put anything secret or sensitive in there.) There are three script blocks in this next example: <script type="text/javascript"> alert( 'First script Block '); alert( 'First script Block - Second Line ');
9
6803.book Page 10 Friday, June 9, 2006 3:19 PM
10
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
Test Page
<script type="text/JavaScript"> alert( 'Second script Block' );
Some more HTML
<script type="text/JavaScript"> alert( 'Third script Block' ); function doSomething() { alert( 'Function in Third script Block' ); } If you try it out, you’ll see that the alert() dialog in the first script block appears first displaying the message
First script Block
followed by the next alert() dialog in the second line displaying the message
First script Block - Second Line.
The interpreter continues down the page and comes to the second script block, where the alert() function displays this dialog:
Second script Block
and the third script block following it with an alert() statement that displays
Third script Block
6803.book Page 11 Friday, June 9, 2006 3:19 PM
C H A P T E R 1 ■ GETTING STARTED WITH JAVASCRIPT
Although there’s another alert statement inside the function a few lines down, it doesn’t execute and display the message. This is because it’s inside a function definition (function doSomething()) and code inside a function executes only when the function is called.
An Aside About Functions We’ll be talking about functions in much more depth in Chapter 3, but I introduce them here because you can’t get very far in JavaScript without an understanding of functions. A function is a named, reusable block of code, surrounded by curly braces, that you create to perform a task. JavaScript contains functions that are available for us to use and perform tasks like displaying a message to the user. Proper use of functions can save a programmer a lot of writing of repetitive code. We can also create our own functions, which is what we did in the previous code block. Let’s say we create some code that writes out a message to a page in a certain element. We’d probably want to use it again and again in different situations. While we could cut and paste code blocks wherever we wanted to use them, this approach can make the code excessively long; if you want the same piece of code three or four times within one page, it’ll also get pretty hard to decipher and debug. Instead we can wrap the messaging code into a function and then pass in any information that the function needs in order to work using parameters. A function can also return a value to the code that called the function into action originally. To call the function, you simply write its name followed by parentheses, (). (Note—you use the parentheses to pass the parameters. However, even when there are no parameters, you must still use the parentheses.) But you can’t call the function, as you might expect, until the script has created it. We can call it in this script by adding it to the third script block like this: <script type="text/JavaScript"> alert( 'Third script Block '); function doSomething(){ alert( 'Function in Third script Block '); } // Call the function doSomething doSomething();