Beginning JavaScript Fourth Edition
Published byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256
www.wiley.com
Copyright 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-52593-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions .
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Control Number: 2009933758
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. JavaScript is a registered trademark of Sun Microsystems Incorporated. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
In memory of my mum, June Wilton, who in 2006 lost her brave battle against cancer. She was always very proud of me and my books and showed my books to anyone and everyone she happened to meet however briefly and whether they wanted to see them or not! Shes very much missed. Paul Wilton
To my family: Starla, Hayden, and Evan (whom we havent yet met in person). To my parents: Jerry and Judy. Thank you all for your love and support. Jeremy McPeak
About the Authors
Paul Wilton started as a Visual Basic applications programmer at the Ministry of Defense in the UK and then found himself pulled into the Net. Having joined an Internet development company, he spent three years helping create Internet solutions. Hes now running his own successful and rapidly growing company developing online holiday property reservation systems.
Jeremy McPeak is a self-taught programmer who began his career by tinkering with web sites in 1998. He is the co-author of Professional Ajax, 2nd Edition (Wiley 2007) and several online articles covering topics such as XSLT, ASP.NET WebForms, and C#. He is currently employed in an energy-based company building in-house conventional and web applications.
Jeremy can be reached through his web site www.wdonline.com .
Credits
Acquisitions Editor
Scott Meyers
Project Editor
Maureen Spears
Technical Editor
David M. Karr
Production Editor
Rebecca Anderson
Copy Editor
C.M. Jones
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
Marketing Manager
David Mayhew
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lindsay Stanford
Compositor
Craig Johnson, Happenstance Type-O-Rama
Proofreader
Kathryn Duggan
Indexer
J & J Indexing
Cover Image
Photographers Choice/Punchstock
Acknowledgments
First, a big thank you to my partner Beci, who, now that the books finished, will get to see me for more than 10 minutes a week.
Id also like to say a very big thank you to Maureen Spears, who has worked very efficiently on getting this book into print.
Thanks also to Jim Minatel for making this book happen.
Many thanks to everyone whos supported and encouraged me over my many years of writing books. Your help will always be remembered.
Finally, pats and treats to my German Shepherd Dog, Katie, who does an excellent job of warding off disturbances from door-to-door salespeople.
Paul Wilton
First and foremost, a huge thank you to my wife for putting up with my late nights.
Just as huge thanks go to the people at Wiley Publishing: Jim Minatel and Scott Meyers for making this happen; Maureen Spears who was absolutely wonderful to work with in getting this book into its final, printed form; and David M. Karr for keeping me honest.
Lastly, thank you Nicholas C. Zakas, author of Professional JavaScript, 2nd Edition (Wiley 2009) and co-author of Professional Ajax, 2nd Edition (Wiley 2007), for getting me into this business.
Jeremy McPeak
Introduction
JavaScript is a scripting language that enables you to enhance static web applications by providing dynamic, personalized, and interactive content. This improves the experience of visitors to your site and makes it more likely that they will visit again. You must have seen the flashy drop-down menus, moving text, and changing content that are now widespread on web sites they are enabled through JavaScript. Supported by all the major browsers, JavaScript is the language of choice on the Web. It can even be used outside web applications to automate administrative tasks, for example.
This book aims to teach you all you need to know to start experimenting with JavaScript: what it is, how it works, and what you can do with it. Starting from the basic syntax, youll move on to learn how to create powerful web applications. Dont worry if youve never programmed before this book will teach you all you need to know, step by step. Youll find that JavaScript can be a great introduction to the world of programming: with the knowledge and understanding that youll gain from this book, youll be able to move on to learn newer and more advanced technologies in the world of computing.