JavaScript and DHTML cookbook 0596004672, 9780596004675

On numerous online forums for JavaScript and DHTML, the majority of questions begin with "How do I...?" This n

276 22 3MB

English Pages 520 Seiten [605] Year 2003

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Main Page......Page 1
Table of content......Page 2
Copyright......Page 7
Preface......Page 8
About You......Page 9
About the Recipes......Page 10
What's in This Book......Page 11
Browser Platforms......Page 12
Conventions Used in This Book......Page 13
Request for Comments......Page 14
Acknowledgments......Page 15
Chapter 1. Strings......Page 16
1.0 Introduction......Page 17
1.1 Concatenating (Joining) Strings......Page 20
1.2 Accessing Substrings......Page 23
1.3 Changing String Case......Page 26
1.4 Testing Equality of Two Strings......Page 27
1.5 Testing String Containment Without Regular Expressions......Page 30
1.6 Testing String Containment with Regular Expressions......Page 33
1.7 Searching and Replacing Substrings......Page 35
1.8 Using Special and Escaped Characters......Page 38
1.9 Reading and Writing Strings for Cookies......Page 41
1.10 Converting Between Unicode Values and String Characters......Page 44
1.11 Encoding and Decoding URL Strings......Page 45
1.12 Encoding and Decoding Base64 Strings......Page 48
Chapter 2. Numbers and Dates......Page 51
2.0 Introduction......Page 52
2.1 Converting Between Numbers and Strings......Page 55
2.2 Testing a Number's Validity......Page 58
2.3 Testing Numeric Equality......Page 61
2.4 Rounding Floating-Point Numbers......Page 64
2.5 Formatting Numbers for Text Display......Page 65
2.6 Converting Between Decimal and Hexadecimal Numbers......Page 68
2.7 Generating Pseudorandom Numbers......Page 71
2.8 Calculating Trigonometric Functions......Page 72
2.9 Creating a Date Object......Page 73
2.10 Calculating a Previous or Future Date......Page 76
2.11 Calculating the Number of Days Between Two Dates......Page 79
2.12 Validating a Date......Page 82
Chapter 3. Arrays and Objects......Page 85
3.0 Introduction......Page 86
3.1 Creating a Simple Array......Page 89
3.2 Creating a Multidimensional Array......Page 92
3.3 Converting Between Arrays and Strings......Page 95
3.4 Doing Something with the Items in an Array......Page 98
3.5 Sorting a Simple Array......Page 101
3.6 Combining Arrays......Page 104
3.7 Dividing Arrays......Page 105
3.8 Creating a Custom Object......Page 108
3.9 Simulating a Hash Table for Fast Array Lookup......Page 111
3.10 Doing Something with a Property of an Object......Page 114
3.11 Sorting an Array of Objects......Page 117
3.12 Customizing an Object's Prototype......Page 120
3.13 Converting Arrays and Custom Objects to Strings......Page 123
Chapter 4. Variables, Functions, and Flow Control......Page 126
4.0 Introduction......Page 127
4.1 Creating a JavaScript Variable......Page 128
4.1 Creating a JavaScript Variable......Page 131
4.1 Creating a JavaScript Variable......Page 134
4.4 Creating an Anonymous Function......Page 137
4.5 Delaying a Function Call......Page 140
4.6 Branching Execution Based on Conditions......Page 143
4.7 Handling Script Errors Gracefully......Page 146
4.8 Improving Script Performance......Page 149
Chapter 5. Browser Feature Detection......Page 152
5.0 Introduction......Page 153
5.1 Detecting the Browser Brand......Page 156
5.2 Detecting an Early Browser Version......Page 157
5.3 Detecting the Internet Explorer Version......Page 160
5.4 Detecting the Netscape Navigator Version......Page 163
5.5 Detecting the Client Operating System......Page 166
5.6 Detecting Object Support......Page 169
5.7 Detecting Object Property and Method Support......Page 172
5.8 Detecting the Browser Written Language......Page 175
5.9 Detecting Cookie Availability......Page 177
5.10 Defining Browser- or Feature-Specific Links......Page 178
5.11 Testing on Multiple Browser Versions......Page 181
Chapter 6. Managing Browser Windows......Page 184
6.0 Introduction......Page 185
6.1 Setting the Main Window's Size......Page 188
6.2 Positioning the Main Window......Page 191
6.3 Maximizing the Main Window......Page 192
6.4 Creating a New Window......Page 195
6.5 Bringing a Window to the Front......Page 198
6.6 Communicating with a New Window......Page 201
6.7 Communicating Back to the Main Window......Page 204
6.8 Using Internet Explorer Modal/Modeless Windows......Page 205
6.9 Simulating a Cross-Browser Modal Dialog Window......Page 208
6.10 Simulating a Window with Layers......Page 211
Chapter 7. Managing Multiple Frames......Page 214
7.0 Introduction......Page 215
7.1 Creating a Blank Frame in a New Frameset......Page 218
7.2 Changing the Content of One Frame from Another......Page 221
7.3 Changing the Content of Multiple Frames at Once......Page 224
7.4 Replacing a Frameset with a Single Page......Page 227
7.5 Avoiding Being 'Framed' by Another Site......Page 228
7.6 Assuring a Page Loads in its Frameset......Page 230
7.7 Reading a Frame's Dimensions......Page 233
7.8 Resizing Frames......Page 236
7.9 Setting Frameset Specifications Dynamically......Page 239
Chapter 8. Dynamic Forms......Page 242
8.0 Introduction......Page 243
8.1 Auto-Focusing the First Text Field......Page 246
8.2 Performing Common Text Field Validations......Page 247
8.3 Preventing Form Submission upon Validation Failure......Page 250
8.4 Auto-Focusing an Invalid Text Field Entry......Page 253
8.5 Changing a Form's Action......Page 256
8.6 Blocking Submissions from the Enter Key......Page 257
8.7 Advancing Text Field Focus with the Enter Key......Page 260
8.8 Submitting a Form by an Enter Key Press in Any Text Box......Page 261
8.9 Disabling Form Controls......Page 264
8.10 Hiding and Showing Form Controls......Page 267
8.11 Allowing Only Numbers (or Letters) in a Text Box......Page 270
8.12 Auto-Tabbing for Fixed-Length Text Boxes......Page 273
8.13 Changing select Element Content......Page 276
8.14 Copying Form Data Between Pages......Page 279
Chapter 9. Managing Events......Page 282
9.0 Introduction......Page 283
9.1 Equalizing the IE and W3C Event Models......Page 286
9.2 Initiating a Process After the Page Loads......Page 289
9.3 Determining the Coordinates of a Click Event......Page 292
9.4 Preventing an Event from Performing Its Default Behavior......Page 295
9.5 Blocking Double Clicks......Page 298
9.6 Determining Which Element Received an Event......Page 300
9.7 Determining Which Mouse Button Was Pressed......Page 303
9.8 Reading Which Character Key Was Typed......Page 306
9.9 Reading Which Noncharacter Key Was Pressed......Page 309
9.10 Determining Which Modifier Keys Were Pressed During an Event......Page 312
9.11 Determining the Element the Cursor Rolled From/To......Page 315
9.12 Synchronizing Sounds to Events......Page 318
Chapter 10. Page Navigation Techniques......Page 321
10.0 Introduction......Page 322
10.1 Loading a New Page or Anchor......Page 325
10.2 Keeping a Page Out of the Browser History......Page 328
10.3 Using a select Element for Navigation......Page 329
10.4 Passing Data Between Pages Via Cookies......Page 332
10.5 Passing Data Between Pages Via Frames......Page 335
10.6 Passing Data Between Pages Via URLs......Page 338
10.7 Creating a Contextual (Right-Click) Menu......Page 341
10.8 Creating Drop-Down Navigation Menus......Page 344
10.9 Providing Navigation Trail Menus......Page 347
10.10 Creating Expandable Menus......Page 350
10.11 Creating Collapsible XML Menus......Page 353
Chapter 11. Managing Style Sheets......Page 356
11.0 Introduction......Page 357
11.1 Assigning Style Sheet Rules to an Element Globally......Page 360
11.2 Assigning Style Sheet Rules to a Subgroup of Elements......Page 361
11.3 Assigning Style Sheet Rules to an Individual Element......Page 364
11.4 Importing External Style Sheets......Page 365
11.5 Importing Browser- or Operating System-Specific Style Sheets......Page 366
11.6 Changing Imported Style Sheets After Loading......Page 367
11.7 Enabling/Disabling Style Sheets......Page 370
11.8 Toggling Between Style Sheets for an Element......Page 371
11.9 Overriding a Style Sheet Rule......Page 374
11.10 Turning Arbitrary Content into a Styled Element......Page 375
11.11 Creating Center-Aligned Body Elements......Page 376
11.12 Reading Effective Style Sheet Property Values......Page 377
11.13 Forcing Version 6 Browsers into Standards-Compatibility Mode......Page 380
Chapter 12. Visual Effects for Stationary Content......Page 383
12.0 Introduction......Page 384
12.1 Precaching Images......Page 387
12.2 Swapping Images (Rollovers)......Page 390
12.3 Changing Text Style Properties......Page 393
12.4 Offering Body Text Size Choices to Users......Page 396
12.5 Creating Custom Link Styles......Page 399
12.6 Changing Page Background Colors and Images......Page 402
12.7 Hiding and Showing Elements......Page 405
12.8 Adjusting Element Transparency......Page 408
12.9 Creating Transition Visual Effects......Page 411
Chapter 13. Positioning HTML Elements......Page 414
13.0 Introduction......Page 415
13.1 Making an Element Positionable in the Document Space......Page 418
13.2 Connecting a Positioned Element to a Body Element......Page 419
13.3 Controlling Positioning Via a DHTML JavaScript Library......Page 422
13.4 Deciding Between div and span Containers......Page 425
13.5 Adjusting Positioned Element Stacking Order (z-order)......Page 428
13.6 Centering an Element on Top of Another Element......Page 431
13.7 Centering an Element in a Window or Frame......Page 434
13.8 Determining the Location of a Nonpositioned Element......Page 437
13.9 Animating Straight-Line Element Paths......Page 440
13.10 Animating Circular Element Paths......Page 443
13.11 Creating a Draggable Element......Page 446
13.12 Scrolling div Content......Page 449
13.13 Creating a Custom Scrollbar......Page 452
Chapter 14. Creating Dynamic Content......Page 455
14.0 Introduction......Page 458
14.1 Writing Dynamic Content During Page Loading......Page 459
14.2 Creating New Page Content Dynamically......Page 462
14.3 Including External HTML Content......Page 465
14.4 Embedding XML Data......Page 468
14.5 Embedding Data as JavaScript Objects......Page 471
14.6 Transforming XML Data into HTML Tables......Page 474
14.7 Transforming JavaScript Objects into HTML Tables......Page 477
14.8 Converting an XML Node Tree to JavaScript Objects......Page 480
14.9 Creating a New Element......Page 483
14.10 Creating Text Content for a New Element......Page 486
14.11 Creating Mixed Element and Text Nodes......Page 489
14.12 Inserting and Populating an iframe Element......Page 492
14.13 Getting a Reference to an HTML Element Object......Page 495
14.14 Replacing Portions of Body Content......Page 498
14.15 Removing Body Content......Page 501
14.16 Sorting Dynamic Tables......Page 504
14.17 Walking the Document Node Tree......Page 507
14.18 Capturing Document Content......Page 510
Chapter 15. Dynamic Content Applications......Page 513
15.0 Introduction......Page 514
15.1 Displaying a Random Aphorism......Page 515
15.2 Converting a User Selection into an Arbitrary Element......Page 518
15.3 Automating the Search-and-Replace of Body Content......Page 521
15.4 Creating a Slide Show......Page 524
15.5 Auto-Scrolling the Page......Page 527
15.6 Greeting Users with Their Time of Day......Page 530
15.7 Displaying the Number of Days Before Christmas......Page 532
15.8 Displaying a Countdown Timer......Page 535
15.9 Creating a Calendar Date Picker......Page 538
15.10 Displaying an Animated Progress Bar......Page 541
Appendix A. Keyboard Event Character Values......Page 544
Appendix B. Keyboard Key Code Values......Page 547
Appendix C. ECMAScript Reserved Keywords......Page 550
Colophon......Page 551
Index......Page 552
Index SYMBOL......Page 553
Index A......Page 554
Index B......Page 557
Index C......Page 560
Index D......Page 563
Index E......Page 566
Index F......Page 569
Index G......Page 572
Index H......Page 573
Index I......Page 574
Index J......Page 577
Index K......Page 578
Index L......Page 579
Index M......Page 580
Index N......Page 581
Index O......Page 584
Index P......Page 587
Index Q......Page 590
Index R......Page 591
Index S......Page 592
Index T......Page 595
Index U......Page 598
Index V......Page 599
Index W......Page 600
Index X......Page 603
Index Y......Page 604
Index Z......Page 605

JavaScript and DHTML cookbook
 0596004672, 9780596004675

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers