Giáo án - Bài giảng: JAVASCRIPT EXAMPLES BIBLE - Pdf 13

100%
COMPREHENSIVE
AUTHORITATIVE
WHAT YOU NEED
Immerse yourself in
a JavaScript master's
code examples and
analysis
Learn JavaScript and
DOM behavior via
interactive labs
The perfect complement^) your 4th or
Gold Edition JavaScript Bible
Complete your
JavaScript Bible
library with this
invaluable
supplement
JavaScript
^Example!
CD-ROM
INSIDE!
Over 300 Ready-to-Run
Example Scripts and More
on CD-ROM!
The Essential Companion
to Java Script™ Bible
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

—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
JavaScript
Examples Bible:
The Essential
Companion to
JavaScript Bible

JavaScript'
Examples Bible:
The Essential
Companion to
JavaScript Bible
Danny Goodman
Hungry Mindsu
Best-Selling Books • Digital Downloads • e-Books • Answer Networks • e-Newsletters • Branded Web Sites • e-Learning
Indianapolis, IN ♦ Cleveland, OH ♦ New York, NY
Published by:
Hungry Minds, Inc.
909 Third Avenue
New York, NY 10022
www.hungrymi nds.com
Copyright © 2001 Danny Goodman. All rights
reserved. No part of this book, including interior
design, cover design, and icons, may be reproduced
or transmitted in any form, by any means

Media Distributor, Inc. for Micronesia; by Chips
Computadoras S.A. de C.V. for Mexico; by Editorial
Norma de Panama S.A. for Panama; by American
Bookshops for Finland.
For general information on Hungry Minds’ products
and services 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.
For sales inquiries and resellers information,
including discounts, premium and bulk quantity
sales and foreign language translations please
contact our Customer Care department at
800-434-3422, fax 317-572-4002 or write to Hungry
Minds, Inc., Attn: Customer Care department, 10475
Crosspoint Boulevard, Indianapolis, IN 46256.
For information on licensing foreign or domestic
rights, please contact our Sub-Rights Customer
Care department at 212-884-5000.
For information on using Hungry Minds’ products
and services in the classroom or for ordering
examination copies, please contact our Educational
Sales department at 800-434-2086 or fax
317-572-4005.
For press review copies, author interviews, or
other publicity information, please contact our
Public Relations department at 317-572-3168 or fax
317-572-4168.
For authorization to photocopy items for
corporate, personal, or educational use, please
contact Copyright Clearance Center, 222 Rosewood

Acquisitions Editor
Sharon Cox
Project Editor
Neil Romanosky
Technical Editor
David Wall
Copy Editors
Jerelind Charles
Victoria Lee O’Malley
Editorial Manager
Colleen Totz
Project Coordinator
Regina Snyder
Graphics and Production Specialists
Gabriele McCann
Betty Schulte
Jeremey Unger
Erin Zeltner
Quality Control Technicians
Laura Albert
David Faust
Andy Hollandbeck
Permissions Editor
Laura Moss
Media Development Specialist
Greg Stephens
Media Development Coordinator
Marisa Pearman
Book Designer
Kurt Krames

included Adobe Acrobat versions of the books on the accompanying CD-ROM.
Although a significant compromise to ease of reading, it was possible to move some
of the book’s content to the CD-ROM and leave the most important parts on the
printed page. For the softcover 4th edition, reference chapters covering less-used or
advanced subjects were pulled from print; for the hardcover Gold edition, which
was longer and targeted more for professional scripters, the advanced chapters
were put back into the book (along with 15 additional chapters for that edition),
and the JavaScript tutorial was exiled to the CD-ROM.
But even after making the difficult decisions about which chapters could go to the
CD-ROMs, the page counts for both volumes were still excessive. Something else —
something big— had to go. The remaining bundle that could free us from the page
X
JavaScript Example Bible: The Essential Companion to JavaScript Bible
count devil was all of the Example sections from the reference vocabulary. By being
nondiscriminatory about these extractions— that is, extracting all of them instead
of only selected sections— we could convey to readers a consistent organizational
model.
In the end, the extracted Example sections from Parts III and IV found their way into
Appendix F on the CD-ROMs of both editions of the larger tome. I knew that as a
reader of my own books (and one of a certain age at that) I would not enjoy having
to flip back and forth between book and screen to refresh my memory about a term
and see it in action. A more pleasing solution for many JavaScript Bible readers
would be a separate volume containing a printed version of the Examples sections.
The new volume would act as a companion to both the 4th and Gold editions of the
JavaScript Bible.
Using Appendix F as a starting point, I divided the content into chapters along the
same lines as the JavaScript Bible reference sections. This also gave me a chance to
study the examples for each chapter with fresh eyes. The examples haven’t
changed, but I had the opportunity to direct the reader’s attention to examples that
I thought were particularly helpful in mastering a document-level or core language

