Tải bản đầy đủ (.pdf) (1,198 trang)

sams javascript, developer's dictionary (2002)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.38 MB, 1,198 trang )

800 East 96th Street, Indianapolis, Indiana 46240
DEVELOPER’S DICTIONARY
JavaScript
ALEXANDER J. VINCENT
00 0672322013 FM 5/3/02 1:31 PM Page i
JavaScript Developer’s Dictionary
Copyright  2002 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from the
publisher. No patent liability is assumed with respect to the use of the
information contained herein. Although every precaution has been taken in
the preparation of this book, the publisher and author assume no responsi-
bility for errors or omissions. Neither is any liability assumed for damages
resulting from the use of the information contained herein.
International Standard Book Number: 0-672-32201-3
Library of Congress Catalog Card Number: 2001094226
Printed in the United States of America
First Printing: June 2002
04 03 02 6543
Trademarks
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to
the accuracy of this information. Use of a term in this book should not be
regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is on
an “as is” basis. The authors and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages


arising from the information contained in this book.
Acquisitions Editors
Patricia Barnes
Shelley Johnston
Development Editor
Jonathan Steever
Managing Editor
Charlotte Clapp
Project Editors
Sheila Schroeder
Tricia Liebig
Copy Editor
Kate Givens
Indexer
Becky Hornyak
Proofreader
Linda Seifert
Technical Editors
Martin Honnen
Jason Byars
Andrew Watt
Team Coordinator
Amy Patton
Interior Design
Gary Adair
Cover Design
Alan Clements
Production
Ayanna Lacey
00 0672322013 FM 5/3/02 1:31 PM Page ii

Contents at a Glance
Introduction 1
Part I Core JavaScript 7
1 Object( ) 9
2 Function( ) 37
3 Array( ) 57
4 String( ) 77
5 Boolean( ) 103
6 Date( ) 111
7 Number( ) 137
8 Math 157
9 RegExp( ) 171
10 Error( ) 201
11 The Global Object and Statements 213
12 Operators 237
13 JavaScript Syntax 255
14 Conditional Compilation in Internet Explorer 265
Part II Window and Client Objects 273
15 window 275
16 location 363
17 history 375
18 navigator 381
19 screen 407
Part III Document Object Model for HTML Documents 415
20 Core DOM Objects 417
21 HTMLElement 477
22 HTMLHtmlElement 509
23 HTMLDocument/document 515
24 Head Elements 561
00 0672322013 FM 5/3/02 1:31 PM Page iii

iv
JAVASCRIPT DEVELOPER’S DICTIONARY
25 Form Elements 579
26 Form Input Elements: HTMLInputElement 649
27 List Elements 667
28 Text Formatting Elements 691
29 Table Elements 777
30 Image Elements 835
31 Programmable Elements 863
32 DOM-2 Events and Event Handlers 907
33 Styling for HTML Elements 985
34 DOM-2 Range 1017
35 Cookies 1057
Part IV The Future 1069
36 XML-Related Technologies and Their DOMs 1071
Index
1099
00 0672322013 FM 5/3/02 1:31 PM Page iv
Table of Contents
Introduction 1
Part I Core JavaScript 7
1Object( ) 9
Description 10
Creating a New Object
10
Getters and Setters of Netscape 6
11
Adding Properties and Methods to an Object
13
Objects Are Typically Unique

13
Returning New Objects Outside of a Function
13
Preparing Your Objects for Others
15
Most Objects You Create Have a Parent Object
17
Properties
18
Methods
21
Example: Creating a Persistent Reference to
this 32
2Function( ) 37
Description 38
Executing a Function
38
Declaring Objects Inside Functions
39
Browser Differences in Properties Returned
39
Functions Defined Within Functions
40
Functions Can Call Themselves: Recursion
40
Exiting from a Function Gracefully
43
The
return Statement Exits a Function 45
Properties

