Professional Documents
Culture Documents
1 Introduction To Javascript - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . .
1 Introduction To Javascript - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . .
1 Introduction To Javascript - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . .
Contents
ACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
INTRODUCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1 Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What You Need to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Basic HTML Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Basic Text Editor and Web Browser Knowledge . . . . . . . . . . . . . . . . 3
Which Version? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Remember, It’s Not Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Similarities to Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Object Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Client Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Scripting Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Project 1-1: Using JavaScript to Write Text . . . . . . . . . . . . . . . . . . . . . . . . . 12
Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Placing JavaScript in an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Using the HTML SCRIPT Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Identifying the Scripting Language . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Calling External Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Hiding JavaScript Code from Older Browsers . . . . . . . . . . . . . . . . . . 18
Creating Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Writing a “Hello World” Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Creating an HTML Document for the Script . . . . . . . . . . . . . . . . . . 21
Inserting the Script into the HTML Document . . . . . . . . . . . . . . . . . 21
Project 2-1: Insert a Script into an HTML Document . . . . . . . . . . . . . . . . . . 24
Revisiting “Hello World” in External Form . . . . . . . . . . . . . . . . . . . . . . . . . 26
Creating a JavaScript File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Creating the HTML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Viewing the Pages in Your Browser . . . . . . . . . . . . . . . . . . . . . . . . . 28
Project 2-2: Call an External Script from an HTML Document . . . . . . . . . . 29
Using JavaScript Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Inserting Comments on One Line . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Adding Multiple-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:40 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:vi
3 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
What Is a Variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Why Are Variables Useful? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Variables as Time-Savers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Variables as Code Clarifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Defining Variables for Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Understanding Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Project 3-1: Declare Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Using Variables in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Making a Call to a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Adding Variables to Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Writing a Page of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Creating the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Defining the Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Adding the Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Modifying the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Project 3-2: Create an HTML Page with JavaScript . . . . . . . . . . . . . . . . . . . 64
Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
What Is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Why Are Functions Useful? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Structuring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Declaring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Defining the Code for Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Adding Parameters to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Adding Return Statements to Functions . . . . . . . . . . . . . . . . . . . . . . 78
Calling Functions in Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Calling a Function in the HEAD Section . . . . . . . . . . . . . . . . . . . . . 80
Calling a Function in the BODY Section . . . . . . . . . . . . . . . . . . . . . 84
Calling a Function from Another Function . . . . . . . . . . . . . . . . . . . . 87
Calling Functions with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Calling Functions with Return Statements . . . . . . . . . . . . . . . . . . . . 94
Project 4-1: Create an HTML Page with Functions . . . . . . . . . . . . . . . . . . . 97
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:40 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:vii
Contents vii
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:40 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:viii
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:41 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:ix
Contents ix
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:41 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:x
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:41 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:xi
Contents xi
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:41 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:xii
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:42 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:xiii
Contents xiii
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:42 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:xiv
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:42 PM
Color profile: Generic CMYK printer profile Begin8 / JavaScript: A Beginner’s Guide / Pollock / 3140-3 / Front Matter
Composite Default screen
Blind Folio FM:xv
Contents xv
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
P:\010Comp\Begin8\140-3\fm.vp
Friday, March 09, 2001 2:00:42 PM