be looking at an example listing of this book and wish to consult the more detailed
discussion of the subject in the large book, turn to the corresponding big book
chapter and locate the corresponding terminology within the object’s chapter.
Many examples throughout this book refer to The Evaluator. This Web page applica
tion is described at length in Chapter 13 of the big books. You can find the file for
The Evaluator within the Listings\Chapl3 folder on the CD-ROM for either the big
book or this book.
CD-ROM
The accompanying CD-ROM contains the complete set of over 300 ready-to-run
HTML documents from the JavaScript Bible, Gold Edition. These include listings for
both the Examples sections in this book and all other listings from the Gold edition.
You can run these examples with your JavaScript-enabled browser, but be sure to
use the i ndex. html page in the Listings folder as a gateway to running the listings.
This page shows you the browsers that are compatible with each example listing.
The Quick Reference from Appendix A of the big books is in .pdf format on the
CD-ROM for you to print out and assemble as a handy reference, if desired. Adobe
Acrobat Reader is included on the CD-ROM so that you can read this .pdf file. Finally,
the text of the book is in a .pdf file format on the CD-ROM for easy searching.
Formatting and Naming Conventions
The script listings and words in this book are presented in a monospace font to
set them apart from the rest of the text. Because of restrictions in page width, lines
of script listings may, from time to time, break unnaturally. In such cases, the
remainder of the script appears in the following line, flush with the left margin of
the listing, just as they would appear in a text editor with word wrapping turned on.
If these line breaks cause you problems when you type a script listing into a docu
ment yourself, I encourage you to access the corresponding listing on the CD-ROM
to see how it should look when you type it.
To make it easier to spot in the text when a particular browser and browser version
is required, most browser references consist of a two-letter abbreviation and a ver
sion number. For example, IE5 means Internet Explorer 5 for any operating system;


1
Chapter 2: Window and Frame Objects (Chapter 1 6 ) 127
Chapter 3: Location and History Objects (Chapter 1 7 ) 205
Chapter 4: The Document and Body Objects (Chapter 18) 223
Chapter 5: Body Text Objects (Chapter 1 9 )
265
Chapter 6: Image, Area, and Map Objects (Chapter 22) 317
Chapter 7: The Form and Related Objects (Chapter 2 3 ) 335
Chapter 8: Button Objects (Chapter 2 4 ) 343
Chapter 9: Text-Related Form Objects (Chapter 25) 357
Chapter 10: Select, Option, and Optgroup Objects (Chapter 2 6 ) 369
Chapter 11: Table and List Objects (Chapter 2 7 ) 381
Chapter 12: Navigator and Other Environment Objects (Chapter 2 8 ) 397
Chapter 13: Event Objects (Chapter 2 9 ) 409
Chapter 14: Style Sheet Objects (Chapter 3 0 ) 435
Chapter 15: The NN4 Layer Object (Chapter 31) 441
Chapter 16: String and Number Objects (Chapters 34 and 35)

469
Chapter 17: The Array Object (Chapter 3 7 )
487
Appendix: What’s on the CD-ROM 497
Index 499
End-User License Agreement 528
CD-ROM Installation Instructions 532
Contents
Preface ix
Acknowledgments xiii
Chapter 1: Generic HTML Element Objects (Chapter 1 5 ) 1

M eth o ds 219
Chapter 4: The D ocum ent and B ody O bjects (Chapter 1 8 )

223
Examples Highlights
224
Document Object 224
Pro p erties 224
M eth o ds 243
Event H a n dlers 256
BODY Element Ob ject 257
Prop erties 257
M eth od s 261
Event H a n dlers 262
Chapter 5: Body Text O bjects (Ch apter 1 9 ) 265
Examples Higlilighls 266
FONT Element O b jec t 266
Prop erties 266
HR Element Object 269
Prop erties 269
MARQUEE Element O b je c t 273
Prop erties 273
Methods 276
Range O b ject 276
Prop erties 276
M eth o ds 279
selection O b je c t 291
Prop erties 291
M eth o ds 292
Text and TextNode Objects 293

LABEL Element Object 342
P ro p e rty 342
Chapter 8: Button Objects (Chapter 2 4 )

343
Examples Highlights 343
The BUTTON Element Object and the Button, Submit, and
Resel Input Objecls 344
Prope rties
344
M eth od s 345
Event handlers 346
Checkbox Input O bje c t 347
Pro p erties 347
Event handlers 349
Radio Input O bject 352
P roperties 352
Event handlers 355
Chapter 9: Text-Related Form Objects (Chapter 2 5) 357
Examples Highlights 358
Text Input Object 358
Pro p erties 358
M eth od s 363
Event handlers 365
TEXTAREA Element O bject 368
Pro p erties 368
M eth od s 368
Chapter 10: Select, Option, and Optgroup Objects (Chapter 26) . . 369
Examples Highlights 370
SELECT Element Object 370

