100% found this document useful (10 votes)
75 views79 pages

Advanced JavaScript 2nd Edition by Chuck Easttom ISBN 155622852X 9781556228520 Instant Download

The document provides information about various textbooks related to JavaScript and other computer science topics, including their authors, ISBNs, and download links. It features titles such as 'Advanced JavaScript 2nd Edition' by Chuck Easttom and 'JavaScript and jQuery: The Missing Manual 2nd Edition' by David Sawyer McFarland. Additionally, it includes a detailed contents summary for 'Advanced JavaScript', outlining chapters on JavaScript fundamentals, object-oriented programming, and more.

Uploaded by

dufelprivez2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (10 votes)
75 views79 pages

Advanced JavaScript 2nd Edition by Chuck Easttom ISBN 155622852X 9781556228520 Instant Download

The document provides information about various textbooks related to JavaScript and other computer science topics, including their authors, ISBNs, and download links. It features titles such as 'Advanced JavaScript 2nd Edition' by Chuck Easttom and 'JavaScript and jQuery: The Missing Manual 2nd Edition' by David Sawyer McFarland. Additionally, it includes a detailed contents summary for 'Advanced JavaScript', outlining chapters on JavaScript fundamentals, object-oriented programming, and more.

Uploaded by

dufelprivez2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Advanced JavaScript 2nd Edition by Chuck Easttom

ISBN 155622852X 9781556228520 download

https://ebookball.com/product/advanced-javascript-2nd-edition-by-
chuck-easttom-isbn-155622852x-9781556228520-13370/

Instantly Access and Download Textbook at https://ebookball.com


Get Your Digital Files Instantly: PDF, ePub, MOBI and More
Quick Digital Downloads: PDF, ePub, MOBI and Other Formats

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/

Object Oriented JavaScript 2nd Edition by Stoyan Stefanov, Kumar


Chetan Sharma ISBN 9781849693134 1849693137

https://ebookball.com/product/object-oriented-javascript-2nd-
edition-by-stoyan-stefanov-kumar-chetan-sharma-
isbn-9781849693134-1849693137-20218/

Superviviente 1st Edition by Chuck Palahniuk

https://ebookball.com/product/superviviente-1st-edition-by-chuck-
palahniuk-14964/

JavaScript and jQuery The Missing Manual 2nd Edition by McFarland,


David Sawyer ISBN 1449399029 9781449399023

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/

Superviviente 1st edition by Chuck Palahniuk ISBN 009928264X


978-0099282648

https://ebookball.com/product/superviviente-1st-edition-by-chuck-
palahniuk-isbn-009928264x-978-0099282648-11848/

Advanced Practice Nursing Procedures 2nd Edition by Margaret Colyar


ISBN 1719642397 9781719642392

https://ebookball.com/product/advanced-practice-nursing-
procedures-2nd-edition-by-margaret-colyar-
isbn-1719642397-9781719642392-6384/

JavaScript Mini FAQ 1st Edition by Danny Goodman ISBN

https://ebookball.com/product/javascript-mini-faq-1st-edition-by-
danny-goodman-isbn-11420/

Advanced Nursing Practice 2nd Edition by Paula McGee,George Castledine


1405102349 9781405102346

https://ebookball.com/product/advanced-nursing-practice-2nd-
edition-by-paula-mcgee-george-
castledine-1405102349-9781405102346-814/
Advanced
JavaScript
Second Edition

Chuck Easttom

Wordware Publishing, Inc.


© 2001, Wordware Publishing, Inc.
All Rights Reserved
2320 Los Rios Boulevard
Plano, Texas 75074

No part of this book may be reproduced in any form or by


any means without permission in writing from
Wordware Publishing, Inc.

Printed in the United States of America

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

Chapter 1 Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Chapter 2 HTML Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Chapter 3 Writing Your First JavaScript . . . . . . . . . . . . . . . . . . . . . . . 21
Chapter 4 JavaScript Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . 34
Chapter 5 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . 68
Chapter 6 Functions and Variable Scope . . . . . . . . . . . . . . . . . . . . . . . 75
Chapter 7 Utilizing JavaScript Operators . . . . . . . . . . . . . . . . . . . . . . . 93
Chapter 8 JavaScript by Example—A Review. . . . . . . . . . . . . . . . . . . . 116
Chapter 9 Events in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Chapter 10 Building and Extending Objects . . . . . . . . . . . . . . . . . . . . . 145
Chapter 11 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Chapter 12 Time and Date in JavaScript . . . . . . . . . . . . . . . . . . . . . . . 192
Chapter 13 JavaScript Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Chapter 14 Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Chapter 15 Browser Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Chapter 16 Utilizing the Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Chapter 17 URLs and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Chapter 18 Using the History List . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Chapter 19 The document Object—Colors, Output, and Properties . . . . . . . . 322
Chapter 20 Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Chapter 21 Links, Anchors, and Image Maps . . . . . . . . . . . . . . . . . . . . 403
Chapter 22 Implementing Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Chapter 23 Images and Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Chapter 24 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Chapter 25 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Chapter 26 Evaluation and Compilation . . . . . . . . . . . . . . . . . . . . . . . 568
Chapter 27 General Software Detection . . . . . . . . . . . . . . . . . . . . . . . 576
Chapter 28 Plug-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
iv n Contents Summary

