JavaScript
A Beginners Guide
Fourth Edition
John Pollock
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
Copyright 2013 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher.
ISBN: 9780071809382
MHID: 0071809384
The material in this e-book also appears in the print version of this title: ISBN: 978-0-07-180937-5, MHID: 0-07-180937-6
McGraw-Hill e-books are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative, please e-mail us at .
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.
Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information.
TERMS OF USE
This is a copyrighted work and McGraw-Hill and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hills prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED AS IS. THE McGRAW-HILL COMPANIES AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
To my wife Heather, daughters Eva and Elizabeth, Bruce and Joy Anderson, and Dr. J. D. and Linda Andrews
In memory of James D. and Livian Anderson, John William and Edith Hopkins, Burley T. and Aline Price, Doc Flores, and Clifton Idom
About the Author
John Pollock is employed as a Web Administrator during the day and works on Web sites and other projects during the evening. He runs a Web site devoted to Web development and design: Script the Web (www.scripttheweb.com). He also is a contributor to Web Xpertz (www.webxpertz.net), a help community for Web developers. John holds a Bachelor of Arts in English from Sam Houston State University and currently lives in Huntsville, Texas with his wife, Heather, and his daughters, Eva and Elizabeth.
About the Technical Editor
Christie Sorenson is a senior software engineer at ZingChart. She has worked on JavaScript-based systems in analytics, content management, and business applications since 1997 and has been fascinated with the evolution of the language and its users. She has collaborated on several books, including Ajax: The Complete Reference and HTML & CSS: The Complete Reference and was also the tech editor on JavaScript: The Complete Reference and HTML: A Beginners Guide. She holds a Bachelor of Science in Computer Science from University of California, San Diego, and now lives in San Francisco with her husband, Luke, and daughters, Ali and Keira.
Contents at a Glance
Contents
TheElement
Acknowledgments
I would like to begin by thanking my wonderful wife, Heather Pollock, for all of her love, support, and encouragement in all I do. I love you! I would also like to thank my two daughters, Eva and Elizabeth. I love both of you!
I would like to thank my parents, Bruce and Joy Anderson, for their love and guidance, and for always supporting my endeavors.
I would like to thank Dr. J. D. and Linda Andrews for their love, guidance, and support.
In addition I would like to thank John and Betty Hopkins (grandparents), James D. and Livian Anderson (grandparents), Clifton and Juanita Idom (grandparents), Richard Pollock (brother) and family, Misty Castleman (sister) and family, Warren Anderson (brother) and family, Jon Andrews (brother) and family, Lisa and Julian Owens (aunt/uncle) and family, and every aunt, uncle, cousin, or other relation in my family. All of you have been a great influence in my life.
I would like to thank all of my editors at McGraw-Hill/Professional for their outstanding help and support throughout the writing of this book. Thanks to Brandi Shailer, Ryan Willard, Amanda Russell, and to all of the editors who worked on each edition of the book.
Thanks to my technical editor, Christie Sorenson, for editing and checking over all of the technical aspects of the book, and helping me provide clear explanations of the topics that are covered.
I would like to thank God for the ability He has given me to help and teach people by my writing. In all your ways acknowledge Him, and He shall direct your paths. (Proverbs 3:6).
Introduction
W elcome to JavaScript: A Beginners Guide, Fourth Edition ! Years ago, I was surfing the Web and noticed that people were publishing pages about themselves and calling them homepages. After viewing a number of these, I decided to create a homepage myself. I had no idea where to begin but, through trial and error, I figured out how to code HTML and publish my documents on a Web server. Over time, I saw some interesting effects used on other homepages (like alert messages that popped up out of nowhere or images that would magically change when I moved my mouse over them). I was curious and just had to know what was being done to create those effects. Were these page creators using HTML tags I did not know about?
Eventually, one site revealed what they were using to create those effects: JavaScript. I went in search of information on it, and came across a few tutorials and scripts on the Web. Since I had programmed in other languages (such as a relatively obscure language called Ada), I was able to catch on to JavaScript fairly quickly by looking at these tutorials and scripts.