(Chapter 28) 397
Examples Highlights 398
clientlnformation Object (IE4+) and navigator Object (All)

398
Properties 398
M eth od s 405
screen O bject 407
P rop e rtie s 407
userProfile O bje ct 407
M eth od s 407
Chapter 13: Event Objects (Chapter 2 9)

409
Examples Highlights
410
NN4 event Object 410
Pro p erties 410
IE4+ event Object 413
Pro p erties 413
NN6+ event O bject 423
Chapter 14: Style Sheet Objects (Chapter 30)

435
Examples Highlights 435
styleSheet Object 436
Prope rties 436
M eth od s 438
cssRule and rule O b je c ts 440
Properties 440

byproduct of this concept is that object model designers find it
easier to implement their models according to genuinely object-
oriented principles. (In truth, modern HTML and DOM industry
standards encourage browser makers to think in object-oriented
terms anyway.) The object-oriented principle most applicable to
the way we work with objects is that all HTML elements inherit
properties, methods, and event handlers from a generic (and
unseen) HTML element object. Thus, specifications for any
HTML element object start with those of the generic object, and
then pile on element-specific features, such as the src property
of an IMG element. This chapter deals almost exclusively with
the properties, methods, and event handlers that all HTML
elements have in common.
Examples Highlights
♦ Modern object models and the scripting world now pay
much attention to the containment hierarchy of ele
ments and text nodes in a document. The function
shown in Listing 15-3 demonstrates how vital the
childNodes property is to scripts that need to inspect
(and then perhaps modify) page content.
♦ Element containment is also at the forefront in Listing
15-10, where W3C DOM syntax demonstrates how to use
the
f i rstChi 1 d
and
1 astChi 1 d
properties, plus the
insertBefore(),appendChi1d(
), and
replaceChi1d()

offset-related properties in both browsers to compare the results.
♦ Compare the IE5+ attachEventC) method and NN6 addEventLi stener()
method for modern ways to assign event handlers to element objects.
Although the method names are different, the two work identically.
♦ Observe how the getAttri bute ( ) method returns an object’s property value
when the property name is a string and the name is the same as an assigned
element attribute name. The getAttri bute () method is the prescribed way
to retrieve property values according to the W3C DOM.
♦ You can see how the getEl ementsByTagNameC) method returns an array of
nested elements with a particular tag. This is a great way, for example, to get a
one-dimensional array of all cells within a table.
♦ Spend time comparing how the various insert- and replace-related methods
operate from different points of view. In the IE world, most operate on the cur
rent element; in the W3C DOM world, the methods operate on child nodes of
the current element.
♦ For IE5+/Windows, check out the way dynamic properties are managed
through the getExpressi on(), setExpressi on(), and recal c() methods.
Listing 15-32 demonstrates a neat graphical clock that employs these methods.
♦ IE5+/Windows provides a number of event handlers, such as onBeforeCopy,
onBeforePaste, onCopy, onCut, and onPaste that let scripts manage the
specific information preserved in the clipboard. These event handlers can
also be used with the onCcntextMenu event handler to facilitate custom
context menus.
4- Another set of IE5+/Windows event handlers provides excellent control over
user dragging and dropping of elements on a page. Listing 15-37 is particularly
interesting in this regard.
♦ Listing 15-41 shows a cross-browser laboratory for understanding the three
keyboard events and how to get key and character information from the event.
You see event-handling that works with IE4+, NN4, and NN6 event models.
♦ Numerous mouse-related events belong to all HTML elements. Listings 15-42

document.all.myTable.accessKey = elem.value
)
return false
)
)
</SCRIPT>
</HEAD>
<B0DY>
<Hl>accessKey Property Lab</Hl>
<HR>
Setti ngs:<BR>
Continued
elementobject.accessKey
4
JavaScript Examples Bible: The Essential Companion to JavaScript Bible
Listing 15-1 (continued)
<F0RM NAME="input">
Assign an accessKey value to the Button below and press Return:
<INPUT TYPE="text” SIZE=2 MAXLENGTH=1
onKeyPress="return assignKeyt'button’, this)">
<BR>
Assign an accessKey value to the Text Box below and press Return:
<INPUT TYPE="text” SIZE=2 MAXLENGTH=1
onKeyPress="return assignKeyt'text', this)">
<BR>
Assign an accessKey value to the Table below (IE5.5 only) and press Return:
<INPUT TYPE="text” SIZE=2 MAXLENGTH=1
onKeyPress="return assignKeyt'table', this)”>
</F0RM>
<BR>


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status