The Javascript Anthology 101 Essential Tips, Tricks & Hacks (4 Chapter Sample)
Total Page:16
File Type:pdf, Size:1020Kb
The JavaScript Anthology 101 Essential Tips, Tricks & Hacks (4 Chapter Sample) Thank you for downloading this four-chapter sample of James Edwards’s and Cameron Adams’s book, The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks, published by SitePoint. This excerpt includes the Summary of Contents, Information about the Authors, Editors and SitePoint, Table of Contents, Preface, four chapters of the book, and the index. We hope you find this information useful in evaluating this book. For more information or to order, visit sitepoint.com Summary of Contents of this Excerpt Preface ..........................................................................................xi 1. Getting Started with JavaScript............................................... 1 5. Navigating the Document Object Model.............................. 79 7. Working with Windows and Frames .................................. 127 13. Basic Dynamic HTML....................................................... 229 Index......................................................................................... 565 Summary of Additional Book Contents 2. Working with Numbers......................................................... 31 3. Working with Strings............................................................. 45 4. Working with Arrays ............................................................. 65 6. Processing and Validating Forms ........................................ 103 8. Working with Cookies......................................................... 143 9. Working with Dates and Times .......................................... 151 10. Working with Images ........................................................ 167 11. Detecting Browser Differences .......................................... 191 12. Using JavaScript with CSS ................................................ 201 14. Time and Motion............................................................... 267 15. DHTML Menus and Navigation....................................... 321 16. JavaScript and Accessibility............................................... 385 17. Using JavaScript with Flash............................................... 457 18. Building Web Applications with JavaScript...................... 467 19. Object Orientation in JavaScript....................................... 515 20. Keeping up the Pace .......................................................... 565 The JavaScript Anthology 101 Essential Tips, Tricks & Hacks by James Edwards and Cameron Adams The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks by James Edwards and Cameron Adams Copyright © 2006 SitePoint Pty. Ltd. Expert Reviewer: Bobby van der Sluis Editor: Georgina Laidlaw Expert Reviewer: Derek Featherstone Index Editor: Bill Johncocks Managing Editor: Simon Mackie Cover Design: Jess Mason Technical Editor: Kevin Yank Cover Layout: Alex Walker Printing History: First Edition: February 2006 Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringe- ment of the trademark. Published by SitePoint Pty. Ltd. 424 Smith Street Collingwood VIC Australia 3066. Web: www.sitepoint.com Email: [email protected] ISBN 0-9752402-6-9 Printed and bound in the United States of America About the Authors James Edwards (aka brothercake1) is a freelance web developer based in the United Kingdom, specializing in advanced DHTML programming and accessible web site devel- opment. He is an outspoken advocate of standards-based development, a part-time forum moderator, and author of the Ultimate Drop Down Menu2 system—the first commercial DHTML menu to be WCAG-compliant. Cameron Adams has a degree in law and one in science; naturally he chose a career in web development. His business cards say, “Web Technologist” because he likes to have a hand in graphic design, JavaScript, CSS, PHP, and anything else that takes his fancy that morning. While running his own business—themaninblue.com3—he’s consulted and worked for numerous government departments, nonprofit organizations, large corporations, and tiny startups. Cameron lives in Melbourne, Australia, where, between coding mara- thons, he likes to play soccer and mix some tunes for his irate neighbors. About the Expert Reviewers Bobby van der Sluis lives in the Netherlands and works at Blast Radius4 in Amsterdam, where he manages the interface development department. He’s a client-side web techno- logies and design specialist, occasionally writing about these topics on his personal web site.5 Bobby is an evangelist of unobtrusive JavaScript, progressive enhancement, and the use of best practices, and has contributed to many notable sites, including A List Apart and CSS Zen Garden. He spends his scarce spare time with his wife Anita and newly-born daughter, Sofie. Derek Featherstone is a well-known instructor, author, speaker, and developer with expert- ise in web accessibility consulting. Derek delivers technical training that is engaging, in- formative, and immediately applicable. A high-quality instructor, he draws on his back- ground as a former high school teacher, plus seven years running his web development and accessibility consultancy Further Ahead.6 Derek blogs at boxofchocolates.ca.7 About the Technical Editor As Technical Director for SitePoint, Kevin Yank oversees all of its technical publica- tions—books, articles, newsletters, and blogs. He has written over 50 articles for SitePoint, but is best known for his book, Build Your Own Database Driven Website Using PHP & 1 http://www.brothercake.com/ 2 http://www.udm4.com/ 3 http://themaninblue.com/ 4 http://www.blastradius.com/ 5 http://www.bobbyvandersluis.com/ 6 http://www.furtherahead.com/ 7 http://boxofchocolates.ca/ MySQL. Kevin lives in Melbourne, Australia, and enjoys performing improvised comedy theatre and flying light aircraft. About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our books, newsletters, articles and community forums. For Kizzy—I missed @media for all the right reasons. —James This is for Mum, Dad, Darren, and Davina, who gave me their love and support throughout the writing of this book, even though I had to explain it all using plasticine dinosaurs. —Cameron Table of Contents Preface ..................................................................................................... xi Who Should Read this Book? .............................................................. xi What’s in this Book? .......................................................................... xii The Book’s Web Site .......................................................................... xv The Code Archive ....................................................................... xv Updates and Errata ..................................................................... xv The SitePoint Forums ......................................................................... xv The SitePoint Newsletters .................................................................. xvi Your Feedback ................................................................................... xvi Acknowledgements ............................................................................ xvi 1. Getting Started with JavaScript .............................................................. 1 JavaScript Defined ............................................................................... 1 JavaScript’s Limitations ........................................................................ 2 Security Restrictions .................................................................... 3 JavaScript Best Practices ...................................................................... 5 Providing for Users who Don’t Have JavaScript (Progressive Enhance- ment) .................................................................................................. 5 Separating Content from Behavior (Unobtrusive Scripting) .................. 8 Using Braces and Semicolons (Consistent Coding Practice) ................. 11 Adding a Script to a Page ................................................................... 12 Putting HTML Comments Around Code .................................... 13 The language Attribute ............................................................. 14 Getting Multiple Scripts to Work on the Same Page ........................... 14 Hiding JavaScript Source Code ........................................................... 18 Debugging a Script ............................................................................. 19 Understanding