46
Methods
50
Example: Using the Function( ) Objects’ Source Codes
53
3Array( ) 57
Description 58
Numbered and Unnumbered Properties of Arrays
59
An Array’s Elements Can Be Any Object or Value
60
Populating an Array Automatically
61
00 0672322013 FM 5/3/02 1:31 PM Page v
vi
JAVASCRIPT DEVELOPER’S DICTIONARY
Properties 61
Methods
63
Example: An Extensible
switch Function 73
4String( ) 77
Description 78
HTML and Strings: The Connection
79
JavaScript and Strings: The Other Connection
79
String HTML-Related Methods Are Not XHTML-Compliant
80
Errata in JScript Documentation Regarding Strings

81
Properties
82
Methods
82
Example: Strings in HTML
99
5Boolean( ) 103
Description 104
Defining Boolean( ) Objects by Comparison
104
Defining Boolean( ) Object Values by the Boolean( ) Function
105
Properties
106
Methods
107
Example: Shorthand for an if Statement
108
6Date( ) 111
Description 112
A Special Number Line for Time
112
JavaScript’s Interpretation of This Timeline
113
Determining a Specific Date
113
Instances of Date( ) Obey the Rules of Number Arithmetic
114
Properties

115
Methods
115
Example: Detecting Daylight Savings Time
134
7Number( ) 137
Description 138
Numbers Are Everywhere in JavaScript
138
JavaScript Supports Scientific Notation
141
Accessing Properties and Methods of Number Literals
141
The Number( ) Function Performs a Type Change
142
00 0672322013 FM 5/3/02 1:31 PM Page vi
vii
CONTENTS
Properties 143
Methods
146
Example: Implementing Complex Numbers in JavaScript
149
8Math 157
Description 157
Properties
158
Methods
161
Example: A Logarithm Function for Multiple Bases

168
9RegExp( ) 171
Description 173
The Special Characters of Regular Expressions
173
The Literal Notation of RegExp( ) Objects
178
The Flags of Regular Expressions
183
Properties
185
Methods
194
Example: Regular Expressions and Text Exercises
197
10 Error( ) 201
Description 202
Properties
203
Methods
208
Example: Creating and Using Errors
209
11 The Global Object and Statements 213
Top-Level Objects 214
Top-Level Functions
215
Statements
221
Controlling How a Script Runs

233
12 Operators 237
The Primary Assignment Operator 238
Arithmetic Operators
238
Comparison Operators
242
Boolean Operators
245
Bitwise Operators
247
Word Operators
250
00 0672322013 FM 5/3/02 1:31 PM Page vii
viii
JAVASCRIPT DEVELOPER’S DICTIONARY
13 JavaScript Syntax 255
Parentheses, Square Brackets, and Curly Braces 256
Parentheses ( )
256
Square Brackets [ ]
257
Curly Braces { }
257
Quote Marks
258
Semicolons, Colons, Commas, and Periods
259
Semicolons ;
259

Commas ,
260
Colons :
260
Periods .
260
Literals
261
ASCII and Unicode
262
Comment Lines //, /* */
262
14 Conditional Compilation in Internet Explorer 265
Description 266
Statements
267
Objects
269
Compatibility with Netscape Browsers
270
Part II Window and Client Objects 273
15 window 275
Description 276
Common
window Object Names 276
Order of Operations in Loading Scripts
279
Sending Objects and Values Between Windows
283
The

window Object, Event Handlers, and the W3C DOM 285
Windows Have Two Names
285
Properties
286
Methods
306
Event Handlers
338
HTMLFrameSetElement /<frameset> </frameset>
339
Description
340
Properties
341
Methods
344
Event Handlers
344
00 0672322013 FM 5/3/02 1:31 PM Page viii
ix
CONTENTS
HTMLFrameElement/<frame /> 345
Description
346
Methods
353
Event Handlers
354
HTMLIFrameElement/<iframe />

354
Description
355
Properties
355
Methods
357
Event Handlers
357
Example: A Basket of Windows
358
16 location 363
Description 364
Properties
364
Methods
369
Example: Sending Information from One Page to Another
370
17 history 375
Description 376
Properties
376
Methods
377
Example: Moving a Separate Frame Back
378
18 navigator 381
Description 382
Browser Sniffing, the Incorrect Way

