O'Reilly - Head First Javascript.pdf

(51807 KB) Pobierz
O'Reilly - Head First Javascript
80479222.004.png
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 corporate@oreilly.com .
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
...but m y koi fi sh
couldn’ t care l ess.
Indexer:
Julie Hawks
Page Viewers:
Masheed Morrison (wife), family, and pet fish
Printing History:
December 2007: First Edition.
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.
TTM
This book uses RepKover , a durable and fl exible lay-fl at binding.
ISBN-10: 0-596-52774-8
ISBN-13: 978-0-596-52774-7
[M]
80479222.005.png 80479222.006.png 80479222.007.png
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
table of contents
the interactive web
1 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 .
(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 <script> tag to tell the browser you’re writing JavaScript
11
Your web browser can handle HTML, CSS, AND JavaScript
12
Man’s virtual best friend... needs YOUR help
15
Making iRock interactive
16
Create the iRock web page
17
Test drive
17
JavaScript events: giving the iRock a voice
18
Alerting the user with a function
19
Add the iRock greeting
20
Now let’s make the iRock really interactive
22
Interaction is TWO-way communication
23
Add a function to get the user’s name
24
Instant replay: what just happened?
27
Test drive iRock 1.0
28
x
Reacting to the Virtual World
80479222.001.png
table of contents
storing data
2 In the real world, people often overlook the importance of
having a place to store all their stuff. Not so in JavaScript. You simply
don’t have the luxury of walk-in closets and three-car garages. In JavaScript, everything
has its place , and it’s your job to make sure of it. The issue is data —how to represent it ,
how to store it , and how to find it once you’ve put it somewhere. As a JavaScript storage
specialist, you’ll be able to take a cluttered room of JavaScript data and impose your will
on it with a flurry of virtual labels and storage bins.
Your scripts can store data
34
Scripts think in data types
35
Constants stay the SAME, variables can CHANGE
40
Variables start out without a value
44
Initialize a variable with “=”
45
Constants are resistant to change
46
What’s in a name?
50
Legal and illegal variable and constant names
51
Variable names often use CamelCase
52
Plan the Duncan’s Donuts web page
56
A first take at the donut calculations
58
Initialize your data...or else
61
NaN is NOT a number
62
You can add more than numbers
64
parseInt() and parseFloat() convert text to a number
65
Why are extra donuts being ordered?
66
Duncan discovers donut espionage
70
Use getElementById() to grab form data
71
Validate the web form’s data
72
Strive for intuitive user input
77
xi
Everything Has Its Place
80479222.002.png 80479222.003.png
Zgłoś jeśli naruszono regulamin