JavaScript: A Beginner's Guide, Fifth Edition Pollock - The ebook in PDF format with all chapters is ready for download
JavaScript: A Beginner's Guide, Fifth Edition Pollock - The ebook in PDF format with all chapters is ready for download
https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-john-pollock/
https://ebookmass.com/product/the-norton-field-guide-to-writing-fifth-
edition/
https://ebookmass.com/product/the-norton-field-guide-to-writing-with-
readings-fifth-edition-fifth-edition-ebook-pdf-version/
Creating Effective Teams: A Guide for Members and Leaders
(NULL) Fifth
https://ebookmass.com/product/creating-effective-teams-a-guide-for-
members-and-leaders-null-fifth/
https://ebookmass.com/product/technical-analysis-explained-fifth-
edition-the-successful-investors-guide/
https://ebookmass.com/product/learning-web-design-a-beginners-guide-
to-html-css-javascript-and-web-graphics-5th-edition-ebook-pdf/
https://ebookmass.com/product/english-file-beginner-teachers-guide-
for-speakers-of-spanish-fourth-edition-christina-latham-koenig/
https://ebookmass.com/product/writing-conversational-korean-book-two-
katarina-pollock/
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Folio: i
JavaScript
A Beginner’s Guide
Fifth Edition
John Pollock
ISBN: 978-1-26-045769-8
MHID: 1-26-045769-9
The material in this eBook also appears in the print version of this title: ISBN: 978-1-26-045768-1,
MHID: 1-26-045768-0.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trade-
marked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringe-
ment of the trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in
corporate training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com.
Oracle and Java are registered trademarks of Oracle Corporation and/or its affiliates. All other trademarks are the property of
their respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these
marks.
Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of Oracle Corpora-
tion and/or its affiliates.
Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human
or mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness
of any information included in this work and is not responsible for any errors or omissions or the results obtained from the use
of such information.
TERMS OF USE
This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work
is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit,
distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill Education’s prior consent. You
may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to
use the work may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES
OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED
FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD-
ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE. McGraw-Hill Education and its licensors do not warrant or guarantee that the functions contained in the work will
meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors
shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages
resulting therefrom. McGraw-Hill Education has no responsibility for the content of any information accessed through the work.
Under no circumstances shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive,
consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of
the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or
cause arises in contract, tort or otherwise.
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Foli iii
Contents
ACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
.
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
.
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
.
Client-Side and Server-Side Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
.
Beginning with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Prototype-Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Interpreted Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Numerous Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
.
v
.
Using <noscript></noscript> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
.
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
.
Understanding Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
.
Number ...................................................................... 41
.
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
.
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Undefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
.
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
.
Try This 3-1: Declare Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
.
Using Variables in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Making a Call to a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Adding Variables to Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Writing a Page of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Creating the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Defining the Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Adding the Commands ....................................................... 55
.
Modifying the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
.
00-FM.indd 6 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Contents vii
.
Chapter 3 Self Test ................................................................. 60
.
4 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
.
What a Function Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
.
Why Functions Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
.
Structuring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
.
Declaring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
.
Defining the Code for Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
.
Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
.
Adding Arguments to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
.
Adding Return Statements to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
.
Calling Functions in Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
.
Script Tags: Head Section or Body Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
.
Calling a Function from Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
.
Calling Functions with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
76
Calling Functions with Return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
.
Other Ways to Define Functions .............................................. 82
.
Try This 4-1: Create an HTML Page with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
.
Scope/Context Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
.
Global Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
.
Function Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.
Block Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
.
Try This 4-2: Write Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
.
Chapter 4 Self Test ................................................................. 91
.
5 JavaScript Operators ..................................................... 95
.
Understanding the Operator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
.
Understanding Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
.
The Addition Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
.
The Subtraction Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.
The Multiplication Operator (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.
The Division Operator (/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
The Modulus Operator (%) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
The Increment Operator (++) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
.
The Decrement Operator (– –) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
.
The Unary Plus Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
.
The Unary Negation Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
.
The Exponentiation Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.
Understanding Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.
The Assignment Operator (=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
.
The Add-and-Assign Operator (+=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
.
The Subtract-and-Assign Operator (–=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
.
The Multiply-and-Assign Operator (*=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
The Divide-and-Assign Operator (/=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
00-FM.indd 7 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
.
The Exponent-and-Assign Operator (**=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
Try This 5-1: Adjust a Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
.
Understanding Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
.
The Is-Equal-To Operator (==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
.
The Is-Not-Equal-To Operator (!=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
.
The Strict Is-Equal-To Operator (===) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
.
The Strict Is-Not-Equal-To Operator (!==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
.
The Is-Greater-Than Operator (>) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
.
The Is-Less-Than Operator (<) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
.
The Is-Greater-Than-or-Equal-To Operator (>=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
.
The Is-Less-Than-or-Equal-To Operator (<=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
.
Understanding Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
117
The AND Operator (&&) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
.
The OR Operator (||) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
.
The NOT Operator (!) ........................................................ 118
.
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
.
Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
.
Understanding Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
.
Try This 5-2: True or False? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
.
Chapter 5 Self Test ................................................................. 123
.
6 Conditional Statements and Loops ....................................... 125
.
Defining Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
.
What Is a Conditional Statement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
.
Why Conditional Statements Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using if/else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
.
Using the Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
.
User Input from a Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
.
Try This 6-1: Work with User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
.
Defining Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
.
What Is a Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
.
Why Loops Are Useful ....................................................... 144
.
Using Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
.
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
.
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
.
do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
.
for in, for each in, and for of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
.
Using break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
.
Try This 6-2: Work with for Loops and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
.
Chapter 6 Self Test ................................................................. 160
.
00-FM.indd 8 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Contents ix
.
What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
.
Why Arrays Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
.
Defining and Accessing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
.
Naming an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
.
Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Accessing an Array’s Elements ............................................... 167
.
Using the length Property and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
.
Changing Array Values and Changing the Length ............................. 169
.
Try This 7-1: Use Loops with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
.
Array Properties and Methods ...................................................... 172
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
.
Nesting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
.
Defining Nested Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
.
Loops and Nested Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
.
Try This 7-2: Nested Arrays Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
.
Chapter 7 Self Test ................................................................. 193
.
8 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
.
Defining Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Single Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
.
Try This 8-1: Create a Computer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
.
Object Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
.
Constructor Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
.
Using Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
.
The class Keyword ........................................................... 209
.
Helpful Statements for Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
.
The for-in Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
.
The with Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
.
Try This 8-2: Practice with the Combination Constructor/Prototype Pattern . . . . . . . . . . 212
.
Understanding Predefined JavaScript Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
.
The Navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
.
The History Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
.
Chapter 8 Self Test ................................................................. 218
.
9 The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
.
Defining the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
.
Using the Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Using the Properties of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
.
The cookie Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
.
00-FM.indd 9 17/09/19 5:33
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
.
The lastModified Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
.
The referrer Property ......................................................... 227
.
The title Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
.
The URL Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
.
The URLUnencoded Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Using the Methods of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
.
The get Methods for Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
.
The open() and close() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
.
The write() and writeln() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
.
Using DOM Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
.
DOM Node Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
238
DOM Node Methods ......................................................... 241
.
Try This 9-1: Add a DOM Node to the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
.
Creating Dynamic Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
.
Styles in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
.
Simple Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
.
Coding a Dynamic Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
.
Try This 9-2: Try Out Property Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Chapter 9 Self Test ................................................................. 253
.
10 Event Handlers ........................................................... 255
.
What Is an Event Handler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
.
Why Event Handlers Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
.
Understanding Event Handler Locations and Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
Using an Event Handler in an HTML Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
Using an Event Handler in the Script Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
.
Learning the Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
.
The Click Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
.
Focus and Blur Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
.
The Load and Unload Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
.
The Reset and Submit Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
.
The Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
The Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
Try This 10-1: Focus and Blur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
.
Other Ways to Register Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
.
The addEventListener() Method .............................................. 272
.
The attachEvent() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
The Event Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
DOM and Internet Explorer: DOM Level 0 Registration . . . . . . . . . . . . . . . . . . . . . . . 274
.
Using event with Modern Event Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
.
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
.
Event Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
.
Try This 10-2: Using addEventListener() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
.
00-FM.indd 10 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Contents xi
.
Show Hidden Content ........................................................ 279
.
Change Content .............................................................. 280
.
Custom Events ............................................................... 284
.
Chapter 10 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
.
11 Introduction to Node.js ................................................... 289
.
Introducing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
.
Installing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
.
Check for a Current Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
.
Install Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
.
Write a “Hello World” Script ................................................. 292
.
Using Node Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
.
Using Native Node Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
295
Asynchronous Execution ..................................................... 296
.
Non-Native Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
.
Try This 11-1: Use a Custom Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
.
Installing a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
.
Database Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
.
Install PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
.
Create a Database Using pgAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
.
Try This 11-2: Test Some SQL Queries ............................................. 312
.
Creating a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
.
Chapter 11 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
.
12 Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
.
Using the Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
What Is the Math Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
How the Math Object Is Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
.
Try This 12-1: Display a Random Link on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
.
Understanding the Number Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
.
Using the Date Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
.
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
.
Methods That Get Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
.
Methods That Set Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
.
Other Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
.
How About Some Date Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
.
Try This 12-2: Create a JavaScript Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
.
Continuing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
.
Getting to the Needed Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
.
Running Some Calculations on the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
.
Chapter 12 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
.
00-FM.indd 11 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
.
Introduction to the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
.
The String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
.
The String Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
.
What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
.
Using the Properties and Methods of the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
.
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
.
Methods of the String Object ....................................................... 360
.
Try This 13-1: Use indexOf() to Test an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
.
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
.
Setting a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
.
Reading a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
.
Try This 13-2: Remember a Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
376
Using Regular Expressions ......................................................... 377
.
Creating Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
.
Testing Strings Against Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
.
Adding Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
.
Creating Powerful Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
.
Grouping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
.
The replace(), match(), matchAll(), and search() Methods . . . . . . . . . . . . . . . . . . . . . 384
.
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.
Continuing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.
Chapter 13 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
.
14 Browser-Based JavaScript ................................................ 391
.
Window: The Global Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
.
Using the Properties of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
.
The closed Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
The frames Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The innerWidth and innerHeight Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The location Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The name Property ........................................................... 396
.
The opener Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
.
The parent, self, and top Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
The status and defaultStatus Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
Try This 14-1: Use the location and innerWidth Properties .......................... 398
.
Using the Methods of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
.
The alert(), prompt(), and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
The print() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
.
The setInterval() and clearInterval() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
The setTimeout() and clearTimeout() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
.
Try This 14-2: Use the setTimeout() and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . 406
.
00-FM.indd 12 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Contents xiii
.
The Tale of Pop-up Windows ................................................. 407
.
Opening New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
.
Closing New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
.
Moving, Resizing, and Scrolling New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
.
The resizeBy() and resizeTo() Methods ....................................... 416
.
The scrollBy() and ScrollTo() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
Working with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
Rollovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
JavaScript and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
420
Purpose of Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
.
Accessing Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
.
Breaking Out of Frames ...................................................... 423
.
Using iFrames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
.
Chapter 14 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
.
15 JavaScript Forms and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
.
Accessing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
.
Using the forms Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
.
Using an ID .................................................................. 431
.
Using the Properties and Methods of the Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
.
Ensuring the Accessibility of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
.
Using Proper Element and Label Order ....................................... 438
.
Using <label></label> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
.
Using <fieldset></fieldset> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
.
Not Assuming Client-Side Scripting .......................................... 439
.
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
.
Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
.
Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
.
Check Boxes and Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
.
Try This 15-1: Request a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
.
HTML5 and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
.
New Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
.
New Input Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
.
New Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
.
HTML5 Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
.
Try This 15-2: Validate a Phone Number with HTML5 or JavaScript ................ 455
.
AJAX and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
.
AJAX ........................................................................ 456
.
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
.
Chapter 15 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
.
00-FM.indd 13 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
.
Using jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
.
Obtaining jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
.
Getting Started: document.ready() ............................................ 471
.
Using Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
.
Altering Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
.
Methods for Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
.
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
.
Try This 16-1: Use jQuery to Create Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
.
Debugging Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
.
Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
.
Using the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
.
Using a Lint Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
.
Browser Developer Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
.
JavaScript and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
.
Separate Content from Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
486
Enhancing Content ........................................................... 488
.
Try This 16-2: Make This Code Accessible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
.
JavaScript Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
.
Page Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
.
JavaScript and APIs from HTML ................................................... 492
.
The <canvas> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
.
Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
.
Try This 16-3: Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
.
Node.js App Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
.
Update the Node.js Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Update the Front-end Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
.
Need Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
.
Chapter 16 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
.
A Answers to Self Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
.
Chapter 1: Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
.
Chapter 2: Placing JavaScript in an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
.
Chapter 3: Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
Chapter 4: Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
Chapter 5: JavaScript Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
.
Chapter 6: Conditional Statements and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
.
Chapter 7: JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
.
Chapter 8: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
.
Chapter 9: The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
.
00-FM.indd 14 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Contents xv
.
Chapter 11: Introduction to Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
.
Chapter 12: Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
.
Chapter 13: Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
.
Chapter 14: Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
.
Chapter 15: JavaScript Forms and Data ....................................... 521
.
Chapter 16: Further Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
.
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
.
00-FM.indd 15 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Folio vi
Acknowledgments
I would like to begin by thanking my wonderful wife, Heather Pollock, for all of her love,
support, and encouragement in all I do. I love you! I would also like to thank my three
daughters, Eva, Elizabeth, and Elaine, as well as my son, Evan. I love all of you!
I would like to thank my parents, Bruce and Joy Anderson, for their love and guidance, and
for always supporting my endeavors.
I would like to thank Dr. J. D. and Linda Andrews for their love, guidance, and support.
In addition, I would like to thank Richard Pollock (brother) and family, Misty Castleman
(sister) and family, Warren Anderson (brother) and family, Jon Andrews (brother) and family,
Lisa and Julian Owens (aunt/uncle) and family, and every aunt, uncle, cousin, or other relation
in my family. All of you have been a great influence in my life.
I would like to thank all of my editors at McGraw-Hill for their outstanding help and
support throughout the writing of this book. Thanks to Lisa McClain, Emily Walters, Claire Yee,
Snehil Sharma, Sarika Gupta, Bart Reed, and to all the editors who worked on this and previous
editions of the book.
Thanks to my technical editor, Christie Sorenson, for editing and checking over all the
technical aspects of the book and for helping me provide clear explanations of the topics that
are covered.
I would like to thank God for the ability He has given me to help and teach people by my
writing. “In all your ways acknowledge Him, and He shall direct your paths.” (Proverbs 3:6)
xvi
Introduction
W elcome to JavaScript: A Beginner’s Guide, Fifth Edition! Years ago, I was surfing
the Web and noticed that people were publishing pages about themselves and calling
them homepages. After viewing a number of these, I decided to create a homepage myself. I
had no idea where to begin, but through trial and error I figured out how to code HTML and
publish my documents on a Web server. Over time, I saw some interesting effects used on other
homepages (like alert messages that popped up out of nowhere or images that would magically
change when I moved my mouse over them). I was curious and just had to know what was being
done to create those effects. Were these page creators using HTML tags I did not know about?
Eventually, one site revealed what was being used to create those effects: JavaScript. I
went in search of information on it and came across a few tutorials and scripts on the Web.
Since I had programmed in other languages (such as a relatively obscure language called Ada),
I was able to catch on to JavaScript fairly quickly by looking at these tutorials and scripts.
I learned enough that I decided to create a Web site that would teach HTML and JavaScript
to beginners. As soon as I began the project, I received questions from visitors that were
way over my head—forcing me to dig deeper and learn more about JavaScript. As a result,
I became completely familiar with this scripting language and what it can do. Not only can
you add fun effects to a Web page, you can create scripts that will perform useful tasks, like
validate form input, add navigational elements to documents, and react to user events.
The goal of this book is to help you to learn the basics of the JavaScript language with as
little hair pulling and monitor smashing as possible. You do not need any prior programming
experience to learn JavaScript from this book. All you need is knowledge of HTML and/or
XHTML, Cascading Style Sheets (CSS), and how to use your favorite text editor and Web
browser (see Chapter 1 for more information).
xvii
● Key Skills & Concepts Each chapter begins with a set of key skills and concepts that
you will understand by the end of the chapter.
● Ask the Expert The Ask the Expert sections present commonly asked questions about
topics covered in the preceding text, with responses from the author.
● Try This These sections get you to practice what you have learned using a hands-on
approach. Each Try This will have you code a script through step-by-step directions
on what you need to do to in order to accomplish the goal. You can find solutions to
each project on the McGraw-Hill Professional Web site at www.mhprofessional.com/
computingdownload.
● Notes, Tips, and Cautions These elements call your attention to noteworthy statements
that you will find helpful as you move through the chapters.
● Code Code listings display example source code used in scripts or programs.
● Callouts Callouts display helpful hints and notes about the example code, pointing to the
relevant lines in the code.
● Self Test Each chapter ends with a Self Test, a series of 15 questions to see if you have
mastered the topics covered in the chapter. The answers to each Self Test can be found in
the appendix.
That is it! You are now familiar with the organization and special features of this book to
start your journey through JavaScript. If you find that you are stuck and need help, feel free to
contact me with your questions. To contact me, you can reach me on LinkedIn (https://www
.linkedin.com/in/john-pollock-82a2b074) or you can find me on Twitter (@ScripttheWeb).
Now it is time to learn JavaScript. Get ready, get set, and have fun!
Chapter 1
Introduction to JavaScript
"Clive.
"P.S. A box of intercepted letters will be sent down to-morrow by
water, under a guard of Sepoys. I would advise the Board to open
every one of them."
FOOTNOTES:
[1] This account is an abstract of a narrative of the mutiny,
written by the late Sir Henry Strachey, and which forms Appendix
1st to the 9th Report of the Committee of Secresy of the House of
Commons, A.D. 1773.
[2] Vide Ibid. Appendix, No. 1. a. fo. 699.
[3] The Court's orders for this reduction, dated the 11th of June,
1764, are very positive and peremptory.
[4] The order was expected some time before it was issued.
[5] Vide 9th Rep. of Com. of Secresy, App. No. 1. fo. 699. This
remonstrance sets forth the high rates of supplies at a distance
from the coast, objections of servants to act except in one
capacity, and various expenses and hardships entailed by their
situation, and aggravated by the reduction of allowances.
[6] Ibid. No. 2. fo. 700.
[7] Vide 9th Rep. of Com. of Secresy, App. No. 4 fo. 702.
[8] Ibid. No. 5. fo. 702.
[9] ibid. No. 6. fo. 702.
[10] Vide 9th Rep. of Com. of Secresy, App. No. 7. fo. 702.
[11] Ibid. No. 8. fo. 702.
[12] Vide 9th Rep. of Com. of Secresy, App. No. 9. fo. 703.
[13] Vide 9th Rep. of Com. of Secresy, App. No. 10. fo. 703.
[14] Ibid. No. 12. fo. 734.
[15] Vide 9th Rep. of Com. of Secresy, App. No. 13. fo. 704.
[16] Ibid. No. 14. fo. 704.
[17] Vide Letter, 1st May. No. 17. fo. 705. No. 18. fo. 705.
[18] Vide 9th Rep. of Com. of Secresy, Letter 4th May, App. No.
21. fo. 706.
[19] Ibid. No. 22. fo. 706.
[20] Vide Letter 5th May, No. 23. fo. 706.
[21] Vide 9th Rep. of Com. of Secresy, Letter 5th May, App. No.
24. fo. 707.
[22] Ibid. No. 27. fo. 707.
[23] Ibid. Nos. 28. and 30. fo. 708.
[24] Vide 9th Rep. of Com. of Secresy, App. No. 29. fo. 708.
[25] Ibid. No. 31. fo. 708.
[26] Ibid. No. 32. fo. 708.
[27] Vide 9th Rep. of Com. of Secresy, App. No. 38. fo. 780.
[28] A legacy of 70,000l. was bequeathed by Meer Jaffier Aly
Khan, Nawab of Bengal, in 1765, to Lord Clive, and paid by his
Lordship, in the year 1766, into the Company's treasury at Fort
William, to run at interest at the rate of 8 per cent., as an annual
fund for the support of European officers and soldiers, who may
be disabled or decayed in the Company's service in Bengal, and
for the widows of officers and soldiers who may die on service
there, 8th June, 1766.
The Company extended this donation afterwards to the benefit of
all invalided, disabled, or superannuated officers and soldiers, and
the widows of such officers and soldiers as may die in their
service in any of their settlements in the East Indies, pursuant to
an agreement stipulated between them and Lord Clive in the year
1770, by which the former establishment of shares was altered to
the present moieties or proportions specified as follows:—
All commissioned or warrant officers shall have half the ordinary
stated pay they enjoyed while in service.
Serjeants belonging to the artillery shall receive 9d. per day, and
such as have lost a limb 1s. per day: private men of the artillery,
6d. per day; and such as lose a limb, 9d. per day.
All other non-commissioned officers and private men shall receive
4¾d. per day.—23d July, 1771.
Vide Parliamentary Papers, A.D. 1773, vol. iv. report 9. p. 535.
[29] A station adjoining the city of Patna.
[30] Captain Goddard became afterwards a very distinguished
officer. He commanded the force that Warren Hastings sent to the
relief of the settlement of Bombay in 1778.
[31] It is not unworthy of remark, that Sir Robert Fletcher, thus
cashiered by sentence of a court-martial for mutiny, was, in 1775,
appointed, by the Court of Directors, Commander-in-chief of the
army at Madras. There he headed the opposition which set aside
Lord Pigot from the government of Madras in 1776.
No mention is made in the text of John Petrie as one of the
ringleaders of the mutiny of the officers of the Bengal army in
1766.
This man was sent home by Lord Clive on that account with a
rope about his neck; but so much do things depend on the party
who may be in power, or influence, with the Court of Directors,
that this very John Petrie obtained an appointment high in the
civil service at Bengal, through the interest of his friends the
Johnstones, who were in opposition to Lord Clive's party in
England.
[32] 20th May, 1766.
[33] Letter to General Lawrence, dated Calcutta, 1st June, 1766.
[34] Letter to Sir R. Fletcher, 28th June, 1765.
[35] Mahommed Ali, at Madras.
[36] Letter to Sir R. Fletcher, under date 6th August, 1765.
[37] Letter to Mr. Sumner, dated 7th July, 1765.
[38] Letter to General Carnac under date 8th July, 1765.
[39] Letter to Mr. Verelst, dated 11th July, 1765.
[40] Vide letter from Colonel Richard Smith to Lord Clive, under
date 31st August, 1765.
[41] Vide letter to Colonel Richard Smith from Lord Clive, under
date 15th February, 1766.
[42] Letter to Sir R. Barker, under date 16th February, 1766.
[43] Dated 3d February, 1766.
[44] Letter to Mr. Verelst, dated 3d May, 1766.
[45] Letter to Mr. Verelst, 4th May, 1766.
[46] Letter to Sir R. Barker, p. 19.
[47] Letter to Colonel Smith, dated 15th May, 1766.
[48] Dated 16th May, 1766.
[49] Dated 18th May, 1766.
[50] Dated 22d May, 1766.
[51] Letters to Mr. Sumner and Mr. Sykes, dated 23d and 24th
May.
[52] Letter to H. Verelst, Esq., 25th May, 1766.
[53]
"Patna, le 27 Mai, 1766.
"Monsieur,
"J'ai eu l'honneur de votre lettre du quinzième courant. Le sujet
étant aussi intéressant pour vous, il sera, je crois, essentiel
qu'une lettre me soit addressée de votre part et de votre conseil
conjointement.
"Un nombre de nos officiers ont très-déshonorablement quitté le
service, sous un prétexte le plus injuste; à savoir, qu'ils n'ont pas
de quoi subsister; quoique notre militaire est le plus advantageux
du monde. Ils sont actuellement en chemin pour Calcutta.
Comme ils passeront par votre Colonie, j'ai jugé à propos de vous
l'annoncer; et je me persuade que vous ne donnerez point
d'accueil à des gens qui ont agi si indignement.
"Clive."
[54] Letter to Mr. Verelst, 28th May, 1766. Messrs. Abzal's
gardens.
[55] Letter to Sir R. Fletcher, dated 30th May, 1766.
[56] Letter to H. Verelst, Esq., dated 6th June, 1766.
[57] Letter to Sir R. Fletcher, dated 3d July, 1766.
[58] Vide Letter to the Junior Field Officers, 6th October, 1766.
[59] Before Lord Clive left India, he wrote to Sir R. Barker,
earnestly advising him to remain till Colonel Smith returned, and
assuring him of his support to succeed that officer in the
command of the troops in Bengal.
[60] Letter to Sir R. Barker, dated 12th August, 1766.
[61] Letter to Sir R. Barker, dated 8th October, 1766.
[62] Vide Verelst's "View of the English Government in Bengal."
Lond. 1772. 4to, p. 56.
[63] A repetition of this command was among the particular
instructions to Lord Clive in 1764.
[64] Under the establishment of this double batta, a Captain's
commission produced little short of 1000l. per annum: when
reduced, it was worth from 650l. to 700l., as appeared upon the
action of Captain Parker against Lord Clive.
[65] Mill's "History of British India," vol. iii. p. 376.
CHAP. XVI.
IN the preceding two chapters, an account has been given of Lord
Clive's successful efforts in the arduous task of reforming the
abuses, and restoring order and discipline to the civil and military
services of Bengal: it remains to notice other public proceedings of
importance during his last residence in India.
No question connected with this period of his service was, at the
moment and subsequently, the subject of more comment and
discussion, both on the part of the Government at home and of
individuals, than the monopoly of the salt trade; the profits of which
he divided among the Governor, the Counsellors, and the senior civil
and military officers; deeming this indulgence, as he repeatedly
states in his official and private letters, indispensable to the integrity
and efficiency of the public service. Men in high station, he argued,
unless some ample and open allowance was given them, could never
be expected to be reconciled to a strict observance of the covenants
that prohibited presents, nor to the loss of that internal trade which
had been denounced as so ruinous and oppressive. The shares in
the profits from salt were, by his plan, divided according to the rank
and duties of the parties. The amount was known, and, though
liberal, it was limited, and, certainly, would not appear, from the
statements made of it, to be more than a fair remuneration to men
employed as those were to whom it was allotted.
Whether this mode of remunerating service was the best at the
period when it was adopted, and whether the monopoly of the salt
produced in the lower parts of Bengal, which the East India
Company found existing, and have ever since, under one shape or
another, continued, was advantageous or hurtful, on sound financial
principles, are questions which merit notice, both as connected with
the biography of Clive, and with the source of our Indian revenues.
The habits of thinking, and constitution, of the Court of Directors,
rendered them very adverse to granting adequate salaries to those
employed in high stations. These had all (including the military) the
privilege of trading; and to the exercise of this privilege many of the
abuses of the earlier times of the service have been justly attributed.
Clive appears to have made reiterated representations upon this
subject, impressing the necessity of adequate allowances, in some
shape, to the superior officers, in order to animate their zeal, and
preserve their public integrity. The narrow allowance to military
officers, and their being expected to gain by trade, he particularly
condemned. Writing to Major Stibbert[66], he observes, "I have
received your letter of the 17th inst., and am not a little surprised
that you should so soon request leave to return to Calcutta,
considering how short a time you have done duty in your brigade.
Your attention, I suspect, is too much taken up with commercial
affairs; a study very foreign from an officer, even of an inferior rank,
as it must frequently interfere with the services of a military station,
but particularly reprehensible in those to whom a share in the profits
upon salt is allotted. However, I admit that the death of your
attorney may make your presence in Calcutta necessary. You have,
therefore, my permission to leave the cantonments immediately, if
the service will permit, and Colonel Smith has no objection."
The ground on which he felt the necessity of assuring to military
officers of rank liberal allowances, in order that they might suitably
maintain their station in life, and enjoy a reward for long service;
and the necessity he saw for putting an end, on their part, to all
indefinite and indirect perquisites, and of giving to their minds a
tone that should elevate them above all sordid views, and make
them what their stations required they should be, is well stated in a
letter[67] to Sir Robert Barker:—"Colonel Smith is making a vigorous
progress in reforming the abuses that fall under his notice. The
monstrous charges and impositions of quarter-masters, surgeons,
&c., &c., require, indeed, the strictest scrutiny; and he seems
determined to go through it with great spirit and attention to the
Company's interest. Nor shall I be disappointed in the assistance I
expect from you in these matters, whilst I shall, at the same time,
have the satisfaction of knowing that you can enforce wholesome
regulations without creating disgust. The privilege of making bills,
and the long track of frauds introduced under the customary
disguise of perquisites, I wish to see entirely abolished. Every
emolument shall be fixed, plain and open: the medium shall, if
possible, be struck between extravagance and niggardly restrictions:
but economy shall take place. The allowance to field officers will be
so large as to prevent even their wishing for more; and, at the same
time, so reasonable, that I think the Company must approve of
them. A colonel's share of the salt produce will be from 5000l. to
6000l. per annum, or more[68]; lieutenant-colonel's and major's in
proportion; and as a further encouragement, I intend that all the
field officers shall be allowed sufficient to defray the expense of their
table. When all mean advantages are disclaimed and held in
contempt by gentlemen high in the service, reformation will, of
course, be with greater ease introduced among inferiors. You will do
me the justice to believe that I mean this as a general observation
only, and not as a necessary hint, either to yourself or any of the
field officers of your regiment, as I know you are all men of honour
and principle."
The reasons of expediency that led Clive to recommend that high
public officers, civil and military, should be remunerated by shares in
the profits of the salt trade, are stated in numerous letters. He
thought that an open, direct, pecuniary allowance would not willingly
be sanctioned by the Company out of any of the revenues which
flowed into their treasury, and still less from the profits of their
trade; and that, besides, such large avowed allowances would invite
an attack from the Crown on their patronage; and that the grasping
character of the administration in England would lead to a ruinous
interference in the nomination of men to India who had no
recommendation but their high birth and great interest.
It was the above considerations that compelled him to devise the
means he deemed least objectionable of adequately rewarding
service, in order to gain, by the tie of self-interest as well as honour,
those instruments without whose aid he was sensible the great
reform he had resolved to introduce could neither be complete nor
permanent. In Clive's correspondence and measures, at this period,
will be found the origin and introduction of that important principle
of a fair and honourable payment for service, suited to its nature
and the rank and responsibility of the individuals employed, which
has been generally ascribed to the more enlightened policy of a
subsequent administration. That his efforts failed, was owing to the
conduct of others, and particularly the public authorities in England,
who, in their attack upon the salt monopoly and its appropriation,
and in the condemnation of his measures, threw, for a period, a
disrepute upon all that he had done, which led to a revival of a great
proportion of the abuses he had corrected, and a disregard of the
principles he had established. As the salt monopoly and its
appropriation has been a subject of constant attack upon his
character, and continues, so far as the monopoly is concerned, to be
still one upon the Indian Government, the subject merits a cursory
notice, which is all that the limits and objects of this Memoir will
permit.[69]
We have already seen that, by the firman of the King of Delhi, the
English Company possessed the right of trading free from duties.
This privilege was granted to favour the kind of trade they then
carried on, which was confined to exports and imports by sea: and
the dustuck, or passport, of the English presidents or chiefs, was
respected by the Subahdar's officers to that extent. Under this
privilege the President favoured also the private trade of the
Company's servants or officers, which, though not strictly according
to the words of the firman, was never objected to.
As to the internal or carrying trade of the country, to engage in it
never entered into the plans of the Company or its servants, which
were confined to the valuable and profitable traffic between Europe
and India; and, had they thought of it, it is clear that it could not
have been profitably conducted by foreigners under a native
government, which had the power of enforcing justice in the
transactions between them and its own subjects.
But after the deposition of Suraj-u-Dowlah, and the elevation of
Meer Jaffier, the influence of the English in Bengal became
paramount; and, as they were all traders, some of them extended
their views, and availed themselves of their political superiority to
enter into the internal trade also; and, applying their partial freedom
from duties on foreign trade to circumstances totally different,
employed it to exempt themselves from duties even on their illegal
internal commerce; an indulgence which, of course, had never been
intended.
As long as Clive remained in Bengal, he checked these pretensions
by his characteristic firmness and spirit; but no sooner had he left
the country, than there was a general rush of the Company's
servants, and of Europeans of all classes, towards the interior trade
of the three provinces. In the foreign trade, the Company and its
officers had, indeed, the advantage of trading free of duties, but the
returns were tardy, and in some instances uncertain; whereas in the
internal trade the return was rapid and certain; and, as they most
unjustly claimed for this trade the same exemption from duties
which they had enjoyed for the articles of their foreign export trade,
it is clear that they had it in their power to undersell the native
merchant in his own market; that, to the extent of their capital, they
had all the advantages of a monopoly; and that, as their trade
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com