www.it-ebooks.info
www.it-ebooks.info Head First JavaScript by Michael Morrison Copyright © 2008 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or
[email protected].
Series Creators:
Kathy Sierra, Bert Bates
Series Editor:
Brett D. McLaughlin
Design Editor:
Louise Barr
Cover Designers:
Louise Barr, Steve Fehler
Production Editor:
Sanders Kleinfeld
Proofreader:
Colleen Gorman
Indexer:
Julie Hawks
Page Viewers:
Masheed Morrison (wife), family, and pet fish
...but my koi fish couldn’t care less.
Printing History: December 2007: First Edition.
My family knows how to celebrate a book release... The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations, Head First JavaScript, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and the author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. No rocks, stick figures, cube puzzles, or macho moviegoers were harmed in the making of this book. Just me, but I can handle it...I’m wiry. TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN-10: 0-596-52774-8 ISBN-13: 978-0-596-52774-7 [M]
www.it-ebooks.info table of contents
Table of Contents (Summary)
Intro
xxiii
1
the interactive web: Reacting to the Virtual World
1
2
storing data: Everything Has Its Place
33
3
exploring the client: Browser Spelunking
85
4
decision making: If There’s a Fork in the Road, Take It
135
5
looping: At the Risk of Repeating Myself
189
6
functions: Reduce, Reuse, Recycle
243
7
forms and validation: Getting the User to Tell All
289
8
wrangling the page: Slicing and Dicing HTML with the DOM
343
9
bringing data to life: Objects as Frankendata
393
10
creating custom objects: Having It Your Way with Custom Objects
449
11
kill bugs dead: Good Scripts Gone Wrong
485
12
dynamic data: Touchy-Feely Web Applications
537
Table of Contents (the real thing) Intro Your brain on JavaScript. You’re sitting around trying to learn something, but your brain keeps telling you all that learning isn’t important. Your brain’s saying, “Better leave room for more important things, like which wild animals to avoid and whether naked water skiing is a bad idea.” So how do you trick your brain into thinking that your life really depends on learning JavaScript?
Who is this book for?
xxiv
We know what you’re thinking
xxv
Metacognition
xxvii
Bend your brain into submission
xxix
Read me
xxx
The technical review team
xxxii
Acknowledgments
xxxiii
ix
www.it-ebooks.info table of contents
1
the interactive web Reacting to the Virtual World Tired of thinking of the Web in terms of passive pages? Been there, done that. They’re called books. And they’re good for reading, learning... lots of good things. But they’re not interactive. And neither is the Web without a little help from JavaScript. Sure, you can submit a form and maybe do a trick here and there with some crafty HTML and CSS coding, but you’re really just playing Weekend at Bernie’s propping up a lifeless web page. Real live interactivity requires a bit more smarts and a little more work... but it has a much bigger payoff.
x
(Online) people have needs
2
Like talking to a brick wall... nothing happens
3
But JavaScript talks back
4
Lights, camera, interaction!
6
Use the