Table of Contents
Introduction..................................................1
JavaScript............................................................................................ 1
AJAX................................................................................................... 3
How To Use This Book....................................................................... 4
Section I: JavaScript Basic..............................7
Chapter 1: JavaScript How To and Where To............... 9
How To Put a JavaScript into an HTML Page..................................... 9
How to Handle Simple Browsers....................................................... 12
Where to Put the JavaScript.............................................................. 12
Using an External JavaScript............................................................. 15
Chapter 2: JavaScript Statements and Comments....... 16
JavaScript Statements........................................................................ 16
JavaScript Comments........................................................................ 18
Chapter 3: JavaScript Variables................................... 23
Do You Remember Algebra from School?.......................................... 23
JavaScript Variables........................................................................... 23
Declaring (Creating) JavaScript Variables.......................................... 25
Assigning Values to Undeclared JavaScript Variables.......................... 25
Redeclaring JavaScript Variables........................................................ 26
JavaScript Arithmetic........................................................................ 26
611944-FM.F.indd 5 4/29/10 9:47 PM
Table of Contents
Chapter 4: JavaScript Operators................................. 27
JavaScript Arithmetic Operators........................................................ 27
JavaScript Assignment Operators....................................................... 28
The + Operator Used on Strings........................................................ 28
Adding Strings and Numbers............................................................ 28
Chapter 5: JavaScript Comparisons............................ 30
Comparison Operators...................................................................... 30
How to Use Comparisons................................................................. 31
Logical Operators.............................................................................. 31
Conditional Operator....................................................................... 31
Chapter 6: JavaScript If...Else Statements................... 32
Conditional Statements..................................................................... 32
if Statement....................................................................................... 33
if...else Statement.............................................................................. 34
if...else if...else Statement................................................................... 36
Chapter 7: JavaScript Loops....................................... 39
The for Loop..................................................................................... 39
The while Loop................................................................................. 41
The do...while Loop.......................................................................... 43
Chapter 8: Additional JavaScript Flow Control
Statements.................................................................... 46
The break Statement......................................................................... 46
The continue Statement.................................................................... 47
JavaScript for...in Statement.............................................................. 48
JavaScript switch Statement............................................................... 50
vi
611944-FM.F.indd 6 4/29/10 9:47 PM
Table of Contents
Chapter 9: JavaScript Popup Boxes............................. 53
Popup Boxes...................................................................................... 53
Chapter 10: JavaScript Functions............................... 58
How to Define a Function................................................................ 58
JavaScript Function Examples........................................................... 59
The return Statement........................................................................ 62
The Lifetime of JavaScript Variables.................................................. 63
Chapter 11: JavaScript Events..................................... 64
onLoad and onUnload...................................................................... 64
onFocus, onBlur, and onChange....................................................... 65
onSubmit.......................................................................................... 65
onMouseOver and onMouseOut....................................................... 66
onClick............................................................................................. 66
Chapter 12: Javascript try…catch and
throw Statements.......................................................... 67
JavaScript—Catching Errors............................................................. 67
The try...catch Statement.................................................................. 67
The throw Statement......................................................................... 70
Chapter 13: JavaScript Special Characters and
Guidelines.................................................................... 73
Insert Special Characters................................................................... 73
JavaScript Is Case Sensitive................................................................ 74
White Space...................................................................................... 74
Break up a Code Line........................................................................ 74
vii
611944-FM.F.indd 7 4/29/10 9:47 PM
Table of Contents
Section II: JavaScript Objects.......................77
Chapter 14: JavaScript Objects Intro.......................... 79
Object-Oriented Programming......................................................... 79
Properties.......................................................................................... 79
Methods............................................................................................ 80
Chapter 15: JavaScript String Object.......................... 81
String Object..................................................................................... 81
Examples........................................................................................... 82
Chapter 16: JavaScript Date Object............................ 88
Create a Date Object......................................................................... 88
Set Dates........................................................................................... 89
Compare Two Dates.......................................................................... 89
Examples........................................................................................... 90
Chapter 17: JavaScript Array Object........................... 95
What Is an Array?.............................................................................. 95
Create an Array................................................................................. 96
Access an Array................................................................................. 96
Modify Values in an Array................................................................. 96
Examples........................................................................................... 97
Chapter 18: JavaScript Boolean Object.................... 107
Create a Boolean Object.................................................................. 107
Examples......................................................................................... 108
viii
611944-FM.F.indd 8 4/29/10 9:47 PM
Table of Contents
Chapter 19: JavaScript Math Object......................... 110
Math Object................................................................................... 110
Mathematical Constants.................................................................. 110
Mathematical Methods................................................................... 111
Examples......................................................................................... 112
Chapter 20: JavaScript RegExp Object..................... 115
What Is RegExp?............................................................................. 115
RegExp Modifiers............................................................................ 116
test()................................................................................................ 118
exec().............................................................................................. 119
Section III: JavaScript Advanced.................121
Chapter 21: JavaScrip Browser Detection................. 123
Browser Detection........................................................................... 123
The Navigator Object..................................................................... 124
Chapter 22: JavaScript Cookies................................ 129
What Is a Cookie?........................................................................... 129
Create and Store a Cookie............................................................... 130
Chapter 23: JavaScript Form Validation................... 134
Required Fields............................................................................... 134
E-mail Validation............................................................................ 136
ix
611944-FM.F.indd 9 4/29/10 9:47 PM
Table of Contents
Chapter 24: JavaScript Animation............................ 138
The HTML Code........................................................................... 138
The JavaScript Code........................................................................ 139
The Entire Code............................................................................. 139
Chapter 25: JavaScript Image Maps.......................... 141
HTML Image Maps........................................................................ 141
Adding Some JavaScript.................................................................. 142
Chapter 26: JavaScript Timing Events...................... 144
The setTimeout() Method............................................................... 144
The clearTimeout() Method............................................................ 149
Chapter 27: Create Your Own Objects
with JavaScript............................................................ 151
JavaScript Objects........................................................................... 151
Creating Your Own Objects............................................................ 152
Section IV: AJAX Basic................................157
Chapter 28: AJAX XMLHttpRequest....................... 159
AJAX Uses the XMLHttpRequest Object........................................ 159
The XMLHttpRequest Object........................................................ 159
Your First AJAX Application........................................................... 160
Chapter 29: AJAX Browser Support......................... 163
The XMLHttpRequest.................................................................... 163
All Together Now............................................................................ 164
611944-FM.F.indd 10 4/29/10 9:47 PM
Table of Contents
Chapter 30: AJAX—The XMLHttpRequest
Object’s Methods and Properties................................. 166
Important Methods......................................................................... 166
Sending an AJAX Request to a Server.............................................. 167
Important Properties....................................................................... 167
The responseText Property.............................................................. 167
XMLHttpRequest Open—Using False............................................ 167
XMLHttpRequest Open—Using True............................................ 168
The readyState Property.................................................................. 168
The onreadystatechange Property.................................................... 169
Chapter 31: AJAX Server.......................................... 170
XMLHttpRequest Object Can Request Any Data........................... 170
Requesting Files.............................................................................. 170
Submitting Forms........................................................................... 171
Section V: AJAX Advanced.........................173
Chapter 32: AJAX Suggest....................................... 175
The HTML Form........................................................................... 175
The showHint() Function............................................................... 176
The GetXmlHttpObject() Function................................................ 177
The stateChanged() Function.......................................................... 177
AJAX Suggest Source Code............................................................. 178
Chapter 33: AJAX Database Example....................... 184
The AJAX JavaScript....................................................................... 185
The AJAX Server Page..................................................................... 186
xi
611944-FM.F.indd 11 4/29/10 9:47 PM
Table of Contents
Chapter 34: AJAX XML Example............................ 188
The AJAX JavaScript....................................................................... 189
The AJAX Server Page..................................................................... 190
The XML File................................................................................. 191
Chapter 35: AJAX ResponseXML Example.............. 198
The AJAX JavaScript....................................................................... 199
The AJAX Server Page..................................................................... 201
Appendixes...............................................203
Appendix A: JavaScript Objects........................................................................203
Appendix B: HTML DOM Objects.................................................................214
Index.........................................................245
xii
611944-FM.F.indd 12 4/29/10 9:47 PM