Chapter 29 LiveConnect—Connecting JavaScript, Java, and Plug-ins . . . . . . 593


Chapter 30 JavaScript Extensions for Layers . . . . . . . . . . . . . . . . . . . . 621
Chapter 31 Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Chapter 32 Security Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Chapter 33 Debugging Scripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
Chapter 34 JavaScript and VRML . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Chapter 35 JavaScript Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . 658
Appendix A HTML-Generated Object Map . . . . . . . . . . . . . . . . . . . . . . . 667
Appendix B JavaScript Object Specification Syntax . . . . . . . . . . . . . . . . . 670
Appendix C ASCII Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
Appendix D Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Contents n v

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

Basic JavaScript Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22


The <SCRIPT> Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Internal Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
External Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Hiding the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Problems with Code Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Alternate Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Placing JavaScript Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Using the Semicolon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
JavaScript is Case Sensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Printing JavaScript Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Updating a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Chapter 4 JavaScript Fundamentals . . . . . . . . . . . . . . . . . . . 34
“Building” a Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Data Types in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
The Value Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
JavaScript Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Type Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Mixing Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Decimal Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Octal Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Hexadecimal Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Converting from Decimal to Other Bases . . . . . . . . . . . . . . . . . . 41
Converting from a Specified Base to Decimal . . . . . . . . . . . . . . . . 42
Referring to Octal and Hexadecimal Integers . . . . . . . . . . . . . . . . 42
Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Making Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Multiple Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Nested Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Evaluating Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Testing Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Contents n vii

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