382
Properties
384
Methods
396
Example: Browser Sniffing, the Correct Way
397
19 screen 407
Description 408
Properties
408
Example: Redirecting Based on Screen Resolution
413
00 0672322013 FM 5/3/02 1:31 PM Page ix
x
JAVASCRIPT DEVELOPER’S DICTIONARY
Part III Document Object Model for HTML Documents 415
20 Core DOM Objects 417
Attr 419
Description
419
Properties
419
Methods
421
CDATASection
421
Description
421
Properties

422
Methods
422
CharacterData
422
Description
422
Properties
422
Methods
423
Example: Manipulating Character Data
425
Comment
426
Description
426
Properties
426
Methods
427
Document
427
Description
427
Properties
427
Methods
429
Example: Indenting All Paragraphs, the Hard Way

434
DocumentFragment
435
Description
436
Properties
436
Methods
436
Example: Pasting a Series of Nodes Repeatedly
436
DocumentType
437
Description
438
Properties
438
Methods
440
DOMImplementation
440
Description
440
Properties
441
Methods
441
00 0672322013 FM 5/3/02 1:31 PM Page x
xi
CONTENTS

Element 442
Description
442
Properties
443
Methods
443
Example: Descendant Nodes (Not Just Child Nodes)
448
Entity
449
Description
450
Properties
450
Methods
451
EntityReference
451
Description
452
Properties
452
Methods
452
Node
452
Properties
453
Methods

460
Example: Navigating a Tree of Nodes
463
NamedNodeMap
466
Properties
466
Methods
467
NodeList
469
Properties
470
Methods
470
Notation
470
Description
471
Properties
471
Methods
472
ProcessingInstruction
472
Description
472
Properties
472
Methods

473
Te x t
473
Description
473
Properties
474
Methods
474
Example: Inserting Hypertext Inside Plaintext
474
00 0672322013 FM 5/3/02 1:31 PM Page xi
xii
JAVASCRIPT DEVELOPER’S DICTIONARY
21 HTMLElement 477
Description 478
Elements That Are HTMLElement Objects
478
Properties
483
Methods
485
Methods
495
Event Handlers
506
22 HTMLHtmlElement 509
Description 510
Properties
510

Methods
514
23 HTMLDocument/document 515
Description 516
One Method, Two Properties, and Compatibility with Three
Browsers
516
The Impact of the Standards
517
The Second Browser War: 6.x
518
Collections: A Holdover from 3.x Browsers
518
The Body and Document Are Now Separate
519
Properties
519
Methods
536
Event Handlers
552
HTMLBodyElement/<body> </body>
553
Description
553
Properties
553
Methods
559
Event Handlers

559
24 Head Elements 561
HTMLHeadElement Interface/<head> </head> 563
Description
563
Properties
564
Methods
567
Event Handlers
567
HTMLBaseElement/<base />
567
Description
568
Properties
568
00 0672322013 FM 5/3/02 1:31 PM Page xii
xiii
CONTENTS
HTMLMetaElement/<meta /> 570
Description
570
Properties
571
Methods
573
Event Handler
573
Example: Automatic Refreshing

573
HTMLTitleElement Interface/<title> </title>
575
Description
575
Properties
576
Methods
577
Event Handlers
578
25 Form Elements 579
HTMLFormElement interface/<form>…</form> 580
Description
580
Referencing a Form
580
Validation of Forms
583
Where a Form Sends Data and How
584
Properties
585
Methods
592
Event Handlers
593
HTMLButtonElement Interface/<button>…</button>
594
Description

594
Properties
595
Methods
600
Event Handlers
600
HTMLFieldSetElement Interface/<fieldset>…</fieldset>
601
Description
602
Properties
603
Methods
605
HTMLIsIndexElement Interface/<isindex />
605
Description
606
Properties
607
Methods
610
Event Handlers
610
HTMLLabelElement/<label>…</label>
610
Description
611
Properties

612
Methods
615
Event Handlers
616
00 0672322013 FM 5/3/02 1:31 PM Page xiii
xiv
JAVASCRIPT DEVELOPER’S DICTIONARY
HTMLLegendElement/<legend>…</legend> 616
Description
617
Properties
617
Methods
620
HTMLOptGroupElement/<optgroup>…</optgroup>
620
Description
621
Properties
621
Methods
622
HTMLOptionElement/<option />
622
Description
623
Properties
623
Methods

626
Event Handlers
626
Description
627
Properties
629
Methods
634
Event Handlers
637
HTMLTextAreaElement/<textarea>…</textarea>
638
Description
638
Properties
639
Methods
645
Event Handlers
646
26 Form Input Elements: HTMLInputElement 649
Description 650
The Various Types of Inputs
650
Properties
653
Methods
661
Event Handlers

663
27 List Elements 667
HTMLDirectoryElement Interface 669
Properties
669
Methods
671
Event Handlers
671
HTMLDListElement Interface
672
Properties
673
Methods
674
Event Handlers
674
00 0672322013 FM 5/3/02 1:31 PM Page xiv
xv
CONTENTS
HTMLLIElement Interface 675
Properties
676
Methods
678
Event Handlers
678
HTMLMenuElement Interface
679
Properties

680
Methods
682
Event Handlers
682
HTMLOListElement Interface
683
Properties
683
Methods
686
Event Handlers
686
HTMLUListElement Interface
687
Properties
687
Methods
690
Event Handlers
690
28 Te xt Formatting Elements 691
Five Unusual But Significant Text Formatting Elements 694
HTMLAnchorElement/<a>…</a>
699
Description
699
Properties
700
Methods

711
Event Handlers
712
HTMLBaseFontElement Interface/<basefont> </basefont>
713
Description
713
Properties
714
Event Handlers
715
HTMLBRElement/<br />
715
Description
715
Properties
716
Event Handlers
716
HTMLDivElement/<div> </div>
717
Description
717
Properties
718
Event Handlers
719
HTMLFontElement Interface/<font> </font>
720
Description

721
Properties
721
Event Handlers
724
00 0672322013 FM 5/3/02 1:31 PM Page xv
xvi
JAVASCRIPT DEVELOPER’S DICTIONARY
HTMLHeadingElement Interface / <hN> </hN> 724
Description
726
Properties
727
Methods
729
Event Handlers
729
HTMLHRElement Interface / <hr />
730
Description
730
Properties
730
Event Handlers
733
Layer( )/<layer> </layer> / <ilayer> </ilayer>
734
Description
734
Properties

735
Methods
742
Event Handlers
746
<marquee>…</marquee>
746
Description
746
Properties
747
Methods
752
Event Handlers
752
HTMLModElement Interface/<ins> </ins>/<del> </del>
753
Description
754
Event Handlers
757
<nobr>…</nobr>
757
Description
758
Properties
758
Methods
759
Event Handlers

759
HTMLParagraphElement/<p>…</p>
760
Description
760
Properties
760
Methods
762
Event Handlers
762
HTMLPreElement Interface / <pre>…</pre>
763
Description
764
Properties
764
Methods
767
Event Handlers
767
HTMLQuoteElement Interface / <q>…</q>
768
Description
768
00 0672322013 FM 5/3/02 1:31 PM Page xvi
xvii
CONTENTS
Properties 768
Methods

770
Event Handlers
771
<wbr />
772
Description
772
<xmp>…</xmp>
773
Description
774
Properties
774
Methods
775
Event Handlers
776
29 Table Elements 777
HTMLTableElement / <table> </table> 778
Description
778
The DOM and HTML Tables
778
Other Features of HTML Tables
779
Properties
780
Methods
795
Event Handlers

798
HTMLTableCaptionElement/<caption>…</caption>
799
Description
799
Properties
800
Methods
802
Event Handlers
802
HTMLTableCellElement/<td>…</td> / <th>…</th>
803
Description
803
Properties
804
Methods
812
Event Handlers
812
HTMLTableColElement/<colgroup>…</colgroup> /
<col />
813
Description
813
Properties
814
Methods
817

Event Handlers
817
HTMLTableRowElement/<tr>…</tr>
817
Description
818
Properties
818
Methods
824
Event Handlers
825
00 0672322013 FM 5/3/02 1:31 PM Page xvii
xviii
JAVASCRIPT DEVELOPER’S DICTIONARY
HTMLTableSectionElement/<thead>…</thead> / <tfoot>…</tfoot> /
<tbody>…</tbody>
826
Description
827
Properties
827
Methods
832
Event Handlers
833
30 Image Elements 835
HTMLImageElement/<img /> 836
Description
836

