Contents
1 Introduction to JavaScript 1
What You Need to Know 2
Basic HTML and CSS Knowledge 3
Basic Text Editor and Web Browser Knowledge 3
Which Version? 5
Remember,Its Not Java 6
Similarities to Other Languages 6
Beginning with JavaScript 8
Prototype-Based 8
Client-Side 8
Scripting Language 9
Putting It All Together 9
Online Resources 10
Try This 1-1: Use JavaScript to Write Text 10
Chapter 1 Self Test 11
2 Placing JavaScript in an HTML File 15
Using the HTML Script Tags 16
Identifying the Scripting Language 17
Calling External Scripts 17
Specifying When the Script Should Load 18
Using Tags 18
Creating Your First Script 20
Writing aHello World Script 20
Creating an HTML Document for the Script 21 Inserting the Script into the HTML Document 21
Try This 2-1: Insert a Script into an HTML Document 23 Using External JavaScript Files 24
Creating a JavaScript File 24
Creating the HTML Files 24
Viewing the Pages in Your Browser 26
Try This 2-2: Call an External Script from an HTML Document 27
Using JavaScript Comments 28
Inserting Comments on One Line 28
Adding Multiple-Line Comments 28
Chapter 2 Self Test 30
3 Using Variables 33
Understanding Variables 34
Why Variables Are Useful 35
Variables as Placeholders for Unknown Values 35 Variables as Time-Savers 35
Variables as Code Clarifiers 36
Defining Variables for Your Scripts 36
Declaring Variables 36
Assigning Values to Variables 37
Naming Variables 38
Understanding Data Types 41
Number 41
String 42
Boolean 46
Null 47
Undefined 47
Try This 3-1: Declare Variables 48
Using Variables in Scripts 49
Making a Call to a Variable 49
Adding Variables to Text Strings 50
Writing a Page of JavaScript 51
Creating the Framework 51
Defining the Variables 52
Adding the Commands 53
Modifying the Page 54
Try This 3-2: Create an HTML Page with JavaScript 55
Chapter 3 Self Test 57
4 Using Functions 59
What a Function Is 60
Why Functions Are Useful 60
Structuring Functions 61
Declaring Functions 61
Defining the Code for Functions 62
Naming Functions 63
Adding Arguments to Functions 63
Adding Return Statements to Functions 65
Calling Functions in Your Scripts 66
Script Tags: Head Section or Body Section 67 Calling a Function from Another Function 70 Calling Functions with Arguments 71 Calling Functions with Return Statements 75 Other Ways to Define Functions 75
Try This 4-1: Create an HTML Page with Functions 79 Scope/Context Basics 81
Global Context 81
Function Context 81
Try This 4-2: Write Your Own Functions 83
Chapter 4 Self Test 84
5 JavaScript Operators 87
Understanding the Operator Types 88
Understanding Arithmetic Operators 89
The Addition Operator (+) 90
The Subtraction Operator () 93
The Multiplication Operator (*) 93
The Division Operator (/) 94
The Modulus Operator (%) 95
The Increment Operator (++) 95
The Decrement Operator ( ) 96
The Unary Plus Operator (+) 96
The Unary Negation Operator () 97
Understanding Assignment Operators 98
The Assignment Operator (=) 99
The Add-and-Assign Operator (+=) 99 The Subtract-and-Assign Operator (=) 100 The Multiply-and-Assign Operator (*=) 100 The Divide-and-Assign Operator (/=) 100 The Modulus-and-Assign Operator (%=) 100
Try This 5-1: Adjust a Variable Value 101
Understanding Comparison Operators 102
The Is-Equal-To Operator (==) 102
The Is-Not-Equal-To Operator (!=) 104 The Strict Is-Equal-To Operator (===) 105 The Strict Is-Not-Equal-To Operator (!==) 105 The Is-Greater-Than Operator (>) 106
The Is-Less-Than Operator (<) 106
The Is-Greater-Than-or-Equal-To Operator (>=) 107 The Is-Less-Than-or-Equal-To Operator (<=) 107 Understanding Logical Operators 108
The AND Operator (&&) 108
The OR Operator (||) 108
The NOT Operator (!) 109
The Bitwise Operators 109
Special Operators 110
Understanding Order of Operations 111
Try This 5-2: True or False? 113
Chapter 5 Self Test 114
6 Conditional Statements and Loops 117
Defining Conditional Statements 118
What Is a Conditional Statement? 118
Why Conditional Statements Are Useful 119
Using Conditional Statements 119
Using if/else Statements 119
Using the switch Statement 127
Using the Conditional Operator 129
User Input from a Prompt 130
Try This 6-1: Work with User Input 133
Defining Loops 134
What Is a Loop? 134
Why Loops Are Useful 134
Using Loops 135
for 135
while 143
do while 143
for in and for each in 145
Using break and continue 145
Try This 6-2: Work with for Loops and while Loops 147
Chapter 6 Self Test 149
7 JavaScript Arrays 153
What Is an Array? 154
Why Arrays Are Useful 155
Defining and Accessing Arrays 155
Naming an Array 155
Defining an Array 155
Accessing anArrays Elements 157
Using the length Property and Loops 158
Changing Array Values and Changing the Length 159
Try This 7-1: Use Loops with Arrays 161
Array Properties and Methods 162
Properties 162
Methods 163
Nesting Arrays 176
Defining Nested Arrays 177
Loops and Nested Arrays 177
Try This 7-2: Nested Arrays Practice 180
Chapter 7 Self Test 181
8 Objects 183
Defining Objects 184
Creating Objects 184
Naming 184
Single Objects 185
Try This 8-1: Create a Computer Object 188
Object Structures 189
Constructor Functions 189
Using Prototypes 194
Helpful Statements for Objects 197
The for-in Loop 197
The with Statement 198
Try This 8-2: Practice with the Combination Constructor/Prototype Pattern 200 Understanding Predefined JavaScript Objects 201
The Navigator Object 201
The History Object 204
Chapter 8 Self Test 206
9 The Document Object 209
Defining the Document Object 210
Using the Document Object Model 210
Using the Properties of the Document Object 211
Collections 211
The cookie Property 214
The dir Property 214
The lastModified Property 214
The referrer Property 215
The title Property 216
The URL Property 217
The URLUnencoded Property 217
Using the Methods of the Document Object 218
The get Methods for Elements 218
The open() and close() Methods 223
The write() and writeln() Methods 225
Using DOM Nodes 225
DOM Node Properties 226
DOM Node Methods 228
Try This 9-1: Add a DOM Node to the Document 233 Creating Dynamic Scripts 234
Styles in JavaScript 234
Simple Event Handling 236
Coding a Dynamic Script 237
Try This 9-2: Try Out Property Changes 238
Chapter 9 Self Test 239
10 Event Handlers 243
What Is an Event Handler? 244
Why Event Handlers Are Useful 244
Understanding Event Handler Locations and Uses 245
Using an Event Handler in an HTML Element 245 Using an Event Handler in the Script Code 246
Learning the Events 248
The Click Event 250
Focus and Blur Events 251
The Load and Unload Events 253
The Reset and Submit Events 255
The Mouse Events 256
The Keyboard Events 257
Try This 10-1: Focus and Blur 258
Other Ways to Register Events 259
The addEventListener() Method 260
The attachEvent() Method 261
The Event Object 261
DOM and Internet Explorer: DOM Level 0 Registration 262 Using event with Modern Event Registration 262 Properties and Methods 263
Event Information 264
Try This 10-2: Using addEventListener() 265
Creating Scripts Using Event Handlers 265
Show Hidden Content 266
Change Content 268
Chapter 10 Self Test 272
11 Window Object 275
Window: The Global Object 276