JavaScript by Example [2nd ed]
9780137054893, 0137054890
The World's Easiest Java Script Tutorial-Fully Updated!JavaScript by Example, Second Edition,is the easiest, most h
356
89
17MB
English
Pages XVI, 879 str.; 23 cm
[901]
Year 2010;2011
Report DMCA / Copyright
DOWNLOAD PDF FILE
Table of contents :
Cover......Page 1
Contents......Page 6
Preface......Page 16
1.1 What JavaScript Is......Page 18
1.2 What JavaScript Is Not......Page 19
1.3 What JavaScript Is Used For......Page 20
1.4.1 Analysis of the Diagram......Page 21
1.5 What Is Ajax?......Page 22
1.6 What JavaScript Looks Like......Page 24
1.7.1 The Three Layers......Page 25
1.8 JavaScript and Events......Page 27
1.9 Standardizing JavaScript and the W3C......Page 29
1.9.2 The Document Object Model......Page 30
1.10 What Browser?......Page 32
1.10.1 Versions of JavaScript......Page 33
1.10.3 Is JavaScript Enabled on Your Browser?......Page 35
1.11 Where to Put JavaScript......Page 37
1.11.1 JavaScript from External Files......Page 39
1.12.1 The W3C Validation Tool......Page 41
1.12.2 The Validome Validation Tool......Page 42
1.13 What You Should Know......Page 43
2.1 The HTML Document and JavaScript......Page 46
2.1.1 Script Execution......Page 47
2.2.2 Free Form and Reserved Words......Page 50
2.2.3 Statements and Semicolons......Page 51
2.2.5 The Tag......Page 52
2.3.1 Strings and String Concatenation......Page 54
2.3.2 The write() and writeln() Methods......Page 55
2.4.1 Types of Errors......Page 57
2.5.1 Firefox......Page 58
2.5.2 Debugging in Internet Explorer 8......Page 61
2.5.3 The JavaScript: URL Protocol......Page 63
2.6.1 Hiding JavaScript from Old Browsers......Page 64
2.7 What You Should Know......Page 67
3.1.1 Primitive Data Types......Page 70
3.2 Variables......Page 76
3.2.2 Declaring and Initializing Variables......Page 77
3.2.3 Dynamically or Loosely Typed Language......Page 79
3.2.5 Concatenation and Variables......Page 83
3.3 Constants......Page 84
3.4 Bugs to Watch For......Page 86
3.5 What You Should Know......Page 87
4.1.1 The alert() Method......Page 90
4.1.2 The prompt() Method......Page 93
4.1.3 The confirm() Method......Page 95
4.2 What You Should Know......Page 97
5.1 About JavaScript Operators and Expressions......Page 100
5.1.2 Precedence and Associativity......Page 101
5.2.1 Arithmetic Operators......Page 105
5.2.2 Shortcut Assignment Operators......Page 107
5.2.3 Autoincrement and Autodecrement Operators......Page 108
5.2.4 Concatenation Operator......Page 111
5.2.5 Comparison Operators......Page 112
5.2.6 Logical Operators......Page 118
5.2.7 The Conditional Operator......Page 125
5.2.8 Bitwise Operators......Page 126
5.3 Number, String, or Boolean? Data Type Conversion......Page 129
5.3.1 The parseInt() Function......Page 131
5.3.2 The parseFloat() Function......Page 133
5.3.3 The eval() Function......Page 135
5.4 Special Operators......Page 136
5.5 What You Should Know......Page 137
6.2 Conditionals......Page 140
6.2.1 if/else......Page 141
6.2.2 if/else if......Page 144
6.2.3 switch......Page 145
6.3.1 The while Loop......Page 148
6.3.2 The do/while Loop......Page 150
6.3.3 The for Loop......Page 151
6.3.4 The for/in Loop......Page 152
6.3.5 Loop Control with break and continue......Page 153
6.3.6 Nested Loops and Labels......Page 154
6.4 What You Should Know......Page 157
7.1 What Is a Function?......Page 160
7.1.1 Function Declaration and Invocation......Page 161
7.1.2 Return Values......Page 170
7.1.3 Anonymous Functions as Variables......Page 173
7.1.4 Closures......Page 175
7.1.5 Recursion......Page 178
7.2.1 Function Syntax......Page 183
7.2.2 Exception Handling with try/catch and throw......Page 185
7.3 What You Should Know......Page 189
8.1 What Are Objects?......Page 192
8.1.1 Objects and the Dot Syntax......Page 193
8.1.2 Creating an Object with a Constructor......Page 194
8.1.3 Properties of the Object......Page 195
8.1.4 Methods of the Object......Page 197
8.2.2 What Is this?......Page 199
8.2.3 Inline Functions as Methods......Page 202
8.3 Object Literals......Page 204
8.4.1 The with Keyword......Page 208
8.4.2 The for/in Loop......Page 211
8.5 Extending Objects with Prototypes......Page 213
8.5.1 Adding Properties with the Prototype Property......Page 215
8.5.2 The Prototype Lookup Chain......Page 216
8.5.3 Adding Methods with Prototype......Page 219
8.5.4 Properties and Methods of All Objects......Page 221
8.5.5 Creating Subclasses and Inheritance......Page 224
8.6 What You Should Know......Page 227
9.2 Array Objects......Page 230
9.2.1 Declaring and Populating Arrays......Page 231
9.2.2 Array Object Properties......Page 236
9.2.3 Associative Arrays......Page 238
9.2.4 Nested Arrays......Page 240
9.3 Array Methods......Page 244
9.4 The Date Object......Page 251
9.4.1 Using the Date Object Methods......Page 252
9.4.2 Manipulating the Date and Time......Page 255
9.4.3 Customizing the Date Object with the prototype Property......Page 257
9.5 The Math Object......Page 258
9.5.1 Rounding Up and Rounding Down......Page 261
9.5.2 Generating Random Numbers......Page 262
9.5.3 Wrapper Objects (String, Number, Function, Boolean)......Page 263
9.5.4 The String Object......Page 264
9.5.5 The Number Object......Page 276
9.5.6 The Boolean Object......Page 280
9.5.7 The Function Object......Page 281
9.5.8 The with Keyword Revisited......Page 283
9.6 What You Should Know......Page 284
10.1 JavaScript and the Browser Object Model......Page 288
10.1.1 Working with the navigator Object......Page 290
10.1.2 Working with the window Object......Page 302
10.1.3 Creating Timed Events......Page 309
10.1.4 Working with Frames......Page 320
10.1.5 The location Object......Page 332
10.1.6 The history Object......Page 336
10.1.7 The screen Object......Page 339
10.2 What You Should Know......Page 342
11.1 The Document Object Model and the Legacy DOM 0......Page 344
11.2 The JavaScript Hierarchy......Page 345
11.2.1 The Document Itself......Page 346
11.3.1 Attributes of the Tag......Page 351
11.4 JavaScript and the form Object......Page 358
11.4.1 Naming Forms and Input Types (Controls) for Forms......Page 359
11.4.2 The Legacy DOM with Forms......Page 362
11.4.3 Naming Forms and Buttons......Page 367
11.4.4 Submitting Fillout Forms......Page 373
11.4.5 The this Keyword......Page 382
11.4.6 The submit() and reset() Methods......Page 385
11.5 Programming Input Devices (Controls)......Page 389
11.5.1 Simple Form Validation......Page 418
11.6 What You Should Know......Page 426
12.1 Introduction to Images......Page 430
12.1.1 HTML Review of Images......Page 431
12.1.2 The JavaScript image Object......Page 433
12.2 Reviewing Links......Page 434
12.2.1 The JavaScript links Object......Page 435
12.3 Working with Imagemaps......Page 439
12.3.1 Replacing Images Dynamically with the src Property......Page 445
12.3.2 Preloading Images and the Image() Constructor......Page 449
12.3.3 Randomly Displaying Images and the onClick Event......Page 451
12.3.4 Links with an Image Map and JavaScript......Page 453
12.4 Resizing an Image to Fit the Window......Page 455
12.5 Introduction to Slideshows......Page 458
12.5.1 A Simple Slideshow with Controls......Page 459
12.5.2 A Clickable Image Slideshow......Page 462
12.6 Animation and Timers......Page 466
12.6.1 Changing Image Position......Page 467
12.6.2 Changing Image Height and Width Properties......Page 468
12.7 What You Should Know......Page 469
13.2 The Inline Model for Handling Events......Page 472
13.2.1 HTML and the Event Handler......Page 473
13.2.2 Setting Up an Event Handler......Page 476
13.2.3 Return Values......Page 478
13.2.4 JavaScript Object Methods and Events......Page 479
13.3.1 The onLoad and onUnLoad Events......Page 482
13.3.2 The onFocus and onBlur Event Handlers......Page 485
13.3.3 The onResize Event Handler......Page 489
13.4 Handling Mouse Events......Page 491
13.4.1 How to Use Mouse Events......Page 492
13.4.2 Mouse Events and Images—Rollovers......Page 494
13.4.3 Creating a Slideshow with Mouse Events......Page 495
13.5.1 JavaScript URLs......Page 498
13.6 Handling a Form Event......Page 499
13.6.1 Buttons......Page 500
13.6.2 this for Forms and this for Buttons......Page 501
13.6.3 Forms and the onClick Event Handler......Page 503
13.6.4 Forms and the onFocus and onBlur Event Handlers......Page 504
13.6.5 Forms and the onChange Event Handler......Page 506
13.6.6 Forms and the onSubmit Event Handler......Page 508
13.6.7 HTML Event Handlers and JavaScript Event Methods......Page 513
13.6.8 The onError Event......Page 515
13.7 The event Object......Page 516
13.7.1 Capturing and Bubbling (Trickle Down and Bubble Up)......Page 517
13.7.2 Event Object Properties......Page 518
13.7.3 Using Event Object Properties......Page 520
13.7.4 Passing Events to a JavaScript Function......Page 522
13.7.5 Mouse Positions......Page 525
13.7.6 Key Events......Page 530
13.8.1 Getting a Reference to the Object......Page 534
13.9 What You Should Know......Page 540
14.2 What Is a Style Sheet?......Page 544
14.2.1 What Is a CSS-Enhanced Browser?......Page 545
14.2.2 How Does a Style Sheet Work?......Page 546
14.3.1 Comments......Page 547
14.3.2 Grouping......Page 548
14.4 Common Style Sheet Properties......Page 549
14.4.1 Units of Measurement......Page 552
14.4.2 Working with Colors......Page 553
14.4.3 Working with Fonts......Page 556
14.4.4 Working with Text......Page 559
14.4.5 Working with Backgrounds and Images......Page 561
14.4.6 Working with Margins and Borders......Page 564
14.5.1 The Embedded Style Sheet and the Tag......Page 567
14.5.2 The Inline Style and the Attribute......Page 570
14.6.1 The Tag......Page 572
14.6.2 Importing with @import......Page 574
14.7 Creating a Style Class......Page 575
14.7.1 Styling a Simple Table with Class......Page 577
14.7.2 Using a Specific Class Selector......Page 579
14.8 The ID Selector and the ID Attribute......Page 581
14.9 Overriding or Adding a Style with the Tag......Page 583
14.9.1 The Tag and the style Attribute......Page 584
14.9.2 The Tag and the class Attribute......Page 585
14.9.3 Inheritance and Contextual Selectors......Page 586
14.10 Positioning Elements and Layers......Page 589
14.10.1 Absolute Positioning......Page 590
14.10.2 The Container......Page 596
14.10.3 Absolute Positioning......Page 597
14.10.4 Relative Positioning......Page 598
14.10.5 The z-index and Three Dimensions......Page 600
14.11.2 How JavaScript Views Style Sheets......Page 602
14.11.3 The style Object......Page 606
14.11.4 The className Property......Page 615
14.11.5 Drop-Down Menus and Tooltips......Page 618
14.12 What You Should Know......Page 626
15.1 The W3C DOM......Page 628
15.2 How the DOM Works with Nodes......Page 629
15.3 Nodes......Page 630
15.3.1 Parents and Children......Page 632
15.3.3 The nodeName and nodeType Properties......Page 633
15.3.4 The Whitespace Bug......Page 634
15.4 Walking with the DOM......Page 635
15.5 DOM Inspectors......Page 638
15.6.1 The document.getElementById() Method......Page 639
15.6.2 The document.getElementsByTagName() Method......Page 642
15.6.3 JavaScript Properties to Represent HTML Attributes......Page 644
15.7 Modifying the DOM (Appending, Copying, and Removing Nodes)......Page 646
15.7.1 The innerHTML Property and the Element’s Content......Page 647
15.7.2 Modifying the Content of an Element......Page 649
15.7.3 Creating New Elements with the DOM......Page 651
15.7.4 Inserting Before a Node......Page 653
15.7.5 Creating Attributes for Nodes......Page 654
15.7.6 DOM Review: Creating a Blog......Page 656
15.7.7 Creating a Table with the DOM......Page 661
15.7.8 Cloning Nodes......Page 665
15.7.9 Removing a Node......Page 670
15.7.10 Scrolling with the Nodes......Page 675
15.8.2 The Scripting Way......Page 678
15.8.4 Bubbling and Capturing......Page 679
15.9.1 Adding an Event......Page 685
15.9.2 Registering More Than One Event......Page 687
15.9.3 Removing an EventListener......Page 690
15.9.4 Event Listeners with Microsoft Internet Explorer......Page 693
15.9.5 Event Properties Revisited......Page 695
15.10 Unobtrusive JavaScript......Page 699
15.10.1 JavaScript Libraries......Page 706
15.11 What You Should Know......Page 707
16.1 What Are Cookies?......Page 712
16.1.1 Cookie Ingredients......Page 715
16.1.2 The Attributes of a Cookie......Page 716
16.2.1 The Cookie Object......Page 718
16.2.2 Assigning Cookie Attributes......Page 719
16.2.3 Let’s Make Cookies!......Page 721
16.2.4 Retrieving Cookies from a Server......Page 725
16.2.5 Deleting a Cookie......Page 727
16.2.6 Using the Browser to Remove Cookies......Page 730
16.3 What You Should Know......Page 731
17.1 What Is a Regular Expression?......Page 734
17.2.1 The Literal Way......Page 736
17.2.2 The Constructor Method......Page 737
17.2.3 Testing the Expression......Page 738
17.2.4 Properties of the RegExp Object......Page 741
17.3.1 The match() Method......Page 744
17.3.2 The search() Method......Page 746
17.3.3 The replace() Method......Page 747
17.3.4 The split() Method......Page 748
17.4 Getting Control—The Metacharacters......Page 750
17.4.1 The Dot Metacharacter......Page 753
17.4.2 The Character Class......Page 755
17.4.3 Metasymbols......Page 758
17.4.4 Metacharacters to Repeat Pattern Matches......Page 762
17.4.5 Anchoring Metacharacters......Page 771
17.4.6 Alternation......Page 776
17.5.1 Checking for Empty Fields......Page 782
17.5.2 Checking for Numeric Zip Codes......Page 784
17.5.3 Checking for Alphabetic Data......Page 786
17.5.4 Removing Extraneous Characters......Page 788
17.5.5 Checking for Valid Social Security Numbers......Page 792
17.5.6 Checking for Valid Phone Numbers......Page 794
17.5.7 Checking for Valid E-Mail Addresses......Page 798
17.5.8 Credit Card Validation......Page 800
17.5.9 Putting It All Together......Page 808
17.6 What You Should Know......Page 812
18.1 Why Ajax?......Page 814
18.2 Why Is Ajax Covered Last?......Page 815
18.3 The Steps for Creating Ajax Communication......Page 816
18.3.1 Step 1: Create the XMLHttpRequest Object......Page 817
18.3.2 Step 2: Initializing the Object......Page 820
18.3.3 Sending the Request to the Server......Page 822
18.3.4 Step 3: Monitoring the State of the Server Response......Page 823
18.3.5 Handling the Response with a Callback Function......Page 825
18.3.6 The Browser Cache Issue......Page 827
18.4 Putting It All Together......Page 829
18.4.1 Using Ajax to Retrieve Text from a File......Page 836
18.4.2 Using Ajax to Retrieve XML from a File......Page 839
18.4.3 Ajax and Forms......Page 843
18.5 Ajax and JSON......Page 851
18.5.1 JSON Data Structures......Page 852
18.5.2 Steps to Use JSON......Page 853
18.5.3 Putting It All Together with JSON......Page 856
18.5.4 Solving the eval() Security Problem......Page 860
18.6 Debugging Ajax with Firebug......Page 865
18.6.1 Basic Instructions for Using Firefox......Page 868
18.6.2 What You Should Know......Page 869
A......Page 872
B......Page 874
C......Page 875
D......Page 877
E......Page 878
F......Page 879
H......Page 881
I......Page 882
J......Page 883
L......Page 884
M......Page 885
N......Page 886
O......Page 887
P......Page 889
R......Page 890
S......Page 891
T......Page 893
W......Page 895
Z......Page 896