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 developer’s dictionary
Nội dung xem thử
Mô tả chi tiết
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 responsibility 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
1 Object( ) 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
2 Function( ) 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
3 Array( ) 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
4 String( ) 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
5 Boolean( ) 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
6 Date( ) 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
7 Number( ) 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
CONTENTS vii
Properties............................................................................................ 143
Methods.............................................................................................. 146
Example: Implementing Complex Numbers in JavaScript............................ 149
8 Math 157
Description.......................................................................................... 157
Properties............................................................................................ 158
Methods.............................................................................................. 161
Example: A Logarithm Function for Multiple Bases ................................... 168
9 RegExp( ) 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
CONTENTS ix
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
CONTENTS xi
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
Text .................................................................................................... 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
CONTENTS xiii
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