Chapter 9 Events in JavaScript . . . . . . . . . . . . . . . . . . . . . 132


Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Event Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
onLoad and onUnload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
onError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Emulating Events via Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Error Message Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Calling Event Handlers Explicitly. . . . . . . . . . . . . . . . . . . . . . . . . 142
Cancelling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Chapter 10 Building and Extending Objects . . . . . . . . . . . . . . . 145
Creating Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Constructor Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Defining Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Nested Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Creating Objects—An Example . . . . . . . . . . . . . . . . . . . . . . . . 153
Prototype and Object Extensions . . . . . . . . . . . . . . . . . . . . . . . . . 155
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Chapter 11 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
What is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Some History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Creating Simple Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Referring to an Array’s Elements . . . . . . . . . . . . . . . . . . . . . . . . . 161
Creating Dense Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Array Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Array Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Array Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
chop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
grep() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
join() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
pop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
push() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
reverse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
shift() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
sort() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
splice() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
split(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
unshift() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The arguments Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Creation with a Single Constructor Function . . . . . . . . . . . . . . . . . 185
Creation without a Constructor Function . . . . . . . . . . . . . . . . . . . 186
Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Populating an Associative Array . . . . . . . . . . . . . . . . . . . . . . . . 189
Splitting a String into an Associative Array . . . . . . . . . . . . . . . . . . 190
x n Contents

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

Chapter 13 JavaScript Math . . . . . . . . . . . . . . . . . . . . . . . 226


Math in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
The Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
E. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
LN2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
LN10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
LOG2E. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
LOG10E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
PI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
SQRT1_2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
SQRT2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Math Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Arithmetic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
abs(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
ceil() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
exp() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
floor() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
log() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
max(), min() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
pow() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
random() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
round() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
sqrt() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Trigonometric Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
cos(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
acos() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
sin() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
asin() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
tan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
atan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
atan2() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
The Number Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Number Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
MAX_VALUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
MIN_VALUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
NEGATIVE_INFINITY . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
POSITIVE_INFINITY . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Number Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Math-Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
parseInt() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
parseFloat(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
eval() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Math Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Creating a Curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
drawBlank(num) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
xii n Contents

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

String Enciphering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270


encipher(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
encode(str, key) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Chapter 15 Browser Objects . . . . . . . . . . . . . . . . . . . . . . . 274
The Object Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Creating Browser Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Topmost Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Chapter 16 Utilizing the Status Bar . . . . . . . . . . . . . . . . . . . 278
The Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Writing to the Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Setting a Default Value for the Status Bar . . . . . . . . . . . . . . . . . . . . 281
Banners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
T-banner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
stopBanner() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
startBanner() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
showBanner() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Event Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
R-banner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
stopBanner() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
startBanner() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
clearState() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
getRandom(max) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
getString() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
showBanner() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Event Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
N-banner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
scrollBanner(seed) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Chapter 17 URLs and JavaScript . . . . . . . . . . . . . . . . . . . . . 295
A Crash Course in URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
General URL Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
URL Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Hypertext Transfer Protocol (HTTP) . . . . . . . . . . . . . . . . . . . . 296
File Transfer Protocol (FTP) . . . . . . . . . . . . . . . . . . . . . . . . 296
Gopher Protocol (Gopher) . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Electronic Mail (Mailto) . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Usenet News (News) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
xiv n Contents

Host-Specific File Names (File) . . . . . . . . . . . . . . . . . . . . . . . 297


JavaScript Statements (javascript) . . . . . . . . . . . . . . . . . . . . . 297
Other Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
location Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
location Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
href . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
hostname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
pathname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
port. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
location Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
reload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Another Location—document.location (document.URL) . . . . . . . . . . . . 312
Search Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Chapter 18 Using the History List . . . . . . . . . . . . . . . . . . . . 318
What is a History List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The history Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
History List Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
History List Entry Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
history Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
forward. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
go. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Security Aspects of the history Object . . . . . . . . . . . . . . . . . . . . . . 321
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Chapter 19 The document Object—Colors, Output, and Properties . . 322
The document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
The title Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
bgColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
display(triplet) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
drawCell(red, green, blue) . . . . . . . . . . . . . . . . . . . . . . . . . . 328
drawRow(red, blue) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
drawTable(blue) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
drawCube() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
toHex(dec) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
setbgColor(red, green, blue). . . . . . . . . . . . . . . . . . . . . . . . . 332
fade(sred, sgreen, sblue, ered, egreen, eblue, step) . . . . . . . . . . . . 332
Event Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
fgColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Contents n xv

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

HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365


JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Methods and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
textarea Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Inserting New Line Characters . . . . . . . . . . . . . . . . . . . . . . . . 368
Handling textareas By Line . . . . . . . . . . . . . . . . . . . . . . . . . . 368
hidden Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
button, submit, and reset Objects . . . . . . . . . . . . . . . . . . . . . . . . . 370
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
onClick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
click() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
checkbox Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
onClick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
click() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
checked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
defaultChecked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
radio Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
HTML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
JavaScript Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
onClick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
click() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
checked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Contents n xvii

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

Location-Equivalent Properties . . . . . . . . . . . . . . . . . . . . . . . . 410


target. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Referring Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Chapter 22 Implementing Cookies . . . . . . . . . . . . . . . . . . . . 413
Maintaining State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Cookies and HTTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Setting an HTTP Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Getting an HTTP Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Notes and Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
First Transaction Sequence Example . . . . . . . . . . . . . . . . . . . . 416
Second Transaction Sequence Example. . . . . . . . . . . . . . . . . . . 416
Cookies and JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Cookie Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
getInput(num, monthName) . . . . . . . . . . . . . . . . . . . . . . . . . . 431
initCookie(monthName) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
getSpecificReminder(num, monthName) . . . . . . . . . . . . . . . . . . . 432
setSpecificReminder(num, monthName, newValue) . . . . . . . . . . . . . 432
Outliner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
item(parent, text, depth) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
makeArray(length) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
makeDatabase() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
setStates() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
setImages() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
toggle(num) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
setStorage() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Global Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Chapter 23 Images and Graphics . . . . . . . . . . . . . . . . . . . . . 444
Defining Images in HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
The Image Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
The images Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
border . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
complete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
hspace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
lowsrc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
src . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
vspace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
onAbort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
onError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Contents n xix

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

Chapter 29 LiveConnect—Connecting JavaScript, Java,


and Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . 593
What is LiveConnect? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Enabling LiveConnect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
The Java Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Netscape Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Compiling Java Code with Netscape Packages . . . . . . . . . . . . . . . . 594
Java to JavaScript Communication . . . . . . . . . . . . . . . . . . . . . . . . 595
Passing Values Between Java and JavaScript . . . . . . . . . . . . . . . . . 595
Getting a Handle for the Browser Window . . . . . . . . . . . . . . . . . . 596
Accessing JavaScript Objects and Properties . . . . . . . . . . . . . . . . . 596
Calling JavaScript Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Other Methods in the JSObject Class . . . . . . . . . . . . . . . . . . . . . 598
JavaScript Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
JavaScript to Java Communication . . . . . . . . . . . . . . . . . . . . . . . . 599
Accessing Java Directly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Controlling Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
The applet Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
What Else is Scriptable in an Applet? . . . . . . . . . . . . . . . . . . . . 605
Modifying an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Why Make an Applet Scriptable? . . . . . . . . . . . . . . . . . . . . . . 612
Controlling Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Accessing a Plug-in’s Elements . . . . . . . . . . . . . . . . . . . . . . . 612
JavaScript and LiveAudio . . . . . . . . . . . . . . . . . . . . . . . . . . 612
JavaScript and LiveVideo . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Chapter 30 JavaScript Extensions for Layers . . . . . . . . . . . . . . 621
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Defining a Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
LEFT and TOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
WIDTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
CLIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Z-INDEX, ABOVE, and BELOW . . . . . . . . . . . . . . . . . . . . . . . 625
VISIBILITY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
BGCOLOR and BACKGROUND . . . . . . . . . . . . . . . . . . . . . . . 626
The layer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
width and height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
left and top . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
zIndex. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
visibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
clip.top, clip.left, clip.right, clip.bottom, clip.width, and clip.height . . . . 628
background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
bgColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Contents n xxiii

siblingAbove and siblingBelow . . . . . . . . . . . . . . . . . . . . . . . 629


above and below . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
parentLayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
offset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
moveTo() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
resize() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
moveAbove() and moveBelow() . . . . . . . . . . . . . . . . . . . . . . . 630
Demonstration: A Pictorial Album of U.S. Presidents . . . . . . . . . . . . . . 630
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Chapter 31 Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . 634
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Content Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Font Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Text Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Inheritance of Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Creating Style Sheets and Assigning Styles . . . . . . . . . . . . . . . . . . . 636
Defining Styles with the <STYLE> Tag in the Header . . . . . . . . . . . 636
Specifying Styles for Individual Elements . . . . . . . . . . . . . . . . . . 637
Defining Classes of Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Format Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Box Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Replaced Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Setting Margins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Settting Border Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Settting the Padding Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
Chapter 32 Security Issues . . . . . . . . . . . . . . . . . . . . . . . . 641
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
URL to URL Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
The Concept of Tainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Enabling Data Tainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Specific Tainting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Window Taint Accumulator. . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Chapter 33 Debugging Scripts . . . . . . . . . . . . . . . . . . . . . . 646
Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
“string is not defined” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
“string is not a function” . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
“unterminated string literal” . . . . . . . . . . . . . . . . . . . . . . . . . . 649
“missing } after function body” . . . . . . . . . . . . . . . . . . . . . . . . 649
“string is not a number” . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
“string has no property named property” . . . . . . . . . . . . . . . . . . . 649
xxiv n Contents

“string has no property indexed by [i]” . . . . . . . . . . . . . . . . . . . . 649


“string cannot be set by assignment” . . . . . . . . . . . . . . . . . . . . . 650
“test for equality (==) mistyped as assignment (=)?
Assuming equality test” . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
“function does not always return a value” . . . . . . . . . . . . . . . . . . 650
“access disallowed from scripts at URL_1 to documents at URL_2” . . . . 650
“Lengthy JavaScript still running. Continue?” . . . . . . . . . . . . . . . . 650
“syntax error” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Manual Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Match Those Tag Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
View the Intermediate HTML Source. . . . . . . . . . . . . . . . . . . . . 651
Reopen the File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Print Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Comment Out Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Watching and Tracing Variables . . . . . . . . . . . . . . . . . . . . . . . . 651
Getting It Right the First Time . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Build the Script’s Skeleton First . . . . . . . . . . . . . . . . . . . . . . . . 652
Keep Testing While Developing . . . . . . . . . . . . . . . . . . . . . . . . 653
Evaluate Expressions Outside Your Source . . . . . . . . . . . . . . . . . . 653
Test Your Functions Outside Your Source . . . . . . . . . . . . . . . . . . . 653
Testing Your Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
Chapter 34 JavaScript and VRML . . . . . . . . . . . . . . . . . . . . 655
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Smaller File Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Chapter 35 JavaScript Tips and Tricks. . . . . . . . . . . . . . . . . . 658
Transparent Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Browser Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Popup Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
System Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Appendix A HTML-Generated Object Map . . . . . . . . . . . . . . . . 667
Appendix B JavaScript Object Specification Syntax . . . . . . . . . . 670
Anchor Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Applet Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Area Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Array Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Button, Submit, and Reset Objects . . . . . . . . . . . . . . . . . . . . . . . . 672
Checkbox Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Date Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Document Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Contents n xxv

FileUpload Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674


Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
Hidden Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
History Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Image Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Layer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
Link Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
Location Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
Math Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
MimeType Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Plugin Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Radio Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Select Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
Text, Textarea, and Password Objects . . . . . . . . . . . . . . . . . . . . . . 683
Window and Frame Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Appendix C ASCII Character Set . . . . . . . . . . . . . . . . . . . . . 688
Appendix D Online Resources . . . . . . . . . . . . . . . . . . . . . . . 692
JavaScript Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
HTML Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Organizational Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Certification Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Employment Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
xxvi n Acknowledgments

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.

How to Read This Book


As the title suggests, this book is geared towards advanced JavaScript programming
and has the experienced JavaScript programmer in mind. However, the first few chap-
ters do provide basic primers on both HTML and JavaScript. Those with limited
JavaScript experience should carefully study the first few chapters, while more experi-
enced programmers may want to skim these chapters or skip them entirely.

The Structure of This Book


It’s important to understand how the book is structured so you can plan your learning
experience in the best possible way. The table of contents lists the chapters by name.
If you are an experienced JavaScript programmer just looking for a reference on a par-
ticular subject, feel free to skip directly to the relevant chapter.
JavaScript is a viable scripting language for both Netscape and Internet Explorer, so I
will use examples from both browsers and will point out any differences. If you have
the latest version of either Netscape or Internet Explorer, then virtually all the
xxviii n Introduction

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

Some JavaScript scripters prefer to use a different style:


for (var num = 0; num < 10; ++num){
[JavaScript statements]
}

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.

Encouragement for Nonprogrammers


Learning a scripting language is a lot easier than learning a full programming language
such as Java or C++. Fortunately, JavaScript is enough like Java that you will find it
relatively easy to leverage your JavaScript skills into Java programming as well. Just
remember that computers do exactly what we tell them to do, which may not always
be exactly what we intended them to do. Most programming errors (called “bugs”)
stem from simple mistakes. I have an axiom that I believe is true: “Any error that
Introduction n xxix

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.

Warning for Programmers


If your programming experience has been in strictly procedural languages such as C
and Pascal, hold on tight. JavaScript, like Java, is object oriented. This will be a whole
new approach to programming for you. But not to worry—this book will walk you
through it step by step. However, if you are an experienced object-oriented program-
mer, especially a C++ or Java programmer, then this book should be a pretty easy
read for you.
Introduction to JavaScript n 1

Chapter 1

Introduction to JavaScript

The World Wide Web


It may be hard for some readers to recall a time when the web was not a part of our
day-to-day lives. However, it was not until the early ’90s that the web became a seri-
ous medium for communication. Prior to that, the major uses of the Internet were
simply e-mail and file transfer. This is not to say that web pages did not exist before
then; it’s simply that they were not a widespread communication tool. By the early
21st century, however, there were millions of web sites from major corporations and
government agencies to 10-year-olds putting up web sites extolling the virtues of
their favorite cartoon. It is hard to imagine conducting business today without some
web presence, whether it’s a small web site that gives the essentials of your business
or a full blown e-commerce site.
This spread of the web has also lead to greater expectations from web pages. Ori-
ginally, simply putting up some well-organized text was enough to give you a
successful web site. But soon, web surfers became accustomed to graphics, anima-
tions, sounds, and more. All of this could be accomplished with successive versions of
HTML. Modern web users, however, expect a higher level of functionality from web
sites.

Competition on the Web


Site popularity is usually measured by the number of visits, or “hits,” a web site
receives. Major sites such as www.yahoo.com can receive tens of thousands of hits per
day. Less popular sites may only receive a few hundred hits a day, while personal web
pages may only receive a few hits per week or month. The competition to get visitors
to sites is becoming quite fierce. In fact, advertising dollars are now pouring in to sites
that can attract a significant number of visitors. The most common way to get visitors
to a site is to add content, both informational/functional and purely decorative. This is
an area for which JavaScript is ideally suited.
The World Wide Web (WWW) is constantly growing as new web servers and pages are
connected. It is estimated that over 15,000 domain names (such as netscent.com) are
2 n Chapter 1

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, Plug-ins, Applets, and CGI


Web page authoring consists of several layers. The more powerful the layer, the more
complex it is to program. The fundamental layer is HTML. As we explained earlier,
JavaScript extends HTML to a dynamic user/page interaction. Helpers, plug-ins, and
applets extend JavaScript’s capabilities even more.

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.

CGI (Common Gateway Interface)


There are some tasks that cannot be accomplished by the browser on the client side.
Building a common database for an engineering department must rely on a server
accepting data and requests from all users and storing it for future access and process-
ing. Such an application can be written in C, C++, Perl, or any other language. The
communication protocol between the server application and the client browser is
called CGI. You should buy a book on CGI if you intend to learn it, because it is far
beyond the scope of this book and is very difficult to learn without detailed
documentation.
Introduction to JavaScript n 5

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.

Who Can Benefit from JavaScript?


Generally speaking, web surfers benefit most from JavaScript, because they have the
opportunity to enjoy the language’s capabilities in the form of games, animation, inter-
action, and so forth. Netscape Communications benefited from JavaScript early on,
because it gave them a competitive advantage over other web browsers. Now, how-
ever, both Microsoft Internet Explorer and Netscape Navigator fully support
JavaScript.
Many other companies believe that they will benefit from JavaScript and its descen-
dants. The words of Rose Ann Giordano, vice president of the Internet Business
Group at Digital Equipment Corp., demonstrate it:
“Tools like JavaScript will unleash a new wave of creativity and transform the
Internet in ways no one can predict. JavaScript and other developments will
demand increased system performance, ideally met by Digital’s Alpha systems
architecture.”
Jan Silverman at Hewlett-Packard also mentioned how JavaScript matches his com-
pany’s plans in a press release:
“JavaScript is an exciting technology because it represents the next generation of
software designed specifically for the Internet. Hewlett-Packard is committed to
open standards and is a supporter of JavaScript because it complements
Hewlett-Packard’s open systems architecture.”
6 n Chapter 1

The History of JavaScript


JavaScript was originated by Netscape as LiveScript, developed in parallel with Live-
Wire server software. LiveScript was developed for several applications—we’ll
discuss two of them here. The first one is to enhance web pages in a way that HTML
cannot. The classic example is verifying a user’s form entries. Instead of sending the
data as is to the server and validating the data types there, the client handles all the
validation and only then sends the data to the server for further processing. Another
application for LiveScript (JavaScript) is as the communication glue between HTML
documents and Java applets. A scrolling banner, for example, can use information from
the user’s customized settings, sent to the applet by LiveScript. This data exchange is
transparent to the server and, since there is no server-client communication, there is
no response time penalty.
In December 1995, Sun Microsystems took over LiveScript development and changed
its name to JavaScript. On the one hand, JavaScript is related to Java in its C++ object
flavor. On the other hand, JavaScript’s vernacular is much reduced compared to Java,
in order to make it suitable for less experienced programmers and scripters.
JavaScript’s first and foremost advantage is in its ease of learning. Additionally, its fea-
tures and capabilities are important for web page interactivity as well as for
customizing solutions around prewritten applets.
In 1996 Microsoft introduced its first JavaScript-enabled browser, Internet Explorer
3.0. Netscape followed up a week later with another JavaScript-enabled browser,
Netscape Navigator. Internet Explorer was far from reaching Navigator 3.0’s level in
terms of JavaScript and lacked many important features and capabilities. In early 1997
the beta version of Netscape Navigator version 4.0 (Communicator) was released.
Microsoft Internet Explorer was still far from reaching Navigator 3.0’s level. At this
point, Internet Explorer 5.5 and Navigator 6.0 both fully support JavaScript.

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.

What Can JavaScript Do?


In this section we take a look at a few interesting effects and programs created
entirely with JavaScript. At this point we will only focus on client-side JavaScript that
is embedded in or referenced by an HTML document.

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.

JavaScript and Java


JavaScript Resembles Java
JavaScript supports most of Java’s expression syntax and basic control flow constructs.
Take a look at the following JavaScript code segment:
for (var i = 0; i < 10; ++i)
{
/* statements come here */
}

Now take a look at the Java equivalent:


for (int i = 0; i < 10; ++i)
{
/* statements come here */
}

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.

JavaScript Differs from Java


JavaScript resembles Perl in that it is interpreted, not compiled. Java is referred to as a
compiled language. Unlike most other programming languages, though, Java is not
compiled to a native machine code, but rather to a Java byte code. Java byte code is an
architecture-neutral byte-code compiled language. That is, an applet is compiled to
Java byte code and then run by a machine-dependent runtime interpreter. Therefore,
Java is much slower than general programming languages such as C++. Since Java is
compiled, the common user cannot see the actual code behind the program. Neverthe-
less, when a user comes across a JavaScript script, he or she can generally see and
even copy (legally or illegally) the code simply by using the browser to view the
HTML source that contains the script (unless it is an external script). A compiled lan-
guage has many other advantages. For example, a compiled program is much more
efficient than one that is always interpreted directly from a text file, such as Perl and
JavaScript. On the other hand, there are more than enough reasons to prefer an inter-
preted language over a compiled one. It is much easier and more convenient to debug
and modify a program by simply modifying its text file rather than having to recompile
it. Furthermore, most scripts and applets implemented in Java or JavaScript for web
usage do not require efficiency and do not demand resources. Therefore, an inter-
preted language is somewhat more convenient.
Both Java and JavaScript are based on objects. However, their implementations of
objects are different. JavaScript is an object-based language. It supports built-in, exten-
sible objects, but no classes or inheritance. Its object hierarchy is not an inheritance
hierarchy as in Java. JavaScript features two types of objects:
n Static objects—objects that combine general functions (methods) and data con-
structs (properties). The values of such objects’ properties are usually read-only.
A static object is a single object, and thus does not enable you to create instances
of it. For example, the Math object in JavaScript is a static one, because you cannot
create instances according to its template. Its methods are common mathematical
functions, whereas its properties are mostly mathematical constants.
n Dynamic objects—objects by which instances are created. A dynamic object
resembles a template. You do not touch or use the object directly. In order to take
advantage of such an object, you must create an instance of it. For example, the
Date object is a dynamic object in JavaScript. An instance of that object is associ-
ated with a given date. You can create as many instances of a dynamic object as
needed.
Java is fully extensible. A programmer can create numerous classes that group objects
together. A class is a term used in object-oriented programming vernacular to refer to
a set of related objects that share common characteristics. Java programmers create
their own extensions to the base set of tools or classes. JavaScript’s object model is
Introduction to JavaScript n 11

somewhat simpler than Java’s equivalent, so JavaScript’s object implementation is


much easier to accommodate than Java’s model. 1
Another difference between Java and JavaScript is their time of binding. JavaScript fea-

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.

JavaScript and Java are Complementary Languages


I have spoken with many people who believe that JavaScript is a scaled-down version
of Java. As you should already know, that is not true. JavaScript cannot perform many
tasks that Java can, whereas Java cannot do most things that JavaScript is capable of
doing. As of Netscape Navigator 3.0x, JavaScript and Java can interact together using
LiveConnect.

JavaScript’s Current Status


JavaScript is currently under development and will probably stay that way, as the lan-
guage specification is not yet complete. Each browser release introduces new features
to the language. Therefore, you must take special precautions when creating scripts
for the general public. First of all, you must follow the rules that enable you to hide the
code from browsers that do not support JavaScript. Secondly, if the script works only
on some JavaScript-enabled browsers, you must take that into consideration. You can
use all versions of JavaScript to find out which browser the user is running, enabling
you to provide a script compatible with the browser being used. If you implement this
technique, you can avoid all errors by disabling the script if it is not compatible with
the user’s browser. In any case, you must assure that the user does not receive any
annoying JavaScript errors that are an indication of a poorly designed web site. The
fact that JavaScript is under development has immense influence because the language
is an interpreted language, not a compiled one. When using a compiled language, you
must use an up-to-date compiler so it can compile your code. However, once the
Introduction to JavaScript n 13

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.

The Future of JavaScript


JavaScript is expected to advance in two areas. The first one is in the direction of
“serverless” CGI scripting. Small databases of information will be embedded in an
HTML document and processed by JavaScript scripts. Cutting the server-client com-
munication will sharply reduce the response time, which is still the number one
problem in surfing the Internet. The second vector is pointing to Java applets. As
applets become more and more powerful, more exciting opportunities will be available
to use JavaScript to assemble existing building blocks.
As ActiveX and VRML become more powerful, JavaScript will work in concert with
them, resulting in very powerful applications. JavaScript, a cross-platform language, is
currently in use alongside many other languages, including Java and VRML. Further-
more, it has many other uses, such as automatic proxy setting scripts. In the future,
other programs and languages will feature interaction with JavaScript. For example,
Netscape is currently using JavaScript with LivePayment, a program that enables a
web site developer to receive immediate payments over the web.
A web page designer cannot afford to ignore JavaScript because his competitors will
not, beating him in web design competition. Nevertheless, JavaScript has its limits and
cannot perform every task. A few classic JavaScript-based solutions are as follows:
n User interaction with form elements (input fields, text areas, buttons, radio but-
tons, check boxes, and selection lists)
n Distributing small collections of database-like information with friendly interfaces
to that data
n Processing the data on the client side before submission to the server
n Animation and live image swapping

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

Placing a hyperlink to another web site or to an e-mail address is just as simple:


<A HREF="http://www.wordware.com">

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 will note a new command at the beginning:


<BODY BGCOLOR="white">

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">

The compubook.gif image is stored on the accompanying CD in a folder named exam-


ples\chapter 2. If you entered the code properly and used the image located on the CD,
your web page should look something like Figure 2-1.
HTML Primer n 17

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.

"Are they not beautiful?" asked Tei, clasping her hands.

Umé looked tenderly at the lower shelves, where the more


common dolls and toys were placed. "These are like the people we
see every day, and I love them," she told Tei; "but when I look at
the emperor dolls it makes me think of our own beloved Emperor,
and I would give up all my toys for him."

"Yes," said Tei, "I would give my life for him."

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."

"Yes," added Tei, "and I am also permitted to go to the shops to-


day and buy a new doll. See all the sen the august father gave me
this morning," and Tei took a handful of coins from her sleeve
pocket.

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.

The firemen run through the streets headed by a man carrying a


large paper standard, which they place near the burning house.
They are very helpful in saving the women and children, but as they
dislike to desert their standard they are not always of much use in
putting out the fire.

House-owners give the firemen a great many presents to keep


them faithful to their duty.

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.

"Oh, the lovely dolls!" cried Umé clapping her hands.

There were a hundred bright kimono sleeves pushing and


reaching toward the shelves of dolls in the shop. There were fifty
little Japanese girls chattering together about the smiling face of one
and the beautiful silk kimono of another.

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

THE DOLLS' FESTIVAL

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.

"What a pity that Saké makes us so much trouble!" she said to


Tei. "It is plain to be seen that the good dog Shiro was no ancestor
of his."

"What good dog Shiro?" asked Tei.

"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."

Tei laughed. "The illustrious dolls always behave most honorably


well," she said. "Perhaps it is because they do not forget from year
to year, but spend all their time in remembering."

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.

She smiled sweetly up into Umé's face. "The honorable fountain!"


she said.
"The Japanese tea-ceremony has nothing to do with the
honorable fountain in the garden," said Umé as she clapped her
hands for old Maru, the nurse.

"Naruhodo!" said old Maru, as she brought towels and wiped the
tea from the baby and the mat with many exclamations of
amazement.

"Naruhodo!" she repeated, as she watched the two older children


try to teach something of the tea-ceremony to the baby.

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.

"Honorably deign to enter," said a little woman crouching at the


door.

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.

"Just what we were respectfully speaking about in the morning


hour!" murmured Tei.
Umé nodded and would have said something in answer, but her
grandmother said, "Hush!"

"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."

Umé and Tei nodded and smiled at one another.

"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.

"One day he began digging for himself in a corner of the garden.


Scratch! went his two paws as fast as he could make the dirt fly, and
the good old man took his spade and dug in the spot to find what
could be hidden in the dirt.

"He was rewarded by finding an honorable quantity of coins;


enough to keep him and his wife comfortable for many months.

"But the envious man, the unworthy neighbor, hearing of this


good fortune, asked to borrow the dog.

"'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.'

"'What is done cannot now be helped,' the bad neighbor


answered.

"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.

"Immediately, pop! The branches were suddenly covered with


beautiful double cherry blossoms.

"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.

"When his tree blossomed as the first had done, he was so


pleased that he gave the old man many valuable gifts of silk and rice
and sent him home, to be known as the 'old man who could make
dead trees blossom.'"

When the story-teller finished, he disappeared behind a red


curtain and there was nothing for Umé and Tei to do but go home.

"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

A VISIT TO THE TEMPLE

"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.

"Why, honorable mother?"

"Because you were ill from eating too many sweets the day
before, and had to stay at home in your bed."

Umé laughed. "Now I do remember it," she said. "My unworthy


head danced like a geisha girl when I tried to stand on my two feet."

Umé's mother looked at her little daughter reprovingly. "Do not


speak so easily of such girls, Umé-ko," she said.

"Was Tara taken to the temple when he was thirty days old?"

"Yes, my daughter."

"But, Mother San, with whom did I ride then?"

"With O Ba San."

"I wish I could go to-day with Tei," said Umé.

"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.

Everything there was in a great state of excitement. The new


baby, dressed in a most gorgeous red silk kimono with the family
crest embroidered on the back and sleeves, was going to make his
first visit to the temple.

"Yes, you may come with me," said Tei to Umé, after asking the
honorable father's permission.

Umé Riding in a Jinrikisha. Page 37.

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é.

"But I thought it must always be done on the seventh day," said


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.

"He is to be called Onda," answered Tei.

Before Umé could ask any more questions they had reached the
temple.

Everything seemed to go wrong with Tei. She caught her clog as


she was getting out of the jinrikisha and fell upon her nose. It bled a
little, just enough to make her say pitifully, "Oh, how truly sad! It will
never bring good luck to the dear brother."

But Umé was always quick at thinking of a way out of trouble.


Near the entrance to the temple stood a deep basin filled with water.
With this water everybody washes his hands before going in to pray.
Umé lifted a spoonful of the water and rubbed it over her cousin's
nose. "That will make it as well as ever," she told Tei.
"What is that in your other hand?" asked Tei, seeing that Umé
was using only one hand, and that the other was tightly closed.

"It is a rice-cake to feed to the goldfish in the temple lake." One


can always buy rice-cakes at the temple gate, but Umé had
thoughtfully brought one from her home.

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.

"We have no more time," he said gently to them. "Unless we are


soon at our unworthy house, all the honorable guests will be there
before us."

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.

"Such an illustrious crowd!" said Tei, drawing in her breath with


excitement.

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.

"Baby Onda is tired with so much looking and holding and


praising," said Umé to her mother as they went home through the
gardens. "He will never go to sleep again, or else he will sleep for a
week of days."

"He is an honorable boy child," answered her mother. "A boy must
learn early to bear hardships."

"It is no hardship to receive honorable praise," said little Umé.


CHAPTER VI

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."

"The Cherry Trees in Ueno Park are in full


Blossom." Page 42.

Umé turned from looking at the cherry blossoms in the garden to


look at her mother who stood on the veranda.

"Something will honorably give way in my heart, O Haha San,"


she said.
"What do you mean, Umé-ko?" asked her mother.

"My heart is greatly joyous over so many blossoms," answered


the little girl. "It has grown so big that I would feel better if it should
take itself to the godown and leave me without it."

"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?"

"The fish-tree festival is the one I like," answered Tara, and he


gave his ball a great toss into the air.

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."

But there were no extra strings hanging in the clog-closet where


some were usually to be found, and Umé had a great hunt for them.

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 streets were crowded with people dressed in gay kimonos


and carrying paper parasols or fans. Some were riding, some were
walking, and all were happily chatting and laughing.
"Is everyone in the whole world going to Ueno Park?" Umé asked
her grandmother, and immediately forgot her question in listening to
the sounds of gongs and tinkling bells that filled the air. The joyous
sound of bells is always a part of the Cherry-blossom Festival in
Tokio, and makes the city a very merry place.

The long avenue leading up to the entrance of the park, which is


on the brow of a high hill, was arched overhead with the blossoming
branches of the cherry trees.

"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é came as near pouting as a Japanese maiden can. "I think I


have heard that the foreign children tell their fathers when they wish
to stop in the honorable ride," she murmured.
"They are all barbarians, those foreigners," said her grandmother.
"You can see by the gardens of flowers that they wear upon their
heads, that they know nothing of propriety."

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.

"They must be glad to come to Japan and learn so many new


ways to be happy, O Ba San," she said.

The grandmother did not quite understand Umé's way of thinking.


"In what way?" she asked.

"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."

As if her father knew that Umé longed to see something of the


foreign children's play, he stopped his own jinrikisha man at that
very moment, and the rest of his party stopped beside him.

Under a particularly large and beautiful cherry tree a group of


both foreign and Japanese children were gathered around a peddler
who carried a tray of candies upon his head. In one hand he held a
drum and on his shoulder perched a monkey dressed in a bright
colored kimono.

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.

By that time a great crowd of merrymakers had collected, and


Umé's father told his coolie to go on. So the little party started on
again, and soon passed an open space among the trees where
Japanese fireworks were shooting into the air. The Japanese send off
their fireworks in the daytime, as well as at night, to make their
festivals more festive.

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.

As Tara looked out over the gaily dressed crowds he said


boastfully, "There can be no other country in the world with such
fine, brave people."

"It is true that we are a brave people," his father answered.


"Many times, when I was no older than you are, little son, has my
mother wakened me very early in the morning and put a toy sword
into my hand. 'Your companions are out playing the sword-game.
Join them!' were her words. And although the ground was white
with snow, and I was very sleepy, I always went as she bade me."

Tara looked at his father in admiration.

"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

You might also like