Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

JavaScript Bible®Sixth EditionDanny Goodman with Michael MorrisonWith a foreword by Brendan pptx
Nội dung xem thử
Mô tả chi tiết
JavaScript®
Bible
Sixth Edition
Danny Goodman
with Michael Morrison
With a foreword by Brendan Eich, JavaScript’s creator
01_069165 ffirs.qxp 3/1/07 3:38 PM Page iii
more free ebooks download links:
http://www.ebook-x.com
A major new edition of the definitive
guide to JavaScript!
Make your Web pages stand out above the noise with
JavaScript and the expert instruction in this muchanticipated update to the bestselling JavaScript Bible.
With renowned JavaScript expert Danny Goodman
at your side, you’ll get a thorough grounding in
JavaScript basics, see how it fits with current Web
browsers, and find all the soup-to-nuts detail you’ll
need, whether you’re a veteran programmer or just
starting out. This is the JavaScript book Web developers
turn to again and again.
Shelving Category:
COMPUTERS/Internet/
Web Site Design
Reader Level:
Beginning to Advanced
$49.99 USA
$59.99 Canada
£31.99 UK
ISBN 978-0-470-06916-5
www.wiley.com/compbooks
• Master JavaScript fundamentals and write your first practical script
• Develop code for both single- and cross-platform audiences and
evolving standards
• Get the essentials of document object models and HTML element objects
• Write scripts that dynamically modify Web pages in response to
user actions
• Learn the power of new Ajax technologies to create efficient Web
page user interfaces
• Apply the latest JavaScript exception handling and custom object techniques
• Create interactivity with sites like Google Maps™
more free ebooks download links:
http://www.ebook-x.com
Praise for Danny Goodman’s JavaScript®Bible
“JavaScript® Bible is the definitive resource in JavaScript programming. I am never more than three feet
from my copy.”
—Steve Reich, CEO, PageCoders
“This book is a must-have for any web developer or programmer.”
—Thoma Lile, President, Kanis Technologies, Inc.
“Outstanding book. I would recommend this book to anyone interested in learning to develop advanced
Web sites. Mr. Goodman did an excellent job of organizing this book and writing it so that even a beginning programmer can understand it.”
—Jason Hensley, Director of Internet Services, NetVoice, Inc.
“Goodman is always great at delivering clear and concise technical books!”
—Dwayne King, Chief Technology Officer, White Horse
“JavaScript® Bible is well worth the money spent!”
—Yen C.Y. Leong, IT Director, Moo Mooltimedia, a member of SmartTransact Group
“A must-have book for any internet developer.”
—Uri Fremder, Senior Consultant, TopTier Software
“I love this book! I use it all the time, and it always delivers. It’s the only JavaScript book I use!”
—Jason Badger, Web Developer
“Whether you are a professional or a beginner, this is a great book to get.”
—Brant Mutch, Web Application Developer, Wells Fargo Card Services, Inc.
“I never thought I’d ever teach programming before reading your book [JavaScript® Bible]. It’s so simple to
use—the Programming Fundamentals section brought it all back! Thank you for such a wonderful book,
and for breaking through my programming block!”
—Susan Sann Mahon, Certified Lotus Instructor, TechNet Training
“Danny Goodman is very good at leading the reader into the subject. JavaScript® Bible has everything we
could possibly need.”
—Philip Gurdon
“An excellent book that builds solidly from whatever level the reader is at. A book that is both witty and
educational.”
—Dave Vane
“I continue to use the book on a daily basis and would be lost without it.”
—Mike Warner, Founder, Oak Place Productions
“JavaScript® Bible is by far the best JavaScript resource I’ve ever seen (and I’ve seen quite a few).”
—Robert J. Mirro, Independent Consultant, RJM Consulting
01_069165 ffirs.qxp 3/1/07 3:38 PM Page i
more free ebooks download links:
http://www.ebook-x.com
01_069165 ffirs.qxp 3/1/07 3:38 PM Page ii
more free ebooks download links:
http://www.ebook-x.com
JavaScript®
Bible
Sixth Edition
Danny Goodman
with Michael Morrison
With a foreword by Brendan Eich, JavaScript’s creator
01_069165 ffirs.qxp 3/1/07 3:38 PM Page iii
more free ebooks download links:
http://www.ebook-x.com
JavaScript® Bible, Sixth Edition
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Danny Goodman
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-06916-5
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
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 Legal
Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317)
572-4355, 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 WEBSITE 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 WEBSITE MAY
PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET
WEBSITES 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 or to obtain technical support, please contact our Customer
Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Cataloging-in-Publication Data
Goodman, Danny.
JavaScript bible / Danny Goodman with Michael Morrison ; with a foreword by Brendan Eich. — 6th ed.
p. cm.
Includes index.
ISBN-13: 978-0-470-06916-5 (paper/cd-rom)
ISBN-10: 0-470-06916-3 (paper/cd-rom)
1. JavaScript (Computer program language) I. Morrison, Michael, 1970– II. Title.
QA76.73.J39G65 2007
005.13'3—dc22 2006101137
Trademarks: Wiley, the Wiley logo, 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 Inc. in the United States and other countries. 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.
01_069165 ffirs.qxp 3/1/07 3:38 PM Page iv
more free ebooks download links:
http://www.ebook-x.com
About the Authors
Danny Goodman is the author of numerous critically acclaimed and best-selling books, including The
Complete HyperCard Handbook, Danny Goodman’s AppleScript Handbook, Dynamic HTML: The Definitive
Reference, and JavaScript & DHTML Cookbook. He is a renowned authority and expert teacher of computer
scripting languages. His writing style and pedagogy continue to earn praise from readers and teachers
around the world. To help keep his finger on the pulse of real-world programming challenges, Goodman
frequently lends his touch as consulting programmer and designer to leading-edge World Wide Web and
intranet sites from his home base in the San Francisco area.
Michael Morrison is a writer, developer, toy inventor, and author of a variety of books covering topics such
as Java, C++, Web scripting, XML, game development, and mobile devices. Some of Michael’s notable writing projects include Faster Smarter HTML and XML, Teach Yourself HTML & CSS in 24 Hours, and Beginning
Game Programming. Michael is also the founder of Stalefish Labs (www.stalefishlabs.com), an entertainment company specializing in unusual games, toys, and interactive products.
01_069165 ffirs.qxp 3/1/07 3:38 PM Page v
more free ebooks download links:
http://www.ebook-x.com
Credits
Acquisitions Editor
Kit Kemper
Senior Development Editor
Kevin Kent
Copy Editor
Travis Henderson
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group
Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Project Coordinator
Lynsey Osborn
Graphics and Production Specialists
Brooke Graczyk
Joyce Haughey
Jennifer Mayberry
Alicia B. South
Quality Control Technicians
David Faust
John Greenough
Media Development Project Supervisor
Laura Atkinson
Media Development Specialist
Kate Jenkins
Proofreading
David Faust
Kathy Simpson
Sossity Smith
Indexing
Valerie Haynes Perry
Anniversary Logo Design
Richard Pacifico
01_069165 ffirs.qxp 3/1/07 3:38 PM Page vi
more free ebooks download links:
http://www.ebook-x.com
This sixth edition is the second time I’ve been fortunate enough to have Michael Morrison—a firstrate author and scripter in his own right—help bring the content of the book up to date. When
you add the hundreds of pages on the CD-ROM to the 1,200+ pages of the printed book, the job
of revising JavaScript Bible is monumental in scale. I therefore appreciate the personal sacrifices Michael
made while he kept the motor running during extensive revision cycles. Many thanks to the hardworking folks at Wiley Publishing, Kit Kemper and Kevin Kent. Above all, I want to thank the many
readers of the earlier editions of this book for investing in this ongoing effort. I wish I had the space here
to acknowledge by name so many who have sent e-mail notes and suggestions: Your input has been
most welcome and greatly appreciated.
vii
01_069165 ffirs.qxp 3/1/07 3:38 PM Page vii
more free ebooks download links:
http://www.ebook-x.com
01_069165 ffirs.qxp 3/1/07 3:38 PM Page viii
more free ebooks download links:
http://www.ebook-x.com
About the Authors ........................................................................................................................v
Acknowledgments ......................................................................................................................vii
Foreword ..................................................................................................................................xvii
Preface........................................................................................................................................xix
Part I: Getting Started with JavaScript 1
Chapter 1: JavaScript’s Role in the World Wide Web and Beyond . . . . . . . 3
Competing for Web Traffic ............................................................................................................4
Other Web Technologies ..............................................................................................................4
JavaScript: A Language for All ......................................................................................................6
JavaScript: The Right Tool for the Right Job ..................................................................................8
Chapter 2: Authoring Challenges Amid the Browser Wars . . . . . . . . . . . 9
Leapfrog ........................................................................................................................................9
Duck and Cover ..........................................................................................................................10
Compatibility Issues Today..........................................................................................................11
Developing a Scripting Strategy ..................................................................................................14
Chapter 3: Your First JavaScript Script . . . . . . . . . . . . . . . . . . . . . 17
The Software Tools ......................................................................................................................17
Setting Up Your Authoring Environment ....................................................................................18
What Your First Script Will Do....................................................................................................21
Entering Your First Script............................................................................................................21
Examining the Script ..................................................................................................................23
Have Some Fun ..........................................................................................................................25
Part II: JavaScript Tutorial 27
Chapter 4: Browser and Document Objects . . . . . . . . . . . . . . . . . . 29
Scripts Run the Show ..................................................................................................................29
When to Use JavaScript ..............................................................................................................30
The Document Object Model ......................................................................................................31
When a Document Loads ............................................................................................................33
Object References........................................................................................................................36
Node Terminology ......................................................................................................................38
What Defines an Object? ............................................................................................................39
Exercises ....................................................................................................................................43
ix
02_069165 ftoc.qxp 3/1/07 4:02 PM Page ix
more free ebooks download links:
http://www.ebook-x.com
Chapter 5: Scripts and HTML Documents . . . . . . . . . . . . . . . . . . . 45
Where Scripts Go in Documents ................................................................................................45
JavaScript Statements ..................................................................................................................49
When Script Statements Execute ................................................................................................49
Viewing Script Errors ..................................................................................................................52
Scripting versus Programming ....................................................................................................54
Exercises ....................................................................................................................................55
Chapter 6: Programming Fundamentals, Part I. . . . . . . . . . . . . . . . . 57
What Language Is This? ..............................................................................................................57
Working with Information ..........................................................................................................57
Variables......................................................................................................................................58
Expressions and Evaluation ........................................................................................................60
Data Type Conversions ................................................................................................................62
Operators ....................................................................................................................................64
Exercises ....................................................................................................................................65
Chapter 7: Programming Fundamentals, Part II . . . . . . . . . . . . . . . . 67
Decisions and Loops....................................................................................................................67
Control Structures ......................................................................................................................68
About Repeat Loops ....................................................................................................................69
Functions ....................................................................................................................................70
About Curly Braces ....................................................................................................................74
Arrays..........................................................................................................................................74
Exercises ....................................................................................................................................78
Chapter 8: Window and Document Objects . . . . . . . . . . . . . . . . . . 81
Top-Level Objects........................................................................................................................81
The window Object ....................................................................................................................82
Window Properties and Methods ................................................................................................85
The location Object ....................................................................................................................87
The navigator Object ..................................................................................................................88
The document Object..................................................................................................................88
Exercises ....................................................................................................................................93
Chapter 9: Forms and Form Elements. . . . . . . . . . . . . . . . . . . . . . 95
The form Object..........................................................................................................................95
Form Controls as Objects............................................................................................................97
Passing Form Data and Elements to Functions ..........................................................................104
Submitting and Prevalidating Forms..........................................................................................106
Exercises ..................................................................................................................................108
Chapter 10: Strings, Math, and Dates . . . . . . . . . . . . . . . . . . . . . 109
Core Language Objects..............................................................................................................109
String Objects............................................................................................................................110
The Math Object ......................................................................................................................113
The Date Object ........................................................................................................................114
Date Calculations ......................................................................................................................115
Exercises ..................................................................................................................................117
x
Contents
02_069165 ftoc.qxp 3/1/07 4:02 PM Page x
more free ebooks download links:
http://www.ebook-x.com
Chapter 11: Scripting Frames and Multiple Windows . . . . . . . . . . . . 119
Frames: Parents and Children ..................................................................................................119
References Among Family Members ..........................................................................................121
Frame-Scripting Tips ................................................................................................................123
About iframe Elements ..............................................................................................................124
Controlling Multiple Frames: Navigation Bars ..........................................................................124
References for Multiple Windows..............................................................................................126
Exercises ..................................................................................................................................128
Chapter 12: Images and Dynamic HTML . . . . . . . . . . . . . . . . . . . 129
The Image Object......................................................................................................................129
Rollovers Without Scripts..........................................................................................................135
The javascript: Pseudo-URL ......................................................................................................137
Popular Dynamic HTML Techniques ........................................................................................138
Exercises ..................................................................................................................................140
Part III: Document Objects Reference 141
Chapter 13: JavaScript Essentials . . . . . . . . . . . . . . . . . . . . . . . 143
JavaScript Versions ....................................................................................................................143
Core Language Standard: ECMAScript ......................................................................................144
Embedding Scripts in HTML Documents ..................................................................................145
Browser Version Detection ........................................................................................................149
Designing for Compatibility ......................................................................................................154
Language Essentials for Experienced Programmers....................................................................158
Onward to Object Models ........................................................................................................161
Chapter 14: Document Object Model Essentials . . . . . . . . . . . . . . . 163
The Object Model Hierarchy ....................................................................................................163
How Document Objects Are Born ............................................................................................166
Object Properties ......................................................................................................................166
Object Methods ........................................................................................................................167
Object Event Handlers ..............................................................................................................168
Object Model Smorgasbord ......................................................................................................169
Basic Object Model....................................................................................................................169
Basic Object Model Plus Images ................................................................................................170
Navigator 4–Only Extensions....................................................................................................170
Internet Explorer 4+ Extensions ................................................................................................171
Internet Explorer 5+ Extensions ................................................................................................174
The W3C DOM ........................................................................................................................175
Scripting Trends ........................................................................................................................190
Standards Compatibility Modes (DOCTYPE Switching) ............................................................191
Where to Go from Here ............................................................................................................192
Chapter 15: Generic HTML Element Objects. . . . . . . . . . . . . . . . . 195
Generic Objects ........................................................................................................................196
xi
Contents
02_069165 ftoc.qxp 3/1/07 4:02 PM Page xi
more free ebooks download links:
http://www.ebook-x.com
Chapter 16: Window and Frame Objects . . . . . . . . . . . . . . . . . . . 369
Window Terminology................................................................................................................369
Frames ......................................................................................................................................370
window Object..........................................................................................................................376
frame Element Object................................................................................................................471
frameset Element Object ..........................................................................................................478
iframe Element Object ..............................................................................................................484
popup Object............................................................................................................................490
Chapter 17: Location and History Objects . . . . . . . . . . . . . . . . . . 495
location Object..........................................................................................................................496
history Object ..........................................................................................................................513
Chapter 18: The Document and Body Objects . . . . . . . . . . . . . . . . 519
document Object ......................................................................................................................520
body Element Object ................................................................................................................587
TreeWalker Object ....................................................................................................................595
Chapter 19: Link and Anchor Objects. . . . . . . . . . . . . . . . . . . . . 599
Anchor, Link, and a Element Objects ........................................................................................600
Chapter 20: Image, Area, Map, and Canvas Objects . . . . . . . . . . . . . 607
Image and img Element Objects................................................................................................607
area Element Object ..................................................................................................................626
map Element Object..................................................................................................................630
canvas Element Object ..............................................................................................................634
Chapter 21: The Form and Related Objects. . . . . . . . . . . . . . . . . . 645
The Form in the Object Hierarchy ............................................................................................645
form Object ..............................................................................................................................646
fieldset and legend Element Objects..........................................................................................663
label Element Object ................................................................................................................665
Scripting and Web Forms 2.0....................................................................................................666
Chapter 21: Button Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 669
The button Element Object, and the Button, Submit, and Reset Input Objects..........................669
checkbox Input Object..............................................................................................................675
radio Input Object ....................................................................................................................682
image Input Object ..................................................................................................................688
Chapter 23: Text-Related Form Objects. . . . . . . . . . . . . . . . . . . . 691
Text Input Object ......................................................................................................................691
password Input Object ..............................................................................................................706
hidden Input Object..................................................................................................................707
textarea Element Object ............................................................................................................708
Chapter 24: Select, Option, and FileUpload Objects . . . . . . . . . . . . 713
select Element Object................................................................................................................713
option Element Object ..............................................................................................................732
optgroup Element Object ..........................................................................................................734
file Input Element Object ..........................................................................................................736
xii
Contents
02_069165 ftoc.qxp 3/1/07 4:02 PM Page xii
more free ebooks download links:
http://www.ebook-x.com