Animation and Caching of Images in HTML
837
Properties
840
Methods
847
Event Handlers
848
HTMLInputElement/<input type=”image” />
848
Description
849
Properties
849
Methods
850
Event Handlers
852
HTMLMapElement/<map>…</map>
852
Description
853
Properties
853
Methods
855
Event Handlers
856
HTMLAreaElement/<area />
856

Description
857
Properties
858
31 Programmable Elements 863
LiveConnect: Netscape’s Bridge to Java 864
HTMLAppletElement/<applet> </applet>
865
Description
866
Properties
868
Methods
872
Event Handlers
872
HTMLEmbedElement/<embed> </embed>
873
Description
873
Properties
874
Methods
876
Event Handlers
876
00 0672322013 FM 5/3/02 1:31 PM Page xviii
xix
CONTENTS
HTMLObjectElement/<object> </object> 877

Description
877
Properties
881
Methods
888
Event Handlers
888
HTMLParamElement/<param />
888
Description
889
Properties
889
Methods
890
HTMLScriptElement/<script> </script>
891
Description
891
Properties
894
Methods
897
Event Handlers
897
Example: Reorganizing a Script
898
32 DOM-2 Events and Event Handlers 907
DOM-2 Events Introduced: Listeners and Interfaces 908

Creating and Using Event Model Objects
909
Event Capturing and Bubbling
912
Canceling an Event’s Propagation
916
Canceling an Event’s Default Action
918
Use Event Listeners Carefully
918
Creating and Throwing Custom Events
920
Who Supports DOM-2 Events?
924
Events in Internet Explorer
924
Event Handlers Are Also Available as Methods
925
Event Bubbling and Canceling in Internet Explorer
926
Mouse Capture in Internet Explorer
927
Netscape 4 and Events
928
Event Interface (DOM)/Event
931
Description
931
Properties
931

Methods
953
EventTarget Interface (DOM)
958
Description
958
Properties
959
Methods
959
DocumentEvent Interface (DOM)
960
00 0672322013 FM 5/3/02 1:31 PM Page xix
xx
JAVASCRIPT DEVELOPER’S DICTIONARY
Description 960
Properties
961
Methods
961
Events in Netscape and Internet Explorer
961
33 Styling for HTML Elements 985
The Basics of CSS 987
Simple Selectors in CSS
988
Compound Selectors in CSS
991
The Order of Precedence in Styling, from Elements
992

The Order of Precedence in Styling, from Style Sheets
993
Activating and Deactivating Style Sheets
995
The !important Style Sheet Attribute
997
Style Sheet Property Names in JavaScript
998
Object Models and Cascading Style Sheets
998
Netscape 4 and JavaScript Styling
1003
Twelve CSS Properties
1005
HTMLStyleElement/<style> </style>
1008
Properties
1008
Methods
1010
HTMLLinkElement/<link />/<?xml-stylesheet ?>
1011
Properties
1012
Methods
1016
34 DOM-2 Range 1017
What Is A Range? 1018
Boundary Points for Element and Document Nodes
1018

Boundary Points for Text Nodes
1020
Range
1021
Description
1021
Properties
1026
Methods
1029
DocumentRange Interface (DOM)
1049
Description
1049
Properties
1049
Methods
1049
nsISelection Interface
1050
Description
1050
Properties
1054
Methods
1054
00 0672322013 FM 5/3/02 1:31 PM Page xx
xxi
CONTENTS
35 Cookies 1057

Cookies, HTTP, and JavaScript 1058
The Syntax of Cookies
1060
Using Cookies
1062
John Krutsch’s Cookie Jar
1063
Best Practices for Using Cookies
1067
Part IV The Future 1069
36 XML-Related Technologies and Their DOMs 1071
XML 1.0 1072
Elements
1072
Attributes
1072
Document Structure
1073
DTD
1074
Elements
1075
Attributes
1076
Entities
1077
Conditional Sections
1078
XHTML 1.0: eXtensible HyperText Markup Language
1079

Namespaces in XML
1080
Scripting an XML Document: <html:script />
1083
Internet Explorer and XML Data Islands
1084
Modularization of XHTML and XHTML 1.1
1085
XLink
1087
XSLT 1.0: eXtensible Stylesheet Language, Transformations
1088
XML Schemas
1089
MathML 2.0: Mathematics Markup Language
1090
SVG 1.0: Scalable Vector Graphics
1091
XForms
1095
XQuery
1096
SOAP: Simple Object Access Protocol
1096
Other XML Languages
1096
Conclusion to the JavaScript Developer’s Dictionary
1097
Index 1099
00 0672322013 FM 5/3/02 1:31 PM Page xxi

About the Author
Alexander James Vincent is a 24-year-old Web design expert based in Vallejo, CA.
Born and raised in Seattle, WA, he served 23 months in the United States Navy as a
Seaman Journalist. Following his tour of duty, he returned to the field of computers he
grew up around and discovered JavaScript. Since then he has become a standards
evangelist for Web design, and works to encourage best practices in coding for all Web
languages, client- and server-side. He moderates the JavaScript Programming Help
forum and writes tutorials for Website Abstraction at
. He
also contributes Quality Assurance efforts to the Mozilla.org project. He is currently
in the process of completing the design of his JavaScript Laboratory site at
, a project he set aside for the JavaScript Developer’s Dictionary.
He is a huge fan of the Seattle Mariners, and hopes to see them in a World Series one
day soon.
00 0672322013 FM 5/3/02 1:31 PM Page xxii
Dedication
To the Nathan Hale High School faculty members; particularly, Principal
Eric Benson, and Language Arts teacher Victoria Carver. Also, the
Mathematics team at Nathan Hale—particularly Mr. Dong, Mr. Coon,
and Mr. Wiegand.
Numerous other departments (and the lunchroom cooks) may never forgive
me for leaving them out. But I will say what I told Mr. Benson a couple
years after my graduation; Nathan Hale High School was an average school
when I entered it as a freshman, in Mr. Benson’s first year as a principal.
By the time I had left four years later, it was a first-class high school.
Acknowledgments
There are a number of people I wish to say “Thank You” to. First on that list is God.
It may sound trite, but it isn’t. I would have died at sea were it not for Him, before
this book even became an idea in my head.
Next would be Arun Ranganathan at Netscape, for pointing me to those IDL files at

lxr.mozilla.org. Without his help in at least pointing me to them, this book would
have died halfway through.
Then there are the numerous people on the Sams Publishing team: Shelley Johnston-
Markanday, Jon Steever, Patricia Barnes, the Technical Editors for this book, Martin
Honnen and Jason Byers, and several other behind-the-scenes people I haven’t yet met
or e-mailed.
John Krutsch, a longtime friend and fellow moderator at Website Abstraction, for his
work on cookies.
Jason Karl Davis, Ryan Frishberg (a.k.a. “Arielladog”), and Andrew Shults (a.k.a.
“thejavaman1”), for second opinions on my occasional guessing.
Several Mozilla.org engineers and QA people whose names I can’t even begin to recall.
What goes around comes around, ladies and gentlemen, and I’ll be coming around
quite a bit in the future.
George Chiang, owner and operator of Website Abstraction
(
), the first place I found a home in Web site design.
00 0672322013 FM 5/3/02 1:31 PM Page xxiii
The various journalism instructors at Defense Information School at Fort Meade,
MD. Particularly then-Gunnery Sergeants Beyer and Roberts of the United States
Marine Corps, in the radio and public affairs departments respectively, and Sergeant
1st Class Sizer who put up with my attitudes (barely) in print journalism class in the
winter of 1996–1997. I don’t think I could have written this book as well as I did
without your teachings, and you’ve definitely impacted my writing style. The same
applies to all the non-commissioned officers, petty officers, and chief petty officers at
DINFOS during that time.
James LeBlance at Smart Scents, and Dimple Sayles at D & R Hauling, for doing
everything they could to keep a roof over my head and food in my stomach.
Who could forget Mom and Dad, who will be celebrating 25 years of marriage in
April 2002? I couldn’t.
00 0672322013 FM 5/3/02 1:31 PM Page xxiv

×