Advanced JavaScript 2nd Edition by Chuck Easttom ISBN 155622852X 9781556228520 Instant Download
Advanced JavaScript 2nd Edition by Chuck Easttom ISBN 155622852X 9781556228520 Instant Download
https://ebookball.com/product/advanced-javascript-2nd-edition-by-
chuck-easttom-isbn-155622852x-9781556228520-13370/
Computer Crime Investigation and the Law 1st Edition by Chuck Easttom,
Jeff Taylor ISBN 1435455320 9781435455320
https://ebookball.com/product/computer-crime-investigation-and-
the-law-1st-edition-by-chuck-easttom-jeff-taylor-
isbn-1435455320-9781435455320-11852/
https://ebookball.com/product/object-oriented-javascript-2nd-
edition-by-stoyan-stefanov-kumar-chetan-sharma-
isbn-9781849693134-1849693137-20218/
https://ebookball.com/product/superviviente-1st-edition-by-chuck-
palahniuk-14964/
https://ebookball.com/product/javascript-and-jquery-the-missing-
manual-2nd-edition-by-mcfarland-david-sawyer-
isbn-1449399029-9781449399023-11302/
Computer Security Fundamentals 1st edition by William Easttom II ISBN
0135774772 978-0135774779
https://ebookball.com/product/computer-security-fundamentals-1st-
edition-by-william-easttom-ii-
isbn-0135774772-978-0135774779-16578/
https://ebookball.com/product/superviviente-1st-edition-by-chuck-
palahniuk-isbn-009928264x-978-0099282648-11848/
https://ebookball.com/product/advanced-practice-nursing-
procedures-2nd-edition-by-margaret-colyar-
isbn-1719642397-9781719642392-6384/
https://ebookball.com/product/javascript-mini-faq-1st-edition-by-
danny-goodman-isbn-11420/
https://ebookball.com/product/advanced-nursing-practice-2nd-
edition-by-paula-mcgee-george-
castledine-1405102349-9781405102346-814/
Advanced
JavaScript
Second Edition
Chuck Easttom
ISBN 1-55622-852-X
10 9 8 7 6 5 4 3 2 1
0105
Product names mentioned are used for identification purposes only and may be trademarks of their respective
companies.
All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the
above address. Telephone inquiries may be made by calling:
(972) 423-0090
Contents Summary n iii
Contents Summary
Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Chapter 1 Introduction to JavaScript . . . . . . . . . . . . . . . . . . . 1
The World Wide Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Competition on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Extending HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
CGI Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Helpers, Plug-ins, Applets, and CGI. . . . . . . . . . . . . . . . . . . . . . . . . 3
Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Plug-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
CGI (Common Gateway Interface) . . . . . . . . . . . . . . . . . . . . . . . . 4
What is JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Who Can Benefit from JavaScript?. . . . . . . . . . . . . . . . . . . . . . . . . . 5
The History of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
JavaScript in a Browser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Client-side JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Browser Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What Can JavaScript Do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Banners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
JavaScript and Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
JavaScript Resembles Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
JavaScript Differs from Java . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
JavaScript and Java are Complementary Languages . . . . . . . . . . . . . . 12
JavaScript’s Current Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Future of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 2 HTML Primer . . . . . . . . . . . . . . . . . . . . . . . . . . 14
HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Chapter 3 Writing Your First JavaScript . . . . . . . . . . . . . . . . . 21
Essential Authoring Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Choosing a Text Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Choosing a Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
vi n Contents
Function Definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Control Structures in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Alert Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Confirm Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Prompt Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Nested if-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
for…in Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
with Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Chapter 5 Object-Oriented Programming. . . . . . . . . . . . . . . . . 68
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Using the Array Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Object Based Versus Object Oriented . . . . . . . . . . . . . . . . . . . . . . . 73
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Chapter 6 Functions and Variable Scope . . . . . . . . . . . . . . . . 75
Variable Scope and Storage Class. . . . . . . . . . . . . . . . . . . . . . . . . . 75
What are Functions?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Defining and Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Using the Argument Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Creating Functions with Default Arguments . . . . . . . . . . . . . . . . . . 85
Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Tracing Values in Recursive Functions . . . . . . . . . . . . . . . . . . . . . 90
Variables and Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . 91
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Chapter 7 Utilizing JavaScript Operators . . . . . . . . . . . . . . . . 93
Operator Categories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Mathematical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
viii n Contents
Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Modulus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The Nonexistent Integral Division Operator . . . . . . . . . . . . . . . . . . 95
Increment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
String Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Bitwise AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Bitwise OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Bitwise XOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Bitwise NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Shift Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Zero-fill Right Shift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Equality and Inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Short-Circuit Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . . . 108
Logical OR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Logical AND. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Logical NOT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Testing Equality for Floating-Point Numbers . . . . . . . . . . . . . . . . . 110
More Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Data Type Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Void Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Side Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 8 JavaScript by Example—A Review . . . . . . . . . . . . . 116
Height and Weight Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The Task. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
convertInput() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
getHeight(sex, age, height) . . . . . . . . . . . . . . . . . . . . . . . . . 126
getWeight(sex, age, weight) . . . . . . . . . . . . . . . . . . . . . . . . . 128
printResult(height, weight, sex, age) . . . . . . . . . . . . . . . . . . . . 128
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Contents n ix
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Chapter 12 Time and Date in JavaScript . . . . . . . . . . . . . . . . . 192
The Date Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Creating a Date Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Parameters of the Date Constructor. . . . . . . . . . . . . . . . . . . . . . 193
Date Numeric Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Date Method Categories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
get Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
getYear() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
getMonth() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
getDate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
getDay() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
getHours() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
getMinutes() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
getSeconds(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
getTimezoneOffset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
getTime() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
The set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
setYear() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
setMonth(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
setDate(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
setHours() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
setMinutes(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
setSeconds(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
setTime() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
toGMTString() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
toLocaleString() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
parse Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
parse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
UTC() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Time-Related Methods of Other Objects . . . . . . . . . . . . . . . . . . . . . 206
setTimeout() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
clearTimeout() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Time and Date Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
A Simple Digital Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Digital Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
getTime() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
leapYear(year) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
getDays(month, year) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
getMonthName(month) . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
setCal() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
drawCal(firstDay, lastDate, date, monthName, year). . . . . . . . . . . . 221
Random Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Contents n xi
drawDot() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
getRadian(deg) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
getSpot(deg) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
get3DigitNum(num) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
printDeg(deg) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
drawLine(deg) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
drawCurve(lastDeg, jump). . . . . . . . . . . . . . . . . . . . . . . . . . 244
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
General Plotting Utility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
root(a, b) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
logab(a, b). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
factorial(a) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
startWizard() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
drawBlank(num) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
drawDot(x, y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
replaceSpecialSequence(str). . . . . . . . . . . . . . . . . . . . . . . . . 253
splitFunc(func) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
getInput() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
yVal(xVal) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
makeArray(min, max, dif) . . . . . . . . . . . . . . . . . . . . . . . . . . 256
printUnit(num) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
drawCurve() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Chapter 14 Handling Strings . . . . . . . . . . . . . . . . . . . . . . . 259
String Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Creating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
String Length. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
HTML Formatting Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
General String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
charAt() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
indexOf(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
lastIndexOf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
substring() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
escape and unescape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Number-to-String Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Empty String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . 265
String Instance Construction. . . . . . . . . . . . . . . . . . . . . . . . . . 266
The toString() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
String-to-Number Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . 268
parseInt() and parseFloat() . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Determining if a Value is a Number or Not . . . . . . . . . . . . . . . . . . 269
Evaluating Text Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
String Handling Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Contents n xiii
alinkColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
linkColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
vlinkColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Output Methods and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
write and writeln . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Data Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Chapter 20 Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
What are HTML Forms?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
JavaScript Form Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
forms Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Form Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
onSubmit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
onReset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
submit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
reset(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Element Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Using this with Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . 348
Utilizing the form Property . . . . . . . . . . . . . . . . . . . . . . . . . . 351
text Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
onBlur. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
onChange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
onFocus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
onSelect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
blur() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
focus(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
select() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
defaultValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
password Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
xvi n Contents
defaultChecked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
select Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
onBlur. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
onFocus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
onChange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
blur, focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Properties (select Object) . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
selectedIndex. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Properties (options Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
defaultSelected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
selected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
The Option Object—Adding Options Using the Option Constructor . . . . 398
fileUpload Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Chapter 21 Links, Anchors, and Image Maps . . . . . . . . . . . . . . 403
Defining a Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Defining an Anchor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Defining an Image Map Area . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
links Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
anchors Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Link and Area Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Calling Event Handlers Explicitly . . . . . . . . . . . . . . . . . . . . . . . 406
onClick. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
onMouseOver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
onMouseOut. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Link and Area Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
xviii n Contents
onLoad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Demonstration 1: Updating Digital Clock . . . . . . . . . . . . . . . . . . . . 454
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
setClock() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
update() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
stopClock() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
getHour(place) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
getMinute(place) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
getAmpm() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
getPath(url) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Demonstration 2: LED Sign . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
letterArray(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
drawBlank() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
setLight(state, x, y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
drawLetter(letter, startX) . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
drawSpace(startX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
computeIndex() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
floodBoard(startX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
drawMessage(num) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
startSign() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
stopSign() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
More Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Demonstration 3: Netris Deluxe . . . . . . . . . . . . . . . . . . . . . . . . . 478
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
drawScreen() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
computeIndex(x, y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
state(x, y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
setSquare(x, y, state) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
clearActive(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
checkMoveX(step) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
checkMoveY() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
moveX(step). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
smartX(x, y, step) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
moveY() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
smartY(x, y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
shapeMap() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
getRandom(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
insertShape() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
complexWarp() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
checkWarp(startX, startY, endX, endY) . . . . . . . . . . . . . . . . . . . . 506
rotate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
flood(state) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
noActive() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
isLine(y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
warp(startX, startY, endX, endY) . . . . . . . . . . . . . . . . . . . . . . . 508
start() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
xx n Contents
dropLines() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
play() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
characteristics(x, y) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
fullMap() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
pause() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Chapter 24 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
What are Frames? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Creating Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Targeting Frames. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Nested Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
JavaScript and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
parent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
top . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
An Example: The Color Center . . . . . . . . . . . . . . . . . . . . . . . . . . 523
The Frame-setting Window . . . . . . . . . . . . . . . . . . . . . . . . . . 523
display(r, g, b). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
makeAttribute(r, g, b) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
select(r, g, b) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
curAttribute(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
bodyDefinition() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
update() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
save() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
The “control” Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
display(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Event Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
The “swatches” Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
The “main” Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Frames, Events, and Event Handlers . . . . . . . . . . . . . . . . . . . . . . . 537
Using Event Handlers in a Frame . . . . . . . . . . . . . . . . . . . . . . . 537
Using Event Handlers in a Frame-Setting Document . . . . . . . . . . . . 537
onFocus and onBlur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Emulating Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Targeting Multiple Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Chapter 25 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
What are Windows? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Opening a Window with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . 540
Referencing Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Window Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Contents n xxi
Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
HTML-Based Remotes (Recommended) . . . . . . . . . . . . . . . . . . . 545
JavaScript-Based Remote. . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Closing a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Scrolling a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Windows, Events, and Event Handlers . . . . . . . . . . . . . . . . . . . . . . 550
An Example: Accessing a Window’s Objects . . . . . . . . . . . . . . . . . . . 553
getNewLine() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
getInput(func, form) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
vertical() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Chapter 26 Evaluation and Compilation . . . . . . . . . . . . . . . . . 568
Evaluating a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Function References and Calls . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Compiling Code as a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Specifying the Function’s Body . . . . . . . . . . . . . . . . . . . . . . . . 571
Using the Function Object with a Variable . . . . . . . . . . . . . . . . . . 572
Specifying Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Using the Function Object with an Event Handler . . . . . . . . . . . . . . 573
Properties of the Function Object . . . . . . . . . . . . . . . . . . . . . . . 574
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Chapter 27 General Software Detection . . . . . . . . . . . . . . . . . 576
Browser Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
The navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Using the navigator Object to Detect the User’s Browser and Platform . . 577
The User Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Determining the User’s Browser . . . . . . . . . . . . . . . . . . . . . . . 578
Redirecting the User to a Browser-Specific Page. . . . . . . . . . . . . . . 578
Using Frames to Display Different Pages for Each Browser. . . . . . . . . 579
Checking if Java is Enabled . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Chapter 28 Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Embedding a Plug-in Object in HTML . . . . . . . . . . . . . . . . . . . . . . 582
Referencing Plug-ins in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . 583
Determining Installed Plug-ins with JavaScript . . . . . . . . . . . . . . . . . 583
Properties of the mimeTypes Object . . . . . . . . . . . . . . . . . . . . . . . 584
Properties of the plugins Object . . . . . . . . . . . . . . . . . . . . . . . . . 586
LiveAudio and LiveVideo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
LiveAudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
LiveVideo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Other Plug-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
xxii n Contents
Acknowledgments
Writing a book is a daunting task, and it is never the work of a single person. I would
like to take the time to thank several people who were indispensable in making this
project happen:
Tomer and Yehuda Shiran, who wrote the first edition of this book. They gave me a
fabulous foundation to work with for this second edition.
Jim Hill and Wes Beckwith, the wonderful people at Wordware Publishing who labored
through this with me.
Derick Richards, who did a technical review of this book.
Last but not at all least, my wife, Misty, who did not complain at all when I was spend-
ing hours in my den typing away.
Introduction n xxvii
Introduction
Prerequisites
JavaScript is a scripting language designed to be used within HTML documents. For
this reason a basic working knowledge of HTML is required before you can learn or
use JavaScript. For those readers either lacking this basic knowledge or requiring a
refresher course, Chapter 2 is a basic HTML primer that will teach you all of the skills
you will need.
A Programming Refresher
Chapter 3 is a brief crash course in basic JavaScript to help readers with no prior
knowledge of JavaScript as well as those who require a brief refresher. This chapter
covers all the fundamental elements of JavaScript and will prepare the reader to con-
tinue on with the material in this book.
examples should work fine for you. Older versions of both browsers, particularly 3.0
and earlier, lacked support for some JavaScript features.
Programming Style
As both an aid to the reader and to illustrate good programming practices, I use a uni-
form style throughout this entire text and in all the sample code you will find on the
companion CD. For example, all variables are in Hungarian notation, meaning the vari-
able is preceded by one to three letters designating the type of variable it is. For
example, an integer being used as a loop counter might be named intcounter, and a
float that holds an account balance might be named fltbalance. I also have a tendency
to comment very heavily, some would say too much. This comes from teaching and
creating code samples for students. I hope you will find this helpful:
for (var intcounter = 0; intcounter < 10; ++intcounter)
{
[JavaScript statements]
}// end of for loop
A lot of my style preferences are just that, preferences. It is hard to call one style right
and another wrong. However, I strongly suggest that when you are writing code you
consider the fact that other programmers will probably need to read your code at some
point. They will have absolutely no idea what you were thinking when you wrote your
code. I try to write code in such a way that even a novice programmer with no prior
knowledge of the project at hand could easily deduce the intended purpose of the code.
Reading Examples
It seems obvious but read the examples thoroughly. Make sure you have a firm under-
standing of how and why they work. It is also helpful to actually run them. If you don’t
wish to run them, at least read them thoroughly and make certain that you fully under-
stand every line of code and what it does.
takes more than 15 minutes to find will turn out to be something simple.” In my expe-
rience, complex or fundamental errors are easy to find, though they may be quite hard
to fix. Errors that take a long time to find are almost invariably simple mistakes.
Chapter 1
Introduction to JavaScript
registered daily. Each domain name is eventually associated with a web server or a vir-
tual server. Your web site is truly useless if people do not visit it. There are several
ways to attract web surfers to a site:
n Advertising. You can advertise your web site on pages that belong to other com-
panies or individuals by placing some text or images on their page. The user loads
the page and sees a banner at the top of the page; when he or she clicks the ban-
ner, the advertiser’s site is loaded. Many large companies also tend to advertise
via traditional methods such as newspapers, magazines, television, and radio.
Although this method is somewhat less efficient, it works surprisingly well for
well-established companies.
n Submission. Once you have a web site, people should know that it exists. Most
people and small businesses do not have enough resources to advertise via ban-
ners that can cost up to tens of thousands of dollars a month. The most common
free promotion method is to submit the URL and description of a web page to
search engines and directories. Those services provide an easy way of retrieving
desired web pages. For example, a search engine returns URLs of pages that are
related to a specific topic that the user searched for. AltaVista, Excite, and Lycos
are just some examples of such search engines; there are hundreds available on
the web. Directories such as Yahoo! gather web pages by subject so the user can
find pages related to a given subject.
n Attractive site. The most important way to attract users to your site is by creat-
ing an engaging site. Most web surfers tend to stick to sites that they have visited
before and where they have found interesting, new material every visit. The most
“effective” way to deter people from revisiting your site is by allowing it to
become stale.
Extending HTML
HTML is a somewhat limited document-formatting language. It is based on tags, which
instruct the browser how to display a chunk of text or an image. As such, the HTML is
limited to a static, one-way interaction with the user. The web page can be as sophisti-
cated and attractive to the surfer’s eyes as it can possibly be, but the user cannot
interact with the page. Interaction cannot be static—it requires constructs such as if
statements and for loops, which are not part of the HTML syntax.
These missing constructs are found in JavaScript. This object-oriented language pro-
vides web page authors with the power to reach a very high level of interaction
between the user and the document. The power of the language is best witnessed
from the pace of its evolution. Browser providers are coming out with new extensions
and features every few months, leapfrogging each other in their constant quest for
market dominance. The leading browser provider companies are thus setting the stan-
dards in web page authoring features, with JavaScript being of special importance.
Introduction to JavaScript n 3
CGI Programs 1
You are probably familiar with popular search engines such as Yahoo!, AltaVista, and
Chapter
Excite. Have you asked yourself how these engines work? They are based on CGI
(Common Gateway Interface) scripts. CGI extends the interaction between the user
and the document to a higher level where the document communicates with the web
server on which the web pages reside. When you click the Submit button on a form,
your browser sends your entries from the HTML form to the server, where the CGI
script processes the data and sends it to another program running on the server. The
results are then transmitted to the user’s browser.
CGI scripts can be written in any language, Perl and C being the most popular ones.
Objectively, Perl and C are more complicated to program than JavaScript, and there-
fore, CGI scripting is usually mastered only by experienced programmers.
You are now in a position to understand why search engines need such powerful
machines to run their devices. A typical search engine receives several million
requests per day. For example, suppose a provider needs to answer one million search
requests per day. A simple calculation shows that that server must process approxi-
mately ten one-second-long requests per second. Each of these ten requests is a CGI
script executing on the server.
Helpers
Browsers are very powerful in converting HTML tags into attractive pages, as well as
interpreting JavaScript scripts to animated commercials, but they have some deficien-
cies in other areas such as understanding audio that comes in from a web site. Your
browser, though, is smart enough to use helper applications to do such jobs instead.
The conversion of digitized sound to audio you can hear through your speakers is done
by a program that resides on your hard disk and is launched by the browser.
Plug-ins
Another way to extend browser capabilities is by plugging an application into it. At this
stage we will not discuss plug-ins, because a full chapter is dedicated to these mecha-
nisms and their powerful connections with JavaScript.
4 n Chapter 1
Java Applets
OK, so you have tried HTML, JavaScript, helpers, and plug-ins (in this order) and you
are still not satisfied with your web page. Let’s say you want to place a rotating globe
on your page. Sun Microsystems answered this need by developing a language for the
web called Java. Talented programmers can use Java to build small applications
(applets) that are downloaded to the browser upon hitting the appropriate page and
then automatically discarded from memory when a new page is loaded. The applet
concept is similar to the image file concept. As the .gif file is loaded upon hitting the
page, which calls the appropriate image, so is the applet loaded upon the browser’s
request. The applet, though, is more than a static combination of pixels; it is an inde-
pendent program that executes when the page loads and is automatically terminated
when the page unloads. The applet is a self-sustained, independent module, without
any possibilities for user interaction during its execution.
The classic example of an applet is a scrolling LED banner, similar to the scrolling sign
at Times Square. There is no better way to inform your visitors about changes you
have made to your page. You can change your banner whenever you want to bring new
items to the user’s attention. This touch of animation can bring your page to life and
create the perception of a dynamic, up-to-date site.
For a browser company to support Java applets, it needs to license the technology
from Sun and build the proper hooks in the browser. The first one to do it was Sun
itself, with its HotJava browser. Netscape incorporated it later, as did Microsoft. Java
applets are assets that people are accumulating, and every browser must support them
to keep up with the nonstop competition.
You have probably asked yourself about the origin of Java. The Java language is derived
from C and C++ and is targeted for more experienced programmers. Therefore, many
web page authors and casual programmers cannot adopt the language as easily as they
have adopted HyperCard, ToolBook, or Visual Basic. JavaScript was created to answer
this exact need.
What is JavaScript? 1
JavaScript is an easy-to-use object scripting language designed for creating live online
Chapter
applications that link together objects and resources on both clients and servers.
While Java is used by programmers to create new objects and applets, JavaScript is
designed for use by HTML page authors and enterprise application developers to
dynamically script the behavior of objects running on either the client or the server.
JavaScript’s design and concepts represent the next generation of software for the
Internet. JavaScript is:
n Designed for creating network-centric applications
n Complementary to and integrated with Java
n Complementary to and integrated with HTML
n Open and cross-platform
With JavaScript, an HTML page might contain a form that processes data on the client
side. A server-side JavaScript might pull data out of a relational database and format it
in HTML on the fly. A page might contain JavaScript scripts that run on both the client
and the server.
JavaScript in a Browser
Client-side JavaScript
Client-side JavaScript is the most common form of the language. The script should be
included in or referenced by an HTML document for the code to be interpreted by the
browser. Suppose you load a web page with a JavaScript-generated banner. The
browser begins interpreting the HTML code and then encounters a JavaScript script.
If the script has no syntax errors, it is executed by the browser’s built-in interpreter.
The JavaScript client-side mechanism features many advantages over traditional CGI
server-side scripts. For example, you might use JavaScript to check if the user has
entered a valid e-mail address in a form field. The JavaScript function is executed
when the user submits the form, and only if the entry is a valid e-mail address
(includes an @) is the form transmitted to the server for further processing, database
storage, and so on. In this case, the user does not need to wait until the form is
Introduction to JavaScript n 7
submitted over the network to the server and back to the client, only to be informed
that a single character is missing. 1
Browser Objects
Chapter
JavaScript is an object-based programming language. Its built-in object model is mostly
based on the HTML content of the given web page. The tight interaction between
JavaScript and other browser objects (such as forms, browser windows, frames, and
images) provides full control over various page elements and enables the programmer
to create a link between “external” objects and “internal” ones. A classic example for
such linking is a JavaScript script that invokes a Java applet from an HTML form.
JavaScript exposes objects and properties related to the browser’s window, history list,
status bar, frames, forms, links, and so forth. Furthermore, JavaScript can be used to
trap user-initiated events such as button clicks, link navigation, and other actions that
the user explicitly or implicitly initiates. You can create a distinct script for each event,
enabling a smooth, logical interaction with the user.
Games
You can create many interesting games with JavaScript. During our JavaScript pro-
gramming experience we have written tic-tac-toe games, a mastermind game, a Tetris
game, and many others. There are basically two types of games you can create via
JavaScript:
n Static games
n Dynamic games
Static games are usually logical ones, and thus do not require much action or immedi-
ate responses. For example, you can play tic-tac-toe without any live action, because
even reloading the entire page for each move does not interfere with the game itself
(although it might be very boring). Static games do not require image replacements or
animation. Dynamic games, on the other hand, require real-time action and animation.
For example, a Tetris game requires that the blocks will fall down the screen smoothly
without having to wait each time for the screen to be refreshed. Dynamic games are
often resource intensive and therefore must be programmed with efficiency in mind.
Small, simple games are usually embedded in existing web pages, whereas large, com-
plex games are most often placed on a separate page.
8 n Chapter 1
Banners
JavaScript is a full scripting language, so you can create an infinite number of distinct
scripts. Nevertheless, some implementations became more popular than others due to
the fact that they are easy to use and create interesting effects. Banners are a classic
example of such scripts. A banner is a script that displays text in the browser’s status
bar or in a form’s text field. The most common browsers are those that scroll and
those that type in messages sequentially.
Form Validation
JavaScript is tightly connected to browser objects, including forms and their elements;
therefore, it provides a great amount of control over forms. A classic form-related
script is one that validates a form’s fields before it is submitted and cancels the sub-
mission to the server if an error is found in one of the fields. The advantage of using
JavaScript in this particular example is that client-side validation is much faster than
validation via transmission to the server and back to the client. For example, suppose
you have a form that accepts data from the user, including his or her e-mail address.
Let’s assume that the e-mail address is essential, and that a string containing an “@”
character is assumed to be a valid e-mail address. There are two options:
n You can choose not to validate the entries. In that case, when the user clicks the
Submit button, the form is submitted to the server where a CGI script checks if
the e-mail address field is valid and then processes the form. If the e-mail address
does not contain an “@” character, the submission is denied and the appropriate
message is returned to the user.
n You can choose to use client-side validation via JavaScript. In that case the validat-
ing script is loaded as plain text (JavaScript code embedded in the HTML code).
When the user clicks the Submit button, a JavaScript function is triggered to vali-
date the form. If the e-mail address seems valid, the form is submitted to the
server—otherwise an appropriate message is immediately displayed and the sub-
mission is canceled. Note that the user does not have to wait for the form to be
submitted to the server and back to the client, only to receive a message inform-
ing him or her that the e-mail address field contains an invalid entry.
Miscellaneous
I have covered three important JavaScript implementations. However, like all other
languages, it is obviously impossible to cover all or most of the possible scripts. You
can use JavaScript to create almost anything you desire. For example, you can create
an LED sign, which once could be created only with Java.
Bear in mind that client-side JavaScript is executed on the user’s machine. That is,
you do not have any direct access to the server’s properties with client-side
JavaScript. Although that may seem to be a disadvantage, it is sometimes very conve-
nient to have access to client-side properties rather than server-side ones. For
example, you can create a digital clock that shows the current time in the client’s time
Introduction to JavaScript n 9
zone, because JavaScript extracts the time from the client’s operating system, be it
Windows 95/98/2000, Macintosh, Unix, or any other OS. 1
The full control over browser objects is what makes client-side JavaScript so powerful.
Chapter
You can pop up windows and use them as remote controls linked to the original win-
dow. You can use one link to perform several operations or load various documents in
separate frames simultaneously. You can also use JavaScript to create an online calcu-
lator or to draw graphs and curves. To attract visitors, you can enhance a web page
with JavaScript in many different ways.
We have introduced a few client-side JavaScript scripts in this section. Although client-
side JavaScript is by far the most important and useful, there are many other imple-
mentations of the language. For example, you can use JavaScript for server-side
applications using the LiveWire compiler. Server-side JavaScript is actually an alterna-
tive to traditional CGI programming via Perl and C++. JavaScript is a very convenient
language; thus, it is used alongside many other languages (such as VRML) and for var-
ious purposes. Another example of using JavaScript is for automatic proxy
configuration. As you might already know, Netscape Navigator enables you to connect
through a proxy server. You can configure the proxies manually by entering the correct
values or entering the URL of an automatic proxy configuration script, which is actu-
ally a JavaScript code. The JavaScript code is located in a text file on the server but is
not compiled like server-side JavaScript. You should know by now that there is plenty
to do with JavaScript, and JavaScript is now one of the most important languages for
web development. We will cover the most important implementations of the language
in this book.
Notice the similarity. The only difference is the variable declaration (JavaScript is
loosely typed).
10 n Chapter 1
JavaScript and Java are both based on objects, but their implementations differ. In both
languages, many built-in functions are implemented as properties and methods of vari-
ous objects.
Chapter
tures dynamic binding, so all object references are checked at run time. Java, on the
other hand, is based on static binding, meaning that all object references must exist
and be valid at compile time. However, an object-oriented language may require
dynamic method bindings because polymorphism allows multiple definitions of meth-
ods sharing a common name, and calling such polymorphic methods often cannot be
resolved until run time. The most obvious reason for this difference is that JavaScript
is not compiled, so checking object references at compile time has no meaning. If you
are not familiar with Java, you should pay no attention to these confusing terms—sim-
ply bear in mind that the object implementation in JavaScript varies widely from that
in Java.
When you write a script, you likely need to use variables. In order to use a variable,
you must declare it. Another difference between Java and JavaScript is that Java is
strongly typed, as opposed to JavaScript, which is loosely typed. That is, when you
declare a variable in Java, you must specify its data type. For example, when you cre-
ate a variable to contain integer values you must use the int keyword. In JavaScript,
all variables are declared in the same way. Furthermore, a variable of one data type can
contain a value of a different data type elsewhere in the script. This behavior resem-
bles other popular languages such as Perl. This topic is discussed later in the book.
I have intentionally left the most critical difference to the end. You may have noticed
that all differences discussed thus far are related to the language itself. A very impor-
tant difference between Java and JavaScript is that JavaScript is integrated with, and
embedded in, HTML. Although Netscape Navigator 3.0 introduced an alternative in
the form of external files, that is still the general situation. Java, conversely, is neither
integrated with nor embedded in HTML. Applets (small Java programs) are always
distinct from the HTML code and are only invoked by it. A space is reserved on the
web page for later execution if needed. If you have some experience creating HTML
documents, JavaScript’s implementations will be a piece of cake for you. When you
create the HTML document for a web page, you simply embed the JavaScript script
according to its syntax and semantics.
Another significant difference between Java and JavaScript is the area in which they
are executed. A Java applet resembles an image in that you give it a specified area on
the page, and the applet executes independently in that area. Within the defined area,
the applet can do virtually anything, including animation, games, and so forth. An
applet does not have any effect outside its area. JavaScript, on the contrary, gives you
access to the entire web page. You can affect various elements in the page, provided
that JavaScript is given permission to access them. For example, you can reference
any form, link, image, and browser window via JavaScript. Netscape Navigator 3.0
introduced a new feature, LiveConnect, which enables Java to interact with JavaScript
and vice versa. This tool makes it possible for a Java applet to interact with HTML ele-
ments through JavaScript, which is the connector in this case.
12 n Chapter 1
Java is not just a language for creating applets that run on web pages; it is also a gen-
eral-purpose language that can create stand-alone applications. That is, you can create
any application with Java, without any connection to the Internet or the World Wide
web. The classic example for a stand-alone application written entirely in Java is
HotJava, the web browser created by Sun Microsystems. On the other hand,
JavaScript is not a stand-alone programming language. Like most scripting languages,
JavaScript is interpreted and thus requires a specific application to run it. JavaScript
currently works with Netscape Navigator, Microsoft Internet Explorer, the LiveWire
server environment, and plug-ins. JavaScript applications will probably never function
independently.
Java requires an appropriate multitasking operating system. Therefore, anyone run-
ning Unix platform, OS/2, Windows 95/98/2000, Windows NT, or Macintosh should not
have a problem viewing Java on the web. Nonetheless, Windows 3.1 or Windows 3.11
users cannot benefit from Java because that operating system does not support
multitasking. Also, some users running multitasking operating systems cannot run
Java applets from a web page. This happens when a user is connected via a proxy
server that disables Java due to possible security flaws. As opposed to Java,
JavaScript’s audience is not limited due to any operating system. Windows 3.1x users
can enjoy JavaScript-enhanced pages exactly like other users. That is obviously a
major benefit of JavaScript over Java.
program is compiled, it will run on any machine with the designated operating system.
Therefore, your script must be suitable for all browsers, or at least the most popular 1
ones. This problem is specific for JavaScript, not for all interpreted languages. For
example, when you create a CGI script via Perl, you must only make sure that the Perl
Chapter
interpreter installed on the server meets the script’s needs. If so, it will work for any
browser that supports CGI (more than 99.99% of all browsers).
JavaScript is supported by many companies. At the same time Netscape Communica-
tions and Sun Microsystems announced JavaScript, more than 28 companies also
announced that they would be endorsing JavaScript as the open scripting standard for
the Internet, and many companies indicated that they were considering licensing the
technology to include it in their own products.
This list is obviously an abbreviated one and does not include all possible uses of
JavaScript.
14 n Chapter 2
Chapter 2
HTML Primer
This chapter is designed to give you the basics of HTML. If you have no prior knowl-
edge of HTML, then it is critical that you thoroughly study this chapter before
proceeding. However if you are an experienced HTML programmer, you may wish to
skim this chapter or skip it entirely. JavaScript is a scripting language that is embed-
ded into HTML documents in order to add significant functionality to those web pages.
For this reason, a working knowledge of HTML is integral to understanding and using
JavaScript.
HTML, or Hypertext Markup Language, is a relatively simple scripting language that
web browsers can use to display web pages. The beautiful part of HTML is that you
don’t need any special software to do it; HTML can be written in any text editor,
including the Windows Notepad. Just remember to save the file as an .htm or .html
file. HTML has had a long history and has gone through a number of revisions. Each
successive revision adds more functionality to HTML and with the current version
(Version 4.0), HTML is a very powerful language that can take some time to learn.
Fortunately, most work on web pages can be done with just the essentials of HTML
and that is what this chapter will teach you. Now let’s start with the basics of HTML.
HTML Tags
The first question, is how do we get the web browser to know that our document has
HTML codes for it to read? Simply put, there is more to creating a web page than sim-
ply taking a document and changing its extension to .htm. At the beginning of your
document you place the <HTML> command and at the end you put </HTML>; the web
browser will know that the codes in between are supposed to be HTML.
<HTML>
put code here
</HTML>
Now that is pretty simple you must admit. But this web page won’t do much of any-
thing at all. So let’s do the obligatory “Hello World” sample that every programming
book starts off with. It will show you how to input text and some basic HTML.
HTML Primer n 15
<HTML>
<HEAD>
<TITLE>My First HTML Page</TITLE>
</HEAD>
<BODY>
<P><CENTER>
<B><FONT SIZE="+2">Hello World</FONT></B>
</CENTER></P>
</BODY>
</HTML>
Believe it or not, this little snippet shows you most of what you need to know about
2
HTML. To begin with, note that everything is contained between the <HTML> and
Chapter
</HTML> tags. These two commands define the beginning and the end of the HTML
document. The web browser will ignore any items outside these commands. Next we
have a section that is contained between the <HEAD> and </HEAD> commands. This is
the header portion of your HTML document. The <TITLE> and </TITLE> commands
contain the title that will actually appear in the title bar of your browser.
Then we have the <BODY> and </BODY> commands. As you might have guessed, this is
the body of your HTML document. This is where all of your web page’s code (includ-
ing scripting code like JavaScript) is going to go. Inside the body section is some text
and some additional commands that will define how the text will appear in the browser.
The <P> and </P> commands define the beginning and the end of a paragraph. The <B>
and </B> commands tell the browser to make whatever text is between them bold.
The <FONT SIZE="+2"> tells the browser how big the text should be (there are a vari-
ety of methods for doing this, as we shall see). The </FONT> command ends the font
section of the HTML code.
By now I trust you have noticed a pattern. All the above commands have an opening
command and a closing command. This is true for all but very few HTML commands.
Just remember this rule: You close the commands in opposite order of how you opened
them. Notice in the above sample code I opened the commands before the text like
this: <P><CENTER><B><FONT SIZE="+2">, and then closed them like this:
</FONT></B></CENTER></P>. This is important to remember. You can think of it as
“backing out” of your commands.
Now this gives you a very simple web page that displays one phrase in bold. Admit-
tedly, this is not very impressive, but if you understand the concepts involved with
using the HTML commands, then you understand HTML. Now let us move on to
some more HTML. Usually web pages contain more than simply a title and some text.
Other items you might put in a web page include images and links to other web pages.
Placing an image on an HTML document is rather simple:
<IMG SRC="imagepath\imagename" WIDTH=52 HEIGHT=88 ALIGN=bottom>
You simply provide the path to the image and the name of the image, including its file
extension (such as .gif, .bmp, .jpg, etc.). The other properties in this command allow
you to alter the placement and size of the image. You can alter its width and height as
well as its alignment.
16 n Chapter 2
This link will connect to the URL (Uniform Resource Locator) contained within the
quotation marks. In order to use this methodology to create an e-mail link, simply use
this:
<A HREF="mailto:somebody@somemail.com">
You simply have to change the “http://” portion to “mailto:”. Notice that all three of
the preceding methods have one thing in common. They do not close the command in
the typical manner that other HTML commands are closed. Now let’s examine the
source code for a simple but complete HTML document:
<HTML>
<HEAD>
<TITLE>Test HTML Page</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<B><FONT SIZE="+2"></FONT></B>
<P><CENTER><B><FONT SIZE="+2">My First Web Page </FONT></B>
</CENTER>
<P>I am learning HTML. I <B><I>LOVE</I></B> HTML!
</P>
<P><CENTER><IMG SRC="CompuBook.gif" WIDTH=52 HEIGHT=88
ALIGN=bottom></CENTER></P>
<P><CENTER>You can e-mail me at</CENTER></P>
<P><CENTER><A HREF="mailto:myemail@someemail.com">E-mail ME</A>
</CENTER></P>
<P><CENTER>Or go to this publisher's Web Site </CENTER></P>
<P><CENTER><A HREF="http://www.wordware.com">WordWare Publishing</A></CENTER></P>
</BODY>
</HTML>
You can change the background color of your page using this command and any stan-
dard color. You can also set a background image for your HTML document with a
similar command:
<BODY background="mypicture.gif">
Chapter
Figure 2-1.
Your first
web page.
Now I will be the first to admit that this sample web page is very trivial. But it does
contain the basics of HTML. With the material we have covered so far, you can display
images, texts, links, e-mail links, background colors, and background images. Not too
bad for just a few short pages. You may also want to look at the accompanying CD in
the sample HTML folder to see several sample HTML documents for various pur-
poses. These can be used as templates for your own web pages. Examining these can
give you a deeper understanding of basic HTML.
Let’s examine a few other simple items we can add to our HTML documents. The
first is altering text color. You can set the default text color for the entire document
and you can alter the color of specific text. You alter the default text color for the
entire document using a technique very similar to the one used to alter the back-
ground color of the document:
<BODY TEXT="blue">
This text simply tells the browser that unless otherwise specified, all text in this docu-
ment should be blue. In addition to changing the default color of all text in a document,
you may wish to simply change the color of a specific section of text. This is fairly easy
to do as well. Instead of using the <BODY TEXT> command we use the <FONT COLOR>
command:
<FONT COLOR="red">This is red text</FONT>
This, like the other color commands, can be used with any standard color.
18 n Chapter 2
There are a wide variety of tags you can use to alter the appearance and behavior of
texts and images. A few others for you to consider are the <BLINK> and </BLINK> tags
which, as the name implies, causes the text to blink (this is only supported by
Netscape and will not work in Internet Explorer). Another example is <STRIKE>
</STRIKE>, which causes the text to appear with a line through it, a strikethrough.
The tags we have covered so far are enough to allow you to accomplish what you need
in HTML.
The last HTML command we are going to examine is <TABLE>. You frequently see
tables on web pages and they are a very good way to organize data on your web page.
You can use the tables with or without a border.
Here is how you create a table with a border:
<TABLE BORDER=1>
<TR>
<TD>
<P>This
</TD><TD>
<P>Is a
</TD></TR>
<TR>
<TD>
<P>Table
</TD><TD>
<P>With a border
</TD></TR>
</TABLE></P>
By now you should be able to recognize that the <TABLE> and </TABLE> tags actually
contain the table. Each <TR> tag designates another row in the table. <TD> and </TD>
create a cell within that row. Using those three tags you can create a table with any
number of rows or columns. Notice in the first line of this code that BORDER is set to 1.
This means the border has a width and is therefore visible.
In some instances, you may not want the border to show. Tables can be used simply to
hold images and text in relative positions. In cases such as these you may not want the
border to show. Below is an example of a table whose borders will not show:
<P><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
<TR>
<TD>
<P>This
</TD><TD>
<P>is a
</TD></TR>
<TR>
<TD>
<P>Table
</TD><TD>
<P>With no borders or padding
HTML Primer n 19
</TD></TR>
</TABLE></P>
Notice that BORDER, CELLSPACING, and CELLPADDING are all set to 0. This causes the
table itself to not be displayed. However, the contents of the table will display. You
should also notice that in both examples there is text in each cell.
Figure 2-2 shows a web page with two tables, one with a border and one without.
Chapter
Figure 2-2.
A web page
containing
two tables.
Since the purpose of this book is to teach you JavaScript, the obvious question on your
mind should be, how do I insert scripts into HTML? Well, fortunately that is not par-
ticularly difficult. The browser deals with script code (including JavaScript) like it
handles HTML code, scanning the source from left to right and from top to bottom.
JavaScript has “tags” of its own by which it instructs the browser and determines the
layout. However, JavaScript is not read exactly like HTML. In HTML, the browser
acts immediately according to the elements it recognizes. Not all JavaScript code
refers to actions that take place while the page is loading. Some parts are just kept in
memory until they are called. For instance, if you write a function and do not call it,
the browser does not do anything with it. This part of the script stays in memory, and
can be invoked later. But the real question you are wondering before you proceed with
the rest of this book and learning JavaScript is, how do I put scripts into HTML docu-
ments? Any script can be placed very easily by encasing the script you write inside of
two commands:
<SCRIPT LANGUAGE = "whateverscriptlanguageyouareusing">
20 n Chapter 2
and
</SCRIPT>
There is a variety of scripting languages available for the web including VBScript
(based on the Visual Basic programming language), CGI, and of course, JavaScript.
Below is an example of JavaScript inserted into HTML code:
<SCRIPT LANGUAGE="JavaScript">
alert("Wonderful!")
</SCRIPT>
Figure 2-3.
An alert box.
Let me stress that what I have just covered are simply the essentials of writing HTML
code. You can find a number of books that go into HTML in more depth than this.
Learn Advanced HTML 4.0 with DHTML by José Ramalho from Wordware Publishing
is a good choice if you want to get serious about HTML.
There is also a variety of software packages that will do the HTML code for you. You
simply create a document just as you would in any word processor and the software
creates the underlying HTML code. Two very inexpensive options that I recommend
for basic HTML coding are Claris Works Home Page (about $25) or Netscape Com-
poser, which is included with Netscape Communicator.
Other documents randomly have
different content
"There must be some reason for it," said Tei. "Of course they
cannot have a Dolls' Festival if they do not keep their dolls. But still
there is no need to keep the dolls if they never have a festival."
The two children stood back and looked at the shelves. On the
step below the emperors knelt the court musicians, some playing on
the koto, some on the samisen, and others beating tiny drums.
There were also many court ladies, dressed in lovely silks and
crêpes, their black hair fastened with jeweled hairpins.
At that moment she caught sight of a baby doll tied to the back of
its nurse, and it reminded her of something very pleasant.
"I held my new baby brother in my arms this morning," she said.
"I am glad of the honorable baby," said Umé, "because now you
are permitted to share the Festival of the Dolls with me."
Umé clapped her hands. "We will go as soon as all the dolls have
had their breakfast," she said. "I will strap Haru on my back, and
you shall strap your new doll on your back, and we will play that
they are truly babies."
She sprang to her feet as she said it, and danced up and down
the room, clapping her hands and singing a queer little tune.
"I have the most honorably best time in the whole year when the
Dolls' Festival comes," she cried.
It was not to be wondered at. Then all the dolls and toys and
games that little girls love to play with are set out on the shelves in
the honorable guest room; and for three days they have a holiday
from school and play all the day long.
The doll-shops are always merry with children waiting to buy dolls
and crowded with dolls waiting to be bought. But there were so
many interesting things to see in the streets that Tei and Umé were
a long time in reaching the doll-shop.
Once they stopped to watch the firemen who ran past them on
their way to a fire.
The fire-stations in Tokio are tall ladders which are made to stand
upright in the street, with a tub at the top in which the watchman
sits. This tub looks like a crow's-nest on the mast of a vessel. Beside
it is a big bell which the watchman strikes when he sees a fire
anywhere.
As the two little girls watched the men running to the fire with a
little box of a hand-engine, and with the beautiful standard in the
lead, they thought it a fine sight.
"Tara says he is going to be a fireman when he grows up," said
Umé. "He says it is because a fireman gets so many presents."
Tei shook her head. "It is a sad thing when a fire burns a
thousand houses as it did in our city last year," she said. "I do not
like to think of it."
"We need have no fear," said Umé lightly. "Our fathers have extra
houses packed away in their godowns."
"That is true," said Tei, "but many others are not so wisely
fortunate."
Just then they reached the doll-shop and the fires were forgotten.
The click of wooden clogs, the clank of Japanese money, and the
merry talk of the children, all trying to be heard at the same time,
made it a jolly affair.
The doll chosen by Tei was the one which was being admired by
two other little girls at the same moment. It was a boy baby with
pink cheeks and black eyes and a little fringe of very black hair; and
it was dressed in a lovely red silk kimono covered with yellow
chrysanthemums.
"It is very like the new brother at home," said Tei, as she counted
out the sen and gave them to the doll-shopman.
Then she strapped the doll on her back and the two little girls
went home slowly, talking of the wonderful baby brother who had
come to Tei's house the week before.
"The house has to be very quiet, because the honorable baby is
not yet well," said Tei. "He has been very ill. I could not have gone
with you to the city streets on your birthday if the baby had been
well. Every one was glad to have me out of the house, so that it
might be kept very still."
CHAPTER IV
When Umé and Tei reached home, carrying their dolls on their
backs, they found Yuki on the veranda.
"My geta! Yuki's geta!" the baby called as soon as she saw her
sister coming down the garden path; and she stood on one clog and
held up the other little white-stockinged foot.
Small as she was, Yuki-ko could slip her feet into her wooden
clogs without any help when she could find them; but Saké, the dog,
generally found them first and as there was never a bone for him to
hide, he liked to hide the tiny shoes.
Now, as usual, one of the clogs was missing from the flat step
where the baby had last left it.
"Perhaps it is under the plum tree, O Yuki San," said Umé, and
ran to find it, but it was not there.
"The dog of the man who made the dead trees to blossom,"
answered Umé as she looked under the quince bushes; but the
missing clog was not there. Several days later the gardener found it
buried under the bush of snow blossoms; but Umé gave up looking
for it when she did not find it in any of Saké's favorite places.
"It is such a long time since I heard the story of the good man
who made trees blossom, that I have nearly forgotten it," said Tei;
but Umé was talking to Yuki.
"Be happy, little treasure-flower," she said to the baby. "You shall
have a new pair of clogs; and you may come with us now and help
serve tea to the honorable dolls."
Baby Yuki forgot her clogs at once. She knelt upon the floor and
held up her tiny hands for the tea-bowl.
"Oh, Umé! She is too little to whip the tea," said Tei when she
saw that her cousin meant to give the baby a bowl of tea powder
and a bamboo brush with which to whip it into foam.
"I will watch her," answered Umé. "It may be that the dolls forget
all they learn about the tea-ceremony when they are shut up in the
godown for a whole year. While I am teaching Yuki San, they may
learn it all over again by most carefully watching us."
Just then there was a happy little gurgle from the baby.
Umé turned quickly to see what she was doing. "O Yuki San! Yuki
San!" she cried, running to the rescue.
But it was too late! While Umé had been talking with Tei, the
baby had been pouring the tea over her head. She was still holding
the bowl above her head when Umé looked, and the water was still
trickling down over her hair and into her eyes.
"Naruhodo!" said old Maru, as she brought towels and wiped the
tea from the baby and the mat with many exclamations of
amazement.
But Yuki San was soon tired of sitting still. She like to watch the
tea powder foam in the bowl, but when she tried to put her tiny
hands into the dish and play they were fishes, Umé gave her a doll
and sent her off to play by herself.
"It will never do for the dolls to see such unworthy actions," Umé
told Tei. "They will think it is all a part of the august tea-ceremony."
It was much easier to teach the dolls without the baby's help, and
there was everything to teach them with. There was a toy kitchen
with its charcoal brazier, its brushes and dishes. There was a toy
work-box with thread, needles and silk.
There were toy quilts and wooden pillows and flower vases; and
there were toy jinrikishas with their runners.
Umé and Tei taught the dolls the proper bowings for the street
and those for the house. They changed the food on the trays, and
taught the girl dolls that they must most carefully wait upon the boy
dolls, as Umé herself had been taught to wait upon Tara, although
she was older than her brother.
Umé even read aloud with much emphasis from the "Book of
Learning for Women": "Let the children be always taught to speak
the simple truth, to stand upright in their proper places, and to listen
with respectful attention."
There are many other directions in the book, all of which the little
women of Japan learn by heart. Umé would have read many of the
rules to the dolls, but her mother called both children to leave their
play and go with the grandmother and old Maru to listen to story-
telling in the street of theaters.
"It is a very different thing to tell the simple truth at one time and
to listen to honorable stories at another," said Umé to the dolls as
she left them.
In the street of theaters are many little booths in which there are
men who tell the most enchanting stories. Sometimes they tell fairy
stories, sometimes ghost stories, and sometimes stories of Japanese
gods and heroes. Umé and Tei liked the fairy stories best of all.
"The old man in this booth tells fairy stories faithfully well," said
the grandmother as they stopped before a tiny house decorated with
paper parasols and lanterns, and with a long red banner floating
above it from a bamboo pole.
Maru gave the woman four sen and the little party entered and
joined a group of about twenty women and girls who were seated
on mats in front of the story-teller.
"Hear, now, the story of the good old man who made dead trees
to blossom!" said the story-teller, waving his fan over his head and
then clapping it in his hand three times to call attention to his words.
Umé and Tei looked at one another and clasped their hands
beneath their chins.
"Once upon a time two men lived side by side in a little village,"
said the story-teller, looking at Umé. Umé again nodded her head.
She knew the story perfectly well, but the Japanese children love to
hear the same stories told over and over again.
"One of these men was kind and generous," continued the story-
teller. "The other was envious and cruel. Neither one of them had
any children to pay them honor in their old age; but the kind man
and his wife were always doing good. One day they found a dog
which they took to their home and taught as they would have taught
a child, to be obedient and faithful.
"They named the dog Shiro, and fed him with the mochi cake
which tastes best after the New Year is made welcome with much
joy and ceremony."
"But Shiro knew nothing about the New Year festival," went on
the story-teller. "He was happy all the day long in following the good
old man about and getting a kind pat from the gentle hand.
"'Yes, truly,' answered the other and sent Shiro home with his
neighbor, although the obedient creature had always been driven
away from the neighbor's gate with sticks and harsh words.
"'Now you must find treasure for me,' said the bad man who
knew nothing about kindness to animals, for he pushed the poor
dog's nose into the earth so deeply that Shiro was nearly smothered.
"The dog did truly begin scratching, but when the cruel man dug
in that place, he found nothing but rubbish, which so enraged him
that he killed the obedient animal and buried his body under a pine
tree.
"At last the good man, wondering why Shiro did not return, went
to his neighbor and asked the reason. 'Ah, he was a bad dog!'
answered the other. 'He would find nothing but rubbish in the
ground for me, and so I killed him and he lies under the pine tree.'
"'It was a great pity to kill him,' said the good man. 'We should be
kind to all animals, because it may be that the souls of our ancestors
return and live in their bodies.'
"So Shiro's master bought the tree, cut it down and took it
home."
Umé and Tei nodded again. The mystery was to begin in the story
and they drew closer to the grandmother.
"The spirit of the little dog spoke to his master in the night," said
the story-teller, "and told him to make a tub from the pieces of the
tree. It must be just such a tub as the mochi-makers use at New
Year's time, and in the tub the old man must make mochi for Shiro.
"So the good old man did as he was bidden, thinking to put some
of the cakes before the tablet on the god-shelf as an offering to the
spirit of the obedient dog.
"But when he put the barley into the tub and began to pound it,
the quantity of barley increased until there was all that the man and
his wife could use for their needs for a long time.
"This also, the envious neighbor saw, and he borrowed the tub as
he had borrowed the dog, thinking to have as much barley meal for
himself.
"But although the tub overflowed with the grain, it was all
worthless; so poor that no one could eat it. A second time the man
was angered and he pounded the tub to pieces in his rage.
"The patient old man gathered up the pieces and used them for
fire-wood, saving the ashes as the spirit of Shiro directed him to do.
"In his garden there was an old dead tree. The spirit of the dog
bade him sprinkle some of the ashes upon the branches of this tree
and he obediently did so.
"People from far and wide flocked to see the sight, and among
them was a prince who begged the old man to do the same thing for
one of his trees which had long been dead.
"It is a good thing that the story was no longer," said Umé,
"because Tara is going to help me build a toy garden for my dolls."
Tara helped to build the garden, to be sure, but the two little girls
waited upon him and listened to him, and not once forgot that in
Japan girls and women must follow their brothers. They must never
try to lead them.
"Go and get the spade from the garden-house, Umé," Tara said to
his sister. "Bring some small stones from the rockery," he told Tei,
and both little girls obeyed without a word.
At the end of the third day of the Dolls' Festival there was a
charming toy garden at one end of the veranda. In the garden there
was a tiny lake bordered with flowering shrubs, a little hill with trees
growing around it, a path leading to the lake beside which grew
peach trees in full bloom, and there were even two tiny stone
lanterns and a little temple on the hill.
It had been a wonderful holiday for the little girls and they were
sorry that it was all over, but they cheerfully helped to pack the dolls
and toys away in boxes and carry them back to the godown.
CHAPTER V
"O Haha San," said Umé, "when we took little Yuki San to the
temple for the first time, with whom did I sit in the jinrikisha?"
"It is not strange that you have no memory of it, little Plum
Blossom," said her mother.
"Because you were ill from eating too many sweets the day
before, and had to stay at home in your bed."
"Was Tara taken to the temple when he was thirty days old?"
"Yes, my daughter."
"With O Ba San."
"It is time for them even now to begin the journey," answered her
mother. "You may perhaps ride in the same jinrikisha with your little
cousin."
Umé made a deep bow to her mother, slipped into her clogs at
the veranda step, and ran swiftly through the garden to her cousin's
house.
"Yes, you may come with me," said Tei to Umé, after asking the
honorable father's permission.
The pale little mother leaned back in her jinrikisha beside the
nurse who carried the beautiful boy.
The father, very proud to have a son who would carry on the
family name, rode in the first jinrikisha, and the little party took their
way to the famous Kameido Temple in the eastern part of the city.
"It was not until three days ago that the baby was well enough to
have his head shaved," Tei confided to Umé.
Tei shook her head. "The august father commanded that it should
not be done," she said. "The baby was so frail that there have been
no visits from anyone since he was first seen in our house."
"Then the baby might just as well have been a girl," said Umé
decidedly.
"Oh no!" said Tei. "There have been dozens of presents of rice
and silk, and many other things. And there have been letters of
congratulation. And to-day, when we return from the temple, many,
many people will come to see the baby, because they could not
come before."
"What name was given to the baby on the seventh day?" asked
Umé curiously.
Before Umé could ask any more questions they had reached the
temple.
Umé would have almost preferred feeding the fish to seeing the
ceremony of placing the new baby under the protecting care of the
patron saint of the temple. Baby Onda's father had chosen the God
of Learning to be his son's patron saint. He wished to have the child
become very studious and know thoroughly all the wisdom of
Confucius and the old, old gods of learning and wisdom.
Before going into the temple everyone slipped out of his clogs,
washed his hands, and made several bows at the entrance.
Tei's father then pulled a rope which rang a bell to attract the
attention of the god. There was a moment when he clapped his
hands together three times to be sure that the god was listening.
After that he asked very earnestly that his little son might be
carefully guarded and guided along the rough path of wisdom. Then
he clapped his hands twice to show that his prayer was ended.
It was so solemn and impressive to little Umé that she forgot her
rice-cake and let it drop to the temple floor as she clasped her own
hands in prayer.
Then followed the gift to the gods, and one to the priest of the
temple. The priest blessed the new baby and he was safely placed
under the care of Sugawara-no-Michizanè, the God of Literature, in
the Kameido Temple in the city of Tokio.
The ceremony was not very long. The moment it was over Umé
and Tei stole as quickly as they could out of the temple, and ran
down to the lake where the goldfish were waiting to be fed.
Of course they stayed there so long, feeding first one fish and
then another, and watching them spread their fan-like tails and glide
away to nibble the bits of rice-cake, that Tei's father came to look for
them.
The jinrikisha runners were told to hurry home, and they obeyed
so well that Umé and Tei clung to one another and gave little shrieks
of delight.
Hardly had they reached home when the guests really did begin
to arrive. All the relatives and friends came by ones and twos and
threes; some in jinrikishas and some on foot,--all who had sent
presents and all who had waited to bring them.
Umé and Tei counted the different pairs of clogs that were left at
the veranda steps, and there were over one hundred pairs.
But there was little time to count and look. The two children were
needed to help pass tea and cakes to the visitors. It was dark before
everybody was at last gone and the baby's first party was over.
"He is an honorable boy child," answered her mother. "A boy must
learn early to bear hardships."
CHERRY-BLOSSOM TIME
"The cherry trees in Ueno Park are in full blossom to-day," read
Umé's father in the morning paper. "The Emperor visited the park
yesterday to see the beautiful flowers."
"Foolish Umé!" said her mother, but she smiled at the child's
fancy.
"The joy began to grow with the first pink buds," Umé went on,
"and now that all the cherry trees everywhere are in blossom,--in
our garden, in Tei's garden, and in all the gardens; along the streets
and river banks, and in all the parks, my heart is bursting with
gladness."
"When hearts feel that way," said her mother, "it is because they
wish to offer thanks to the gods. We will all go to the temple to-day
and leave a gift, and then we will go to the beautiful Ueno Park,
where there will be many others who feel the way that you do in
their hearts."
"It is the way we Japanese always feel when the cherry trees
hang out their pink garlands," said Umé's father.
Tara was bouncing a ball in the garden and heard this talk about
the cherry blossoms.
"Wait until my festival," he said, "and then you will see what it is
really like to feel gladness."
"Your festival," said Umé, "and pray what may your honorable
festival be?"
Umé looked puzzled for a moment, then she cried, "Oh, he means
the Flag Festival!"
"Come, children," interrupted their mother, "find the lunch boxes
and help to put all in peaceful readiness for our journey to the park."
Tara picked up Baby Yuki and gave her a toss into the air. In
doing so he discovered that she had lost her name-label. It is a
common thing for a Japanese child to wear a wooden label tied
around his neck, on which his name and address are printed. Then if
he is lost he can be returned to his home.
Tara made a new label and tied it so firmly around the baby's
neck that her tiny fingers could not possibly loosen the strings.
"Now, O Yuki San," he said, "you are all ready to go to the park,
where you can get lost a dozen times if you wish, honorable Sister,"
and he gave her another toss for good luck.
In the meantime Umé found that her clog string was broken. "I
may as well get a new string for each clog," she said. "When one
breaks, I find that the other soon breaks also, for loneliness."
Yuki San, and not Saké, was the thief this time. She had put them
carefully away in one of the drawers of the writing cabinet the day
before, when she was playing that her shoe was a doll-baby and
must be tied to her back with its strings.
By the time they were all dressed in their finest clothes, three
jinrikishas were waiting at the gate, and Tara rode off proudly with
his father, while Baby San sat beside her mother, and Umé rode with
her grandmother.
"The pink mist almost hides the blue sky," said Umé, "but the
sunshine comes dancing through. See how gently it touches the pink
petals with its rosy light!"
The little party rode through the park looking at the cherry trees
and watching the crowds of people. Umé kept her poor
grandmother's head bobbing to right and left as she spoke of one
strange sight and then another.
First it was, "O Ba San, look at the Japanese baby in the American
baby-carriage. It cannot be that he likes it as well as riding on his
sister's back."
Next it was, "O Ba San, see the little foreign children playing with
the cake-woman's stove."
Umé would have liked to stop the jinrikisha man and watch the
white-faced children as they made little batter cakes and fried them
over the charcoal.
"We must not stop now," said her grandmother. "Your honorable
father will tell us when we may stop."
Umé, who had never worn a hat in her life, could say nothing to
that. Every little foreign girl she saw was wearing a hat on her head
on which there were many flowers of half a dozen different colors
and kinds. Although it was a sight to hurt her eyes, Umé would have
been glad to leave the jinrikisha and study the dresses of the little
foreigners. Most of all she wished to join them in their play of cake-
making.
"To ride among the beautiful cherry trees, with their delicious pink
odors, in the beginning," said Umé. "I know that in no other country
can the trees be so lovely and hold so many flowers."
The man danced and sang a funny song about the troubles of
Daruma, a snow man. Once in a while he beat the drum, and all the
time he was jumping and twisting about until it seemed as if his tray
of candies must surely fall off his head to the ground; but it never
did.
When the monkey jumped from his master's shoulder and
snatched off one of the boys' caps, putting it on his own head, all
the people, big and little, screamed with joy.
The swish of the quick flight of a rocket into the air made every
one look up. In a moment a big paper bird popped out of the rocket
and came sailing slowly down to light on the top of one of the trees.
Then another rocket, and still another, was sent up, and from one
came a golden dragon with a long red tongue and a still longer tail.
Umé's father dismissed all of the jinrikisha coolies, and after they
had watched the fireworks a little while, the family went into a tea-
house to eat their lunch and rest from the confusion.
"There has been much fighting with real swords here in this very
park," his father continued. "There was once a big battle under
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!
ebookball.com