JavaScript for Web Warriors, Seventh Edition
By Patrick Carey and Sasha Vodnik
Contents:
Preface xi
Chapter 1
Introduction to JavaScript 1
Exploring the JavaScript Language 1
Introducing Scripting Languages 2
JavaScript and ECMAScript 2
The DOM and the BOM 3
Understanding Client/Server Architecture 4
JavaScript and Client-Side Scripting 5
Understanding Server-Side Scripting 6
Should You Use Client-Side or Server-Side
Scripting? 6
Writing a JavaScript Program 7
IDEs and Code Editors 7
The script Element 9
JavaScript Statements 9
Understanding JavaScript Objects 10
Using the write() Method 10
Case Sensitivity in JavaScript 13
Adding Comments to a JavaScript Program 13
Writing Basic JavaScript Code 14
Using Variables 14
Assigning Variable Names 15
Declaring and Initializing Variables 15
Building Expressions with Variables 17
Building an Expression 17
Modifying Variables 18
Understanding Events 18
Working with Elements and Events 19
Referencing Web Page Elements 21
Structuring JavaScript Code 22
Including a script Element for Each
Code Section 23
Placing the script Element 23
Creating a JavaScript Source File 23
Referencing an External File 24
Using the async and defer Keywords 24
Connecting to a JavaScript File 25
Working with Libraries 26
Validating Web Pages 27
Summary 28
Key Terms 29
Review Questions 30
Hands-On Projects 31
Case Projects 35
Chapter 2
Working with Functions,
Data Types, and Operators 37
Working with Functions 38
Defining a Function 38
Writing a Function 39
Calling a Function 41
Returning a Value from a Function 41
Managing Events with Functions 42
Using Event Handlers 42
Events as Object Properties 43
Event Listeners 43
Events and Anonymous Functions 44
Applying a Function to an Event 44
Using Built-in JavaScript Functions 45
Understanding Variable Scope 45
let and var Declaration Scopes 46
Local and Global Scope 46
Working with Data Types 48
Working with Numeric Values 48
Working with Boolean Values 49
Working with Strings 49
Escape Characters and Sequences 50
Using Operators to Build Expressions 51
Arithmetic Operators 52
Assignment Operators 53
Comparison Operators 54
Conditional Operators 55
Understanding Falsy and Truthy Values 55
Logical Operators 56
Special Operators 57
Understanding Operator Precedence 57
Using Expressions with Web Form
Controls 59
Working with Input Control Values 59
Working with Checkboxes 61
Using the change Event with Web Form Controls 64
Locating Errors with the Browser
Console 66
Accessing the Browser Console 66
Locating an Error in Your Program 67
Summary 68
Key Terms 69
Review Questions 70
Hands-On Projects 71
Case Projects 76
Chapter 3
Building Arrays and
Controlling Flow 77
Storing Data in Arrays 77
Declaring and Initializing Arrays 78
Elements and Indexes 79
Creating an Array 79
Multidimensional Arrays 82
Exploring HTML Collections 83
Referencing an Element within a Collection 83
Searching through the DOM 84
Viewing Arrays and HTML Collections
with the Console 85
Working with Program Loops 86
The while Loop 86
The do while Loop 89
The for Loop 89
Writing a for Loop 91
Exploring Array Methods for
Generating Loops 94
Adding Decision Making to Your Code 96
The if Statement 96
The if else Statement 97
The else if Statements 97
Nested if and if else Statements 100
Conditional Statements and Browser Testing 101
The switch Statement 102
Managing Program Loops and
Conditional Statements 105
The break Statement 105
The continue Statement 105
Statement Labels 105
Summary 107
Key Terms 107
Review Questions 108
Hands-On Projects 109
Case Projects 116
Chapter 4
Debugging and Error
Handling 117
Introduction to Debugging 117
Load-Time Errors 118
Runtime Errors 119
Logic Errors 120
Starting Debugging with the
Browser Console 121
Running Javascript in Strict Mode 125
Tracing Errors to Their Source 127
Tracing Errors with the window.alert()
Method 127
Tracing Errors with the Console Log 131
Using Comments to Locate Bugs 135
Tracking Program Flow with
Debugging Tools 136
Accessing your Browser’s Debugging Tools 136
Adding and Removing Break Points 137
Stepping through the Program Execution 140
Tracking Variables and Expressions 141
Examining the Call Stack 143
Managing Errors 145
Handling Exceptions with the try catch
Statement 145
Throwing an Exception 146
The try catch finally Statement 146
The error Parameter in the catch
Statement 147
Applying Exception Handling to a Program 147
Customizing Your Error Handling 150
Catching Errors with the error Event 150
Error Handling Functions 151
Summary 152
Key Terms 153
Review Questions 153
Hands-On Projects 154
Case Projects 160
Chapter 5
Creating a Web App using
the Document Object
Model 163
Designing a Web App 163
Introducing Nodes 165
Nodes and the Document Object Model 166
Selecting Nodes with the
querySelectorAll() Method 168
Creating and Connecting Nodes 168
Elements Nodes and HTML Attributes 169
Nodes and Inline Styles 170
Creating a Document Fragment in an App 170
Viewing Elements within the Browser
Debugger 173
Restructuring a Node Tree 177
Moving Nodes with the appendChild()
Method 177
Moving Nodes with the insertBefore()
Method 179
Cloning a Node 181
Running Timed Commands 181
Repeating Commands at Specified Intervals 181
Stopping a Timed Command 182
Using Time-Delayed Commands 183
Working with Popup Windows 184
System Dialog Boxes 184
Working with Browser Windows 185
Writing Content to a Browser Window 187
Limitations of Browser Windows 187
Creating an Overlay 188
Introducing the this Object 190
Removing a Node 191
Exploring the Browser Object Model 193
The History Object 193
The location Object 194
The navigator Object 194
The screen Object 195
Summary 196
Key Terms 197
Review Questions 197
Hands-On Projects 199
Case Projects 206
Chapter 6
Enhancing and Validating
Forms 209
Exploring Forms and Form Elements 209
The Forms Collection 211
Working with Form Elements 211
Properties and Methods of input Elements 212
Navigating Between Input Controls 213
Working with Selection Lists 214
Working with Option Buttons 217
Locating the Checked Option 217
Accessing the Option Label 220
Formatting Data Values in a Form 220
The toFixed() Method 220
Formatting Values Using a Locale String 221
Responding to Form Events 223
Working with Hidden Fields 225
Exploring Form Submission 227
Using the submit Event 227
Resetting a Form 227
Validating Form Data with JavaScript 228
Working with the Constraint Validation API 230
Exploring the ValidityState Object 231
Creating a Custom Validation Message 232
Responding to Invalid Data 233
Validating Data with Pattern Matching 235
Validating a Selection List 236
Testing a Form Field Against
a Regular Expression 238
Creating a Custom Validity Check 240
Managing Form Validation 241
Summary 243
Key Terms 244
Review Questions 244
Hands-On Projects 246
Case Projects 252
Chapter 7
Manipulating Data in
Strings, Arrays, and Other
Objects 253
Retrieving Content from a Text File 253
The file Object 255
The File Reader API 256
Working with Text Strings 258
Searching for Substrings within a Text String 259
Modifying Text Strings 261
Extracting Characters and Substrings 263
Combining Text Strings 264
Comparing Text Strings 265
Introducing Regular Expressions 266
Matching a Substring 266
Setting Regular Expression Flags 267
Defining Character Types and Character
Classes 268
Specifying Repeating Characters 270
Using Escape Sequences 272
Specifying Alternate Patterns and Grouping 273
Programming with Regular
Expressions 274
Regular Expression Methods 275
Replacing Text with Regular Expressions 276
Splitting a Text String into an Array 278
Referencing Substring Matches 279
Exploring Array Methods 280
Reversing and Sorting an Array 281
Sorting with a Compare Function 284
Extracting and Inserting Array Items 286
Using Arrays as Data Stacks 287
Exploring the Math Object 292
The Math Object 292
Math Object Properties 292
Applying a Math Method to an Array 293
Random Numbers and Random Sorting 294
Exploring the Date Object 294
Extracting Information from Dates
and Times 295
Setting Date and Time Values 296
Exploring Template Literals 297
Adding Placeholders to Template Literals 297
Tagging a Template Literal 297
Summary 299
Key Terms 300
Review Questions 300
Hands-On Projects 301
Case Projects 308
Chapter 8
Creating Customized
Objects, Properties, and
Methods 309
Understanding Object-Oriented
Programing 309
Reusing Software Objects 310
Understanding Encapsulation 310
Creating an Object Literal 312
Dot Operators and Bracket Notation 313
Creating a Custom Method 315
Creating an Object with the new Operator 317
Working with Object Classes 318
Understanding Object Classes 318
Object Constructors and Literals 318
Constructor Functions 319
Combining Object Classes 320
Working with Object Prototypes 327
The Prototype Object 327
Extending Built-in JavaScript Objects 329
Introducing Closures 331
Lexical Scope 331
Closures and the Lexical Environment 332
Closures with for Loops 334
Working with Public, Private,
and Privileged Methods 338
Combining Objects with Prototype
Chains 343
Creating a Prototype Chain 344
Using the Base Object 345
Using the apply()and call() Methods 346
Data Storage with Associative Arrays 347
The for in and for of Loops 347
Storing Object data in JSON 349
Summary 351
Key Terms 352
Review Questions 353
Hands-On Projects 354
Case Projects 362
Chapter 9
Managing State
Information and Security 363
Understanding Sessions and State
Information 363
Sharing Data Between Forms 365
Retrieving the Query String Text using the
Location object 367
Replacing URI Encoding Characters 368
Introducing Web Storage 371
The Web Storage API 371
Local Storage and Session Storage Objects 371
Storing Data in Web Storage 372
Viewing Web Storage Items in your Browser 374
Retrieving Items with the getItem()
Method 374
Removing Items from Web Storage 376
Exploring Storage Events 377
Web Storage and the Same-Origin Policy 378
Introducing Cookies 379
Cookies vs. Web Storage 379
The Structure of a Cookie 380
Writing Data into a Cookie 381
Setting the Cookie Expiration Date 382
Setting the Cookie Path 383
Setting the Cookie Domain 383
Defining Cookie Security 383
A Function to Write the Cookie Value 384
Reading a Cookie 385
Deleting a Cookie 386
Exploring Security Issues 386
Secure Coding with JavaScript 387
JavaScript Security Concerns 387
Using Third-Party Scripts 388
Summary 389
Key Terms 389
Review Questions 390
Hands-On Projects 391
Case Projects 399
Chapter 10
Programming with Event
Objects and Third-Party
APIs 401
Working with Events as Objects 402
The Event Object 402
Event Capturing and Bubbling 403
Exploring Mouse, Touch, and Pointer
Events 406
Exploring Touch Events 407
Managing Multiple Touchpoints 409
Using Pointer Events 409
Programming a Drag and Drop Action 410
Finding Event Coordinates 411
Dragging and Dropping an Element 413
Browser Tools for Touchscreen Emulation 415
Exploring the Drag and Drop API 415
The HTML Drag and Drop API 416
Transferring Data with Drag and Drop 417
Working with Keyboard Events 418
Creating an Interactive Map 421
Getting Started with the Google Maps API 421
The map Object 423
Adding Map Pins 425
Mapping Your Position with
Geolocation 426
Adding Directions to a Map 430
The route Object 430
Displaying the Driving Route 431
Introducing the Device Orientation API 434
Preparing an App for Mobile Use 435
Testing Tools 435
Minimizing Download Size 435
Minifying Files 435
Summary 437
Key Terms 437
Review Questions 438
Hands-On Projects 439
Case Projects 446
Chapter 11
Managing Data Requests
with AJAX and Fetch 449
Introducing Server Requests 450
Exploring HTTP Messages 452
Understanding HTTP Messages 452
Introducing AJAX 454
The XMLHttpRequest Object 454
Managing a Response 456
Viewing the Status of a Request
and Response 458
AJAX and Callback Hell 460
Introducing Arrow Functions 461
Arrow Functions and Parameter Values 462
Exploring the Promise Object 464
Defining a Promise Object 464
Chaining Promises 465
Running Multiple Promises 466
Using the Fetch API 467
Managing Fetch Responses 467
Error Handling with Fetch 468
Using Fetch to Return a Search 469
Working with XML 472
Parsing XML Content 473
Working with an XML Node Tree 474
Creating an Autocomplete
Search Box 476
Working with JSON Data 477
Building the Suggestion Box 479
Working with Third-Party APIs 482
Requesting a Random GIF 482
Third-Party Endpoints 483
Exploring Security Issues with APIs 486
Working with CORS 486
Using JSONP 486
Using XHR with a Proxy 487
Summary 488
Key Terms 488
Review Questions 489
Hands-On Projects 490
Case Projects 500
Chapter 12
Introducing jQuery 501
Getting Started with jQuery 501
Versions of jQuery 502
Loading jQuery 502
Is jQuery still Relevant? 503
Working with jQuery Selectors 505
Selecting Elements from the DOM 506
Traversing the DOM with jQuery 507
Working with Attributes and CSS Properties 508
Changing the DOM Structure 509
Handling Events with jQuery 511
Working with Effects and Animations 515
Chaining Effects 516
Creating Custom Effects with Animate 517
Controlling the Animation Queue 519
Exploring jQuery Plugins 520
Summary 524
Key Terms 524
Review Questions 525
Hands-On Projects 526
Case Projects 533
Appendix a
Installing and Configuring a Testing Server 535
Appendix B
Working with HTML and CSS 543
Appendix C Solutions to
Quick Checks On line
GLOSSARY 549
Index 561