0% found this document useful (0 votes)
40 views54 pages

Instant Ebooks Textbook JavaScript: A Beginner's Guide, Fifth Edition Pollock Download All Chapters

ebook

Uploaded by

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

Instant Ebooks Textbook JavaScript: A Beginner's Guide, Fifth Edition Pollock Download All Chapters

ebook

Uploaded by

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

Full download ebook at ebookmass.

com

JavaScript: A Beginner's Guide, Fifth Edition


Pollock

For dowload this book click link below


https://ebookmass.com/product/javascript-a-
beginners-guide-fifth-edition-pollock/

OR CLICK BUTTON

DOWLOAD EBOOK

Download more ebook from https://ebookmass.com


More products digital (pdf, epub, mobi) instant
download maybe you interests ...

JavaScript A Beginner's Guide Fifth Edition John


Pollock

https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-john-pollock/

Javascript: Crash Course - The Complete Beginners Guide


To Learning JavaScript Programming In No Time!
(Angularjs, jQuery, JavaScript Visually) Nathaniel Hill

https://ebookmass.com/product/javascript-crash-course-the-
complete-beginners-guide-to-learning-javascript-programming-in-
no-time-angularjs-jquery-javascript-visually-nathaniel-hill/

The Norton Field Guide to Writing (Fifth Edition)

https://ebookmass.com/product/the-norton-field-guide-to-writing-
fifth-edition/

The Norton Field Guide to Writing: with Readings (Fifth


Edition) Fifth Edition – Ebook PDF Version

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/

Technical Analysis Explained, Fifth Edition : The


Successful Investoru2019s Guide

https://ebookmass.com/product/technical-analysis-explained-fifth-
edition-the-successful-investors-guide/

Learning Web Design: A Beginner’s Guide to HTML, CSS,


JavaScript, and Web Graphics 5th Edition, (Ebook PDF)

https://ebookmass.com/product/learning-web-design-a-beginners-
guide-to-html-css-javascript-and-web-graphics-5th-edition-ebook-
pdf/

English File Beginner. Teacher's Guide (for speakers of


Spanish) Fourth Edition Christina Latham-Koenig

https://ebookmass.com/product/english-file-beginner-teachers-
guide-for-speakers-of-spanish-fourth-edition-christina-latham-
koenig/

Writing Conversational Korean Book Two Katarina Pollock

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

New York Chicago San Francisco


Athens London Madrid Mexico City
Milan New Delhi Singapore
Sydney Toronto

00-FM.indd 1 17/09/19 5:33 PM


Copyright © 2020 by McGraw-Hill Education (Publisher). All rights reserved. Except as permitted under the United States
Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored
in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program
listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.

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.

eBook conversion by codeMantra


Version 1.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

To my wife, Heather, and children, Eva, Elizabeth, Elaine, and Evan,


Bruce and Joy Anderson, and Dr. J. D. and Linda Andrews

In memory of John and Betty Hopkins, James D. and


Livian Anderson, John William and Edith Hopkins,
Burley T. and Aline Price, “Doc” Flores, and Clifton Idom

00-FM.indd 3 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Foli iv

About the Author


John Pollock is employed as a software developer during
the day and works on Web sites and other projects during
the evening. You can find him on Twitter (@ScripttheWeb)
or LinkedIn (https://www.linkedin.com/in/john-pollock-
82a2b074). John holds a Bachelor of Arts in English from Sam
Houston State University and currently lives in New Waverly,
Texas with his wife, Heather, and children, Eva, Elizabeth,
Elaine, and Evan.

About the Technical Editor


Christie Sorenson is a senior software engineer at ZingChart.
She has worked on JavaScript-based systems since 1997 and
has been fascinated with the evolution of the language. She
has collaborated and been the technical editor on several
JavaScript and HTML books. She holds a Bachelor of Science
in Computer Science from University of California, San Diego,
and now lives in San Francisco with her husband, Luke, and
daughters, Ali and Keira.

00-FM.indd 4 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

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

00-FM.indd 5 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

vi JavaScript: A Beginner’s Guide

Specifying when the Script Should Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

.
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

Try This 3-2: Create an HTML Page with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

.
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

viii JavaScript: A Beginner’s Guide

The Modulus-and-Assign Operator (%=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

.
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

7 JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

.
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
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

x JavaScript: A Beginner’s Guide

The dir Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

.
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

Creating Scripts Using Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

.
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

xii JavaScript: A Beginner’s Guide

13 Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357

.
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 Main Window and New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

.
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

xiv JavaScript: A Beginner’s Guide

16 Further Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

.
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 10: Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

.
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

00-FM.indd 16 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

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

00-FM.indd 17 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

xviii JavaScript: A Beginner’s Guide

What This Book Covers


The 16 chapters of this book cover specific topics on the JavaScript language. The first two
chapters cover the most basic aspects of the language: what it is, what you need to know to
begin using JavaScript, and how to place JavaScript into an HTML file. The middle of the
book (Chapters 3–14) covers beginning JavaScript topics from variables all the way to using
JavaScript with forms. The final two chapters (Chapters 15–16) introduce some advanced
techniques, and point you toward resources if you want to learn more about JavaScript once
you have completed the book.
This book includes a number of special features in each chapter to assist you in learning
JavaScript. These features include

● 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!

00-FM.indd 18 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0

Chapter 1
Introduction to JavaScript

01-ch01.indd 1 27/09/19 9:56


Another random document with
no related content on Scribd:
“It is for you to answer that,” he said, quietly. “It was well done;
quietly and with self-possession.”
“I see!” she said, growing pale. “I have failed!”
“No!” he almost shouted; then, in a low voice that quavered: “It is
not your best scene. It ought to be cut out. It is sometimes. You
have nothing to fear. Did you see the house?”
She shook her head.
“No, I did not look.”
He nodded approvingly.
“That’s right. Take no notice of them! Don’t look beyond the
footlights, and—and—the next scene is a trying one—but I don’t
want to make you nervous!”
“You will not make me nervous,” she said, almost sadly. “I wish that
I could feel it more than I do——”
She turned away, and her lips quivered.
The ballroom was set, crowds of supers were hurrying on to the
stage; the orchestra was playing the familiar music; the audience
were applauding the really handsome scene. Then her time came,
and she went on, and the house listened and watched with rapt
attention. When she went off, there was a distinct round of
applause, but still not enthusiastic; the fire was wanting yet!
There were two London critics in the stalls, and they exchanged
glances and comments.
“Awfully pretty girl!” said one.
“And a lady. Plays well, too,” responded the other.
“Ye-es,” assented the first. “Not at all badly, but, somehow, doesn’t
she strike you as being out of the part, so to speak? Seems as if she
were going through it in a dream! But she’s as beautiful as a dream,
too!”
The balcony scene came on—the scene in which a Juliet, who is a
Juliet, can display her powers to the best advantage. In this scene
are opportunities for the display of love and tenderness, maidenly
fear and modesty, and womanly passion, which no other play can
afford.
Jeffrey, pacing to and fro behind the wings, with fingers lacing and
unlacing themselves, was devoured by anxiety, mitigated by hope.
“Now or never!” he muttered. “This is the scene! Oh! Doris, Doris!
Now you raise my heart to the seventh heaven, or break it!—break
it!”
“Awfully pretty scene, Miss Marlowe,” said Romeo, as they stood
together for a moment or two; “you’ll let yourself go now, I expect!”
“Shall I?” she said, dreamily, almost absently. “I don’t know.”
He looked at her curiously.
“Yes, I think I’d put all I know into this,” he said, gently and
respectfully. “It’s a big scene for both of us.”
“Yes,” she said, in a low voice. Then she glided past him and took
her place on the balcony.
The scene began, the audience was as silent as the grave, as Romeo
entered and made his well-known speech.
Then Doris moved forward to the edge of the balcony, and into the
glare of limelight that poured down upon her.
And then a strange thing occurred. As she sighed, that well-known
sigh, she raised her eyes and all unconsciously looked toward the
house.
It was almost darkened; but a single light had been left in the
chandelier, and it fell upon the handsome face of a young man
sitting in the centre box. He was leaning forward, his eyes fixed
upon her face, a strange intent expression in them. His face was
pale, his hands clasped tightly on the velvet lining of the box-edge,
his whole expression that of one surprised, amazed, bewildered and
fascinated.
She saw the face for a moment only, but she recognized it.
It was the one over which she had bent on the preceding day; it was
Cecil Neville’s!
The color rose to her face, and her hands, clasped tightly on the
balcony edge, trembled. Then she went pale again, and her eyes
were raised to the moon.
Then she spoke, and again, marvel of marvels! the very tones of her
voice seemed altered. There was no longer any trace of the cold
abstraction which had marred the preceding scenes.
Melting, ravishing, they fell upon the audience like drops of dew
upon sun-baked travelers.
A thrill seemed to run through the house. Romeo, experienced actor
as he was, felt the change, the difference, and actually almost
faltered.
Then they took up the scene. No need to dwell upon it; every one
knows it; there is no other like it in the whole range of English
literature.
Like notes of music, sounding the full depths of a girl’s pure passion,
her words dropped from her perfect lips. Her face was like a poem of
Dante Gabriel Rosetti’s; pale, passion-pale, yet eloquent. Every
gesture—as she swept the dark, silky hair from her forehead with an
impatient movement; as she bent forward in the keen hope of
touching Romeo’s hand; as she kissed her fingers to him; as she
pressed her throbbing heart, full to o’erbrimming with love—every
gesture was noted and dwelt upon by the enraptured audience, and
when the scene closed, a wild and unanimous burst of applause
rolled like thunder from pit to boxes, from boxes to pit!
They clapped, they stamped, they cheered. It almost seemed as if a
crowd of rational beings had taken leave of their senses. In plain
truth, she had witched the hearts out of them, and they were
fascinated.
Romeo stood, for the first time in his experience, at a loss what to
do, till there rose from the pit a cry, “Juliet, Juliet!” Then he went to
the wings and, breathless, grabbed at her hand.
“Come on!” he said, excitedly.
But Jeffrey held her fast by the arm. He was pale and trembling, but
his voice was stern and grim.
“No!” he said. “Not yet! This is nothing. Let them wait till the last
scene; then—then, if they want her, she shall go, but not till then!”
The two London critics in the stalls exchanged glances.
“Wonderful bit of acting,” said one. “Really wonderful for so young a
girl!”
“Yes,” assented the second; then he added thoughtfully, “I wonder
what made her wake up. It came quite suddenly, did you notice?”
There was one person in the theatre, one out of the whole crowd,
however, who neither clapped nor cheered, but sat perfectly silent. It
was Lord Cecil Neville. He sat, breathing slowly and heavily, like one
under a spell, his eyes fixed on the spot where she had stood, all his
senses in thrall.
CHAPTER VI.
A BUNCH OF VIOLETS.
He had spent the greater part of the day looking for her, his
disappointment growing hour by hour as he grew convinced that he
should not find her; that he had lost her forever. If he had only
known her name, he could have inquired in the town; but he could
scarcely go about asking people if they had happened to see the
loveliest girl on earth, with dark hair and wonderful blue eyes;
besides, there was, to him, something almost sacred in his meeting
with her, and he shrank from putting commonplace questions about
her.
By luncheon time he was, I am sorry to say, in anything but a good
temper. Fortunately the marquis rarely put in an appearance at that
meal, or, in all probability, there would have occurred an open
quarrel between him and his nephew, and Lord Cecil would have fled
the house. Lady Grace, too, did not appear; she had gone to pay a
visit to a friend in the neighborhood, and Lord Cecil, therefore, ate
his cutlet and drank his Chateau Margaux in solitude.
He was not at all sorry for this, for, to tell the truth, Lady Grace’s
candor, though extremely original, had very much embarrassed him,
and Lord Cecil was too little used to embarrassment to find it
agreeable. She was very beautiful, very charming, and he admired
her very much, but still he felt her absence a relief; he was free to
muse over the unknown, who had eluded his search all the morning.
Suddenly, as he finished his last glass of claret, he remembered the
play-bill he had picked up on the terrace, and it occurred to him that
here was the means of escaping dinner at the Towers; for this night,
at all events, he could get away from the marquis’ sneers and
sarcasm.
“I shall not be home to dinner,” he said to the stately butler. “I think
I’ll go to the theatre.”
“Yes, my lord,” responded the butler, displaying not a sign of the
disgust which the announcement caused him. To think that any one
—a viscount, especially—should prefer going to the play to dining!
“What sort of a theatre is it?” asked Lord Cecil, carelessly, and for
the sake of talking.
“Very good, my lord, I believe,” was the solemn reply. “I’ve heard
that it’s almost as good as a London theatre, and that there is an
excellent company there. They play ‘Romeo and Juliet’ to-night. That
is,” he made haste to add, “I heard some of the under-servants
talking about it; I never go to the theatre myself, my lord. I will send
a small dinner, of three or four courses, at an early hour in the
breakfast room, for you, my lord.”
“All right,” said Lord Cecil, carelessly. “That will give you a lot of
trouble, will it not? I can get a chop or something at the hotel in the
town, can’t I?”
“Oh, no, my lord; it will be no trouble,” the butler made haste to
reply; “the marquis would be much annoyed if your lordship were to
be inconvenienced.”
Lord Cecil nodded; he could scarcely suppress a smile at the butler’s
crediting the marquis with such hospitable sentiments.
“All right,” he said, again; “I’ll have it at half-past six.”
“Yes, my lord,” assented the butler, with a faint sigh; it seemed to
him a dreadful sacrifice; and Lord Cecil soon afterward took up his
hat and went out.
He made his way to the meadows, and stood looking down on the
brook and at the spot where Polly had landed him so nearly upon his
head; and at the bank where the fair unknown, whose face and
voice haunted him perpetually, had sat, and a vague hope dwelt in
his breast that she might, perhaps, revisit the scene as he was
doing.
But an hour passed and she did not come, and he strode off,
moodily, full of disappointment and half angry with himself.
“I am a fool!” he thought. “She has forgotten me by this time. Why
should she come back here? If I were to meet her, what could I say
to her? She’d very likely think me an impertinent snob if I did more
than lift my hat. I couldn’t very well tell her that I have scarcely
thought of anything but her since we parted yesterday and to say
anything less to her would seem to me to be saying nothing at all!”
Thus musing, he went into the town, his stalwart figure, with its
military carriage, his handsome, patrician face, and his Poole-made
clothes, which he wore as if they had grown on him, causing no little
sensation amongst the inhabitants.
But though he stared into the shop windows and looked at every girl
who came in sight, he did not see the girl of whom he was thinking;
and it was nearly seven before he came back to the “small dinner of
three or four courses” which the considerate butler had served for
him in the breakfast room.
He was half inclined to give up the idea of the theatre, and if it had
not been for his dread of the marquis’ society he would have done
so. As it was, he ate his dinner slowly, and enjoyed it, although he
was in love; and then, and not till then, he fully made up his mind to
go.
“I’ll have a brougham round in ten minutes, my lord,” said the butler,
but Lord Cecil declined it.
“I’d rather walk,” he said. “I like a stroll after dinner.”
The butler—more in sorrow than in anger—asked what time he
should send the carriage, but Lord Cecil declined a conveyance for
any part of the evening.
“I’ll walk back,” he said; “I rather like a stroll after the theatre,” and
the butler, with a sigh of resignation, gave him up as a bad job.
As he walked along the lanes, fragrant with the breath of spring, a
thought—a hope—flashed through his mind that he might, perhaps,
see the girl in the theatre. He never asked himself what his object in
seeking her might be; men seldom ask themselves such questions.
Lord Cecil was not an altogether bad character. He was not a
modern Lovelace in pursuit of his prey, by any means. He was not,
in fact, a Lovelace at all. He had lived in a fast set—had been the
star and centre of the crack regiment in which he had held a
commission—had gone through the ordeal of London life as
completely as most young men of title; but he had come out of it, if
he could be said to have come out of it, not altogether unscathed,
but not very badly burned or smirched.
The Nevilles had always been wild, and Lord Cecil had not been any
tamer than his ancestors; but in all his wildness he had drawn the
line. For women in general—for the sex, as a whole—he possessed a
respect which had sometimes amused his less scrupulous
companions.
He had overspent his allowance; lost large sums at baccarat and
kindred games, turned night into day, risked his money and his neck
at steeplechases, and generally, as his friends put it, played Old
Harry, but no woman had, as yet, any indictment against him. He
could truthfully declare, with the Frenchman, on his deathbed: “No
woman can come to my grave and say that for want of heart I broke
hers.”
To women he was always frank and gentle, and the women of his
set adored him. If he had broken no hearts in the sterner sense of
the word, he had all unwittingly caused many to ache, and many a
belle of the London season had “given herself away” to Cissy Neville,
as his intimate friends called him.
And now the marquis had intimated that he must marry Lady Grace.
Lord Cecil thought of last night’s after-dinner conversation as he
strolled along, tried to think of it gravely and seriously, but somehow
he could not; all his thoughts flew, whether he would or would not,
to the dark-haired, blue-eyed girl he had so nearly ridden over in the
meadows. After all, he was not obliged to marry Lady Grace. The
marquis could not compel him, and as for the money—— He
shrugged his shoulders, and, having reached the theatre, put the
subject from him.
It must be confessed that he followed the box-keeper to the private
box he had taken with rather doubtful anticipations.
“Romeo and Juliet” in a country theatre is not always an entrancing
spectacle, and Lord Cecil only wondered how long he should stand
it. He was rather surprised at the air of elegance perceptible, and
still more surprised at the crowded state of the house, and he
congratulated himself, as he looked round at the well-dressed and
aristocratic audience, that he had come in evening dress, for he had
at one time thought of retaining his morning clothes.
He settled himself in his box—he had arrived during the entr’acte—
and looked at the programme.
“Juliet—Doris Marlowe.”
The name struck him at once as a pretty one, and he did not trouble
to read the rest of the cast. Then the curtain drew up on the balcony
scene, and, leaning forward carelessly, he looked at the stage and
saw, there in the balcony—the girl for whom he had been seeking,
the girl with the dark hair and blue eyes!
For a moment he thought he was dreaming, and the color rushed to
his face. Then he looked again, “all his soul in his eyes,” and saw
that he was not dreaming, but that it was in solemn truth she,
herself.
If he had had any doubts her voice would have dispelled them. He
would have remembered and recognized those musical tones if he
had heard them fifty years hence instead of as many hours.
He was amazed, bewildered, engrossed, but not too engrossed to be
aware that the “Juliet” he looked upon, Miss Doris Marlowe, was a
great actress.
If she moved the rest of the vast audience, imagine how she moved
him who had been thinking of her and longing to see her!
His heart beat wildly, the color came and went in his face; he was
lost to everything but that bright, celestial, and yet purely human,
being on the stage, then rendering the exquisite lines of her part;
and it was not until he caught one or two curious glances directed at
him that he drew back a little and tried to look simply interested like
the rest.
The drop scene went down on the act, and he, to use his own
phrase, “pulled himself together.”
He got up and went out into the lobby, and made his way to the
refreshment bar; and when he had obtained his brandy and soda he
lingered over it and got in conversation with the attendant.
“This Miss Doris Marlowe is a great success?” he said, trying to
speak indifferently.
“Oh, yes, she is, indeed,” said the girl, with a long sigh; she had
dreamed of being an actress herself, poor thing; “I just stole out and
looked in at the last act. A success?—I should think so! I call it
magnificent. I never saw anything like it; did you, sir?”
“No, never,” responded Lord Cecil. “She is a London actress, I
suppose? And yet I don’t remember seeing her in London,” he
added.
“No, I don’t think she’s ever played in London, but always in the
provinces. This is the first time she’s ever done anything like this.
She’s played here in small parts; this is her first appearance in
Shakespeare.”
“Who is she?” he asked, endeavoring to make his question
commonplace, yet feeling that he was hanging on her reply.
The girl paused in the wiping of a glass and looked puzzled.
“Who is she? I don’t know, sir. I question whether anybody knows
rightly, excepting Mr. Jeffrey.”
“Mr. Jeffrey? Who’s he?” asked Lord Cecil, with a sharp pang. Could
this man be her husband?
“Oh, the old gentleman who goes about with her. He ain’t her father,
but a kind of guardian. He was an actor once. It was he, so they say,
who taught her to act. Anyhow, she treats him just like a father.”
Lord Cecil drew a breath of relief.
“They are always together; they go from theatre to theatre. He is a
very extraordinary old gentleman, and very trying at rehearsals, so
I’ve heard the actors say; but he knows all about it, quite as well as
the stage manager.”
At this moment the two London critics came up for a drink, and one
of them bowed to Lord Cecil.
“Quite an eventful evening, my lord,” he said, with the easy respect
of a fellow-Londoner.
“Yes,” said Lord Cecil. “It is a great success, I suppose. Do you know
who Miss Doris Marlowe is?”
The critic shrugged his shoulders.
“Haven’t the least idea. Quite a stella incognita, but she will not be
so after to-night. We shall see her in Drury Lane before many
months are passed.”
“Who was that?” his friend, the other critic, asked.
“Lord Cecil Neville,” was the reply. “The heir to the Marquisate of
Stoyle. A splendid fellow, and, strange to say, not a bit spoiled,
though all the women make a dead set at him.”
“The Marquis of Stoyle,” said the other thoughtfully. “That old villain?
And this is his nephew. He is immensely good-looking.”
“Oh, a splendid fellow. Did you ever hear that story about him——?”
and they moved away.
Lord Cecil drank half his soda and brandy, and then went back to his
box.
Meanwhile, a thrill of excitement seemed to run through those
engaged behind the scenes. A theatre is rendered famous by its
actors, and it seemed that the Theatre Royal, Barton, was going to
be made celebrated as the place of the first appearance of a great
actress.
“If she can only carry us through to the end!” muttered Jeffrey, as
he paced to and fro, his hands clasped behind his back, his eyes
flashing fire.
“Oh, she’ll do it!” said the manager, who happened to hear him.
“Don’t you be afraid, Mr. Jeffrey; that young lady is a genius! I knew
it from the first. She will carry it through to the very last. And about
the engagement now? You make your own terms, and I’ll agree to
them. You’ll find me straight and honest——”
But Jeffrey paced on. He was an old theatrical hand, and he knew,
full well, that a Juliet may score in the balcony scene and yet fail in
the later and more important ones.
But there did not seem much fear of failure with Doris.
Off the stage, and in her dressing room, she was quiet and subdued,
but the moment she got on the boards her eyes flew to the centre
box, and she seemed to draw inspiration from the handsome face
that leaned forward in rapt, almost devout, attention.
The play proceeded. The great scene, in which Romeo takes leave of
Juliet, his newly-made wife, went with a rush. The audience cheered
until it was hoarse. Thrice the young actress was called to the front,
and everybody who had brought a bouquet flung it at her feet.
Jeffrey, pale and statuesque, implored Doris to be calm.
“It is not all over yet!” he said, warningly. “There is the last scene.
Remember what I taught you! It is the last scene in which a Juliet,
who is a Juliet, declares herself! Do not let their applause make you
forget what is due to your art! I would rather that they remained
mute and silent, Doris.”
And for answer she simply smiled. She did not tell him that while she
could see a certain face in the centre box all would be well.
The pause before the last scene arrived. The whole house was
talking in excited whispers. To the Barton folk, ardent theatre-goers
as they were, nothing like this had befallen them. A twitter of
excitement ran through the house, and amongst the crowd that
thronged the lobbies Lord Cecil walked about, as excited as the rest.
Suddenly, as if he had been stricken by an idea, he turned up the
collar of his coat and made his way through the press to the streets,
and looked about him eagerly.
Some women selling oranges came hurrying up to him, and amongst
them a woman with a basket of violets.
He bought the whole contents of her basket, and bade her tie them
together; then, with the flowers in his hand, he went back to the
theatre; but, instead of going to his box, he made his way to the
stalls and stood close to the orchestra.
The last scene came on. Again it is unnecessary to describe it; the
grim and solemn vault, the beautiful figure of the girl in the death
throes, the terrible agony of Romeo, were all here, rendered real
and lifelike by the genius of the actors.
Spellbound, the house watched and listened in profound silence.
Listened to the passionate, despairing plaint of Romeo, and the
deeper agony of Juliet, who awakes to find her lover dead.
Never, perhaps, since the play was played, was actress more
touching, more tear-compelling than Doris Marlowe that night at the
Theatre Royal, Barton; and as her last words died away in solemn
silence, a great sob seemed to rise from the crowded house.
Then the sob gave place to a thunder of applause. Once more the
sober audience seemed possessed by a spirit of delirium; men
sprang to their feet and waved their hats, women rose and waved
their handkerchiefs with which they had wiped away their tears; and
cries of “Juliet! Juliet!” resounded through the theatre.
A pause, and presently Romeo appeared, leading Juliet by the hand.
The audience stormed and cheered as one man, and those who had
not already thrown their bouquets to her threw them now.
She was pale to the lips, and the blue eyes looked almost black as
she bent them on the cheering crowd, and like a queen bowed
beneath the tribute of their devotion, she bent her girlish head low.
She had nearly crossed the stage, had reached the spot exactly
opposite that on which Lord Cecil stood. Then, and not till then, he
raised his bunch of violets and tossed them at her feet.
She paused a moment in her triumphant progress, for it was nothing
less, then stooped and picked up the rough-and-ready bouquet;
Romeo’s arms were quite full.
For an instant her eyes rested on Lord Cecil’s face, then, as if with
an involuntary movement, she raised the bunch of violets to her lips
and passed off, the side wings engulfing her.
Three times more they called her, as if they could not let her go from
their sight, and thrice she came before them, and, modestly,
girlishly, bowed her acknowledgments.
Then—tired, hot and thirsty—the crowd began to disperse.
Lord Cecil Neville alone remained on the spot from which he had
thrown his bouquet. He could scarcely believe that it was over, until
the attendants began to cover up the seats with their calico
wrappings, and, taking the hint, he made his way out.
The groups of people he passed through were talking about her
triumph. He caught a word here and there, and, all unconsciously,
found himself at the stage door. At least, he thought, he should get
a glimpse of her as she drove away from the theatre.
Meanwhile, behind the scenes the greatest excitement prevailed.
There had never been a Juliet like her, they were declaring; and they
prophesied a success in London which should even eclipse that of
Barton!
And Doris, looking pale, stood smiling dreamily through it all. Even
while Jeffrey paced to and fro in her dressing-room, too excited for
speech, she remained calm and serene, wrapt in a kind of spiritual
veil.
Managers, actors, thronged round her with congratulations; even the
old dresser, declared, with tears, that “nothing had been seen like it.”
At last, the porter announced that Miss Marlowe’s fly was waiting,
and Jeffrey took her away from the excited crowd.
“Draw your cloak well round your throat,” he said, as anxiously as if
she were so fragile that a breath of wind would sweep her away.
“Give me those violets to hold for you,” he said.
She drew her hand back, almost with a gesture of dread, and a dash
of color came flying into her pale face.
“No, no—I can manage, thanks,” she said, quickly. “How sweet they
smell, do they not?” and she held them up to him for a second.
“Yes,” he said, absently. “Were they thrown with the rest?”
“Yes,” she said, in a low voice.
“Some one of the poor people in the pit, I daresay,” he said; “a
graceful and spontaneous tribute, worth, I was going to say, all the
rest of them, beautiful and costly though some of the bouquets are.
But I daresay you don’t agree with me?” and he smiled.
“But I do,” she said, averting her eyes. “Yes, I think them worth all
the rest!”
They had traversed the long passage by this time, and reached the
fly. Jeffrey put her in carefully, and was himself following, when he
stopped suddenly, frowning and biting his lips.
“Doris,” he said, “you leave all to me? You leave all to my judgment,
as hitherto? You are a famous woman now, or will be to-morrow,
and may like to be independent. Would you rather wait till to-
morrow and make your own arrangements with the manager, or
shall I, as of old——”
“Jeffrey!” she broke in, with a reproachful look in her eyes.
“Very well,” he said. “Brown has made me a very large offer for a
month. I put him off just now, but I think I will go back and accept
for you. I shall not be many minutes.”
Doris leaned back, and, closing her eyes, pressed the violets against
her cheek. She could see the handsome face all aglow with
excitement and admiration as he raised his right arm and flung the
flowers; she could see it at that moment, and the mental vision shut
out all the rest of that eventful night.
Suddenly she heard her name spoken beside the carriage window,
and, leaning forward, she saw, in real earnest, the face which had
been her inspiration. It was Lord Cecil Neville’s.
“Miss Marlowe,” he said, learning forward and speaking quietly,
pleadingly. “Don’t be angry! Pray forgive me! I—I could not pass on
without saying a word—one word of thanks.”
“Thanks?” she murmured.
Her eyes were lifted for a moment to his ardent face, then dropped
to the violets and rested there.
“Yes. I was in the theatre,” he said. “You did not see me, of course,
but I was there, and—I can’t tell you how we all felt, how we all feel.
It was superb; it was—but there, I can only thank you.”
“You have done that already,” she said, with a smile, as she raised
the violets.
Lord Cecil Neville blushed. I am afraid it would be rather difficult to
get credit for this statement in certain quarters in London.
“I couldn’t get any better ones,” he said, apologetically.
“No,” she said; “I think you could not! Yes, I saw you in the theatre,”
she added, as if she had been thinking of his first sentence. “Were—
were you surprised, or did you know?” and she glanced at him with
a half curious smile.
“Surprised!” he said. “I could scarcely believe my senses! I had no
idea, until I saw you on the stage, that you, who were so good to
me yesterday, were a great actress.”
“I am not,” she said, in a low voice. “I am only a very little one. To-
night I succeeded, another night I might fail——” a faint shadow
came on her face, as he looked puzzled; then she smiled, as she
broke off, to add: “I have something of yours——”
“Yes, my heart!” was his mental comment, but he said aloud: “Of
mine?”
“Yes,” she said. “A handkerchief, I haven’t it here,” and she smiled
again; then, suddenly, her face grew crimson, for she remembered
that she had left it in the bosom of her dress. “I—I will send it to you
if you tell me where.”
“Let me call for it,” he said, eagerly.
Doris’ brows came together, and she shook her head gently. She
knew that Jeffrey’s welcome to a stranger would be a rough one.
“I will send it,” she said. “I think I know—the Towers, you said, did
you not?”
A sudden inspiration seized him, and, bending forward, he said, in a
low voice:
“If you should walk in the fields to-morrow morning—you may, you
know!—lay it on the bank where you sat yesterday. Will you do this,
Miss Marlowe? I will fetch it in the afternoon.”
The beautiful eyes dwelt upon his face with a deep gravity for a
moment, as if she were wondering what his object could be in
making the request; then she said, gently:
“Yes, why should I not?” as she held out her hand; “good-night.”
“Thanks, thanks!” he said, in his deep, musical voice. “Good-night!
You should be happy to-night, for you have made so many people
miserably so! I shall dream of Juliet all night!”
She let her hand rest in his for a moment, then drew it away and he
was gone.
But at that moment it chanced that a handsomely-appointed
carriage came round the bend of the road, and a lady, with softly-
shimmering hair and darkly-brilliant eyes, who was leaning back in a
corner of it, suddenly caught sight of the fly and the stalwart figure
standing beside it.
She bent forward eagerly, and her keen eyes took in, as the carriage
rolled past, not only the expression of Cecil Neville’s face, but the
face of the girl in the fly.
For an instant the warm blood rushed to Lady Grace’s face; then, as
she sank back again into her corner, she laughed, a laugh of cold,
insolent contempt.
“Some actress or shop-girl,” she murmured. Then her expression
changed, and she bit her lip thoughtfully. “And yet he looked terribly
in earnest!” she added. “Shall I take him up?” and her hand went
out to the check-string; then she let it fall, and the carriage go on its
way. “No; I think I’ll keep my little discovery to myself—it may be
useful—and let you walk home, Lord Cecil!”
CHAPTER VII.
A RARE DIAMOND.
When Doris came down from her room the next morning, it did not
seem as if the tremendous excitement of the preceding night had
left any baleful effects. In her soft-white dress, she still looked more
like a schoolgirl home for the holidays than the tragedienne who
had, a few hours ago, moved a vast audience to tears and wild
enthusiasm.
She came into the room singing, just as the birds sang under the
eaves by her window, and laughed lightly as she saw Jeffrey bending
earnestly over a copy of the local daily paper.
“Well, have I got a tremendous slating, Jeffrey?” she said, almost
carelessly.
“Slating!” he replied. “If anything, it is too laudatory; read it!” and he
held it out to her.
“After breakfast; I am so hungry,” she said, contentedly. “Read it to
me, Jeffrey; all the nicest paragraphs,” and she laughed again.
He glanced at Doris under his heavy brows.
“At any rate, your success has not made you vain, Doris,” he said
with grim approval.
“If it should make any one vain, it should you—not me, dear,” she
said, quietly. “It was you made last night’s Juliet, good or bad.”
“Very well,” he said; “I’ll be vain for both of us. Yes, it is a
wonderfully good critique, and I think the news of your success will
reach London, too. There were a couple of critics from London in the
stalls; I didn’t tell you last night, in case it should make you
nervous.”
She looked at him thoughtfully.
“I don’t think it would have made much difference,” she said. “I
seemed to forget everybody and everything——”
“After the second act,” he put in.
She blushed to her temples.
“There was a distinct change, then; I noticed it, and I have been
puzzling my brain to account for it. Perhaps you can explain it.”
She shook her head, and kept her eyes on her plate.
“No? Strange. But such inspirations are not uncommon with genius;
and yours is genius, Doris.”
“Don’t frighten me, Jeffrey,” she said, with a faint smile.
“I have agreed with Brown, the manager,” he went on, “that you
should play Juliet for a week, and after that some other of the big
characters for a month, and he is to pay you ten pounds a week.”
Doris looked up, surprised. Ten pounds per week is a large sum for
merely provincial actresses.
He smiled grimly.
“You think it a great deal? In a day or two you will get offers from
London of twenty, thirty, forty pounds. But I am in no hurry. I have
not been in a hurry all through. I want you to feel your feet, to feel
secure in all the big parts here in the provinces before you appear in
London. Then your success will be assured whatever you may
undertake.”
“You think of everything, Jeffrey,” she said, gratefully.
“I have nothing else to think of, nothing else to tell you!” he
responded, quietly, almost pathetically. “I have set my heart upon
you being a great actress and”—he paused—“I think it would break,
if you failed. But there is no need to speak of failure after last night.”
He got up as he spoke and folded the newspaper.
“I’m going down to the theatre,” he said; he was never quite
contented away from it. “You’d better look over your part this
morning. Take it into the open air as you did the other day; it seems
to succeed.”
“Very well,” she said, obediently.
He put on his hat and the thick inverness he wore in all weathers,
and went away, and Doris sat looking dreamily before her.
Then, suddenly, she got up. She would take his advice and go into
the meadows—for the meadows meant the open air to her—and as
she was going she would take Cecil Neville’s handkerchief and place
it on the bank as he had requested.
She put on her hat and jacket, and, possibly for the convenience of
carrying, thrust the handkerchief in the bosom of her dress, where it
lay hidden all the preceding day, and started.
It was a glorious morning, with only a feather of cloud here and
there in the sky, and the birds sang as if winter were an unknown
season in England.
With her stage copy of “Romeo and Juliet” under her arm, Doris
Marlowe, the simple child of nature, the famous actress, made her
way to the meadows.
The Barton folks have something else to do than wander in their
meadows, and Doris did not meet a soul; the great elms, which
threw their shadows over the brook, were as solitary as if they had
been planted in Eden. But lonely as the spot was, Doris peopled it
with memories; and she stood by the brook and recalled the vision
of the powerful figure on the great horse, as it appeared before her
the moment prior to its being hurled at her feet.
“How strange that he should have been at the theatre last night!”
she thought. “How curious it must have seemed to him, seeing me
there as Juliet! I wonder whether he was sorry or glad!”
She could not answer the question to her satisfaction, but she stood
motionless for a moment or two, recalling the words he had spoken
as he stood beside the fly last night.
Then she took the handkerchief from her bosom, and, folding it with
careful neatness, placed it on the bank where she had sat.
“It is not likely that any one will come here before he comes to fetch
it this afternoon,” she said.
Almost before the words were out of her lips a stalwart form leaped
the hedge, and stood before her.
Doris started and her face flushed; then, pale and composed, she
lifted her eyes to his.
“Well, now!” he said, in humble apology, “I seem fated to startle
you, Miss Marlowe. I had no idea you were here——” he stopped,
awed to silence by her silence.
“You said you would come for it in the afternoon,” she remarked,
almost coldly.
He colored.
“Yes, I know; but I could not come this afternoon, and I thought
——” he stopped, and raised his frank eyes to her face, pleadingly.
“You thought?” she said very gravely, her brows drawn together
slightly.
“Well,” he said, as if with an effort, “I will tell the truth! I thought
that if I came this morning I might meet you. It was just a chance.
Are you angry?”
She was silent a moment. Was she angry? She felt that she ought to
be; and had a suspicion that he had, so to speak, entrapped her into
a meeting with him; and she honestly tried to be angry.
“It does not matter,” she said, at last, very coldly. “There is your
handkerchief.”
He picked it up, and thrust it in his pocket.
“Oh, thank you, thank you!” he said, gratefully. She turned to go,
with a slight inclination of her head, but he went on, speaking
hurriedly and so earnestly, that she paused, her head half turned
over her shoulder, her eyes cast down; an attitude so full of grace
that it almost drove what he was going to say out of his head. “I
don’t deserve that you should have brought it.”
“I don’t think you do,” she assented, a faint smile curving her lips at
his ingenuousness.
“I daresay you think it strange that I didn’t ask you to send it to the
Towers?” he went on. “You know you would not let me call at your
place for it,” he added, apologetically.
“Why did you not let me send it?” she asked, with faint curiosity.
“Well, I’ll tell you,” he said. “Won’t you sit down and rest? It’s warm
this morning, and you have walked far, perhaps.”
She hesitated a moment, then sat down, almost on the spot she had
sat the preceding day, and Cecil Neville could not help a wild wish
rushing to his heart that he was once again lying at her feet!
He sat down on the bank, as near to her as he dared, and leaned on
his elbow toward her.
“You see, I’m only a visitor at the Towers. The marquis—that’s my
uncle, you know——”
“I don’t know,” she said, with a faint smile, her eyes fixed dreamily
on her book.
“Of course not,” he assented. “Well, we don’t get on together. He is
—not to put too fine a point on it—about as disagreeable a person
as you’d find in two days’ walk! We never have got on together.
They say that a man always hates the fellow who is to come after
him, unless it happens to be his own son; and I suppose that’s the
reason the marquis hates me——”
“Because you are to be the next marquis?” she said.
He nodded coolly, and tilted his hat so that it screened his eyes from
the sun, and permitted him to feast upon her beautiful face more
completely.
“Yes, that is about it; but I’ll give the marquis the credit of hating
everybody all round, himself into the bargain, I dare say; but I fancy
he reserves a special line of detestation for his own relatives. Ah,
you are smiling,” he broke off, with the short laugh that sounded so
good and frank. “You are wondering what this has to do with my
disliking you to send the handkerchief!”
Doris smiled again in assent.
“Well, you see, I thought it might come into the marquis’s
possession, or that he’d hear of it through Lady Grace——”
She turned her eyes upon his, not curiously, but with graceful
questioning.
“That’s a lady—Lord Peyton’s daughter—who is stopping there,” he
explained, “and they might ask questions, and—and bother me
about it!”
“Well?” she said, quietly.
He looked down half hesitatingly, then met her eyes, which seemed
in their fixed regard to reach to his soul.
“Well—I’ve said that I’ll tell you the whole truth, and I will; and the
fact is I didn’t want to be asked questions about the—the accident
yesterday. I—yes, I’ll speak out, though I should offend you—I
wanted to keep it to myself!”
“To keep it to yourself?” she repeated.
A flush came to his tanned face, and his eyes were raised for a
moment.
“Yes. When a man gets a good thing—Suppose—” he broke off—“a
fellow found a big nugget, or a rare diamond, or anything of that
sort, he would like to keep it to himself, you know!”
She smiled again.
“Do you want me to take that as a compliment?” she said. “Am I the
big nugget, the rare diamond which you discovered?”
He flushed more deeply, and looked at her pleadingly.
“I’m such an idiot that I can’t express myself,” he said,
apologetically. “I meant that the whole thing, your—your kindness
and goodness to me was so precious that I didn’t want a lot of
people talking about it. I wanted to keep it to myself, as something
especially belonging to me, something too precious to discuss with
others. I’m afraid I can’t make you understand.”
“You do yourself an injustice,” she said. “You express yourself very
well!”
“Now, you are laughing at me,” he said.
“As you would laugh at me, Lord Neville, if I believed what you said!”
she retorted, not sharply, but with a sweet gravity that was
indescribable.
“I said I would tell you the truth, and I’ve told nothing but the truth,”
he said, earnestly. “I dare say it seems strange to you that I should
have this feeling about our meeting yesterday. I dare say you forgot
all about it half-an-hour afterward! Why should you remember it,
you who have so much to think of?”
Doris turned her face away, lest her eyes should betray her, and tell
him how much, how constantly she had thought of him!
“You,” he went on, “who are so clever and gifted, a great actress,
with no end of people round you——”
She looked at him with a pensive smile.
“But you are wrong, quite wrong,” she said. “I am not a great
actress. Last night was my first success, if success it was——”
“There is no ‘if’ about it!” he said, with fervent enthusiasm. “It was a
tremendous success! Why, I heard people declare that there had
been nothing like it since Kate Terry’s Juliet! And I—though I’m not
of much account—I was never so much carried out of myself. Why,
to tell you how great and grand you were, I actually forgot that you
were the young lady who was so good to me yesterday, and only
thought of you as Shakespeare’s Juliet; and I felt quite ashamed that
I had ever given so much trouble to so great a personage.”
His warm, ardent praise touched her, and her lips quivered.
“Juliet was only a simple girl, after all,” she said. “If she had chanced
to have been placed in my position yesterday she would have done
the same.”
“I don’t know about that,” he said. “I’m not clever, like you,” and he
pushed his hat off his brows with a deprecatory gesture. “But I know
you must have something else to think of than the fellow who was
such an idiot as to jump a hedge before he saw what was on the
other side; and, of course, you must have no end of—of people
round you!”
“But I have not! You are quite wrong,” she said, with her sweet,
thoughtful smile. “I live with an old friend, who has been like a
father to me! I haven’t any father or mother, and I see no one,
except at the theatre, and then only in the way of business,” and she
laughed.
He listened as if every word she dropped from her sweetly-curved
lips were a pearl.
“How strange it sounds! You so clever and beau——so great an
actress.”
“Yes,” she said dreamily; “I suppose it does sound strange!
Everybody thinks that an actress must be the gayest of the gay;
surrounded by light-hearted people turning night into day, and living
on champagne and roast chicken.” She smiled. “Jeffrey and I know
scarcely any one, and I do not think I have tasted champagne,
excepting once, when one of the managers had a benefit; and we
go straight to bed directly we get home from the theatre; and, oh, it
is quite different to what people imagine.”
He drew forward a little, so that the hand upon which he leaned
touched the edge of her cotton dress.
“And—and you didn’t quite forget our strange meeting?”
“I am not in the habit of seeing gentlemen flung from their horses at
my feet, Lord Neville,” she said, but she turned her face from him.
“And I,” he said. “Why, I have not been able to get it out of my
head! I thought of you every minute; and I tried not to, because
——”
“Because?” she said. “Pray go on!” and she smiled.
“Well,” he said, modestly, “because it seemed like presumption. And
then I went to the theatre, and——” he stopped. “For a moment or
two I couldn’t believe that it was really you on the stage there. And
when the people in the theatre began to shout out your name, it
woke me from a kind of dream.”
She smiled in silence; then she made a movement threatening her
departure.
“Ah, wait a little while!” he pleaded. “It is delightful here in the
sunshine. Don’t go for a minute or two. I wish——” he stopped.
“What is it you wish?” she asked, regarding him with smiling eyes
that drooped under his ardent ones.
“Well,” he said, “I wish that you would let me go home with you and
see Mr. Jeffrey——”
“Jeffrey Flint,” she said. She shook her head. “He sees no one,
makes no acquaintances. He—he is very reserved.”
Speaking of him reminded her of the fact that he would strongly
disapprove of her interview with this strange young gentleman. She
rose.
“I must go now,” she said. “I have not asked whether you were hurt
by your fall, Lord Neville, but I hope you were not.”
“Must you go?” he said, ignoring the rest of her sentence as of no
account. “We seem to have been talking only a few minutes! And
there was such a lot that I wanted to say! I wanted to tell you all
that I thought when I saw you last night; but I couldn’t if I had the
chance, because I am a perfect idiot when it comes to expressing
myself. But I do think it was wonderful! Are you going to play to-
night? But of course you are.”
“Yes,” she said, absently, “I play to-night. I play every night!”
“I shall be there,” he said, as if it were a matter of course.
She looked at him thoughtfully.
“Of course I shall!” he said. “Why, last night I seemed to have a kind
of interest in it which the other people in the theatre hadn’t. Yes. As
if—as if—I knew you intimately, you know. Of course, I shall be
there! And I shall bring a big bouquet. What flowers do you like
best?”
She almost started, as if she had not been listening to him; as a
matter of fact, she had been listening to the deep, measured voice
rather than the words.
“Flowers?—oh—violets,” she said, unthinkingly.
“Why!” he exclaimed. “That is what I threw you last night! Of
course, you didn’t know. You can’t see beyond the footlights, can
you? I’ve heard you can’t. Violets! I’ll get some. I shall take a seat in
the stall to-night. I shall see and hear you better there.”
“I should have thought you had seen and heard me enough already,”
she said with a smile.
“No, but I haven’t!” he responded, eagerly. “I couldn’t see you or
hear you too much if I looked at you and listened to you all day!”
Her face grew crimson, but she turned her head toward him with a
smile on her face.
“For flattery, pure and simple, I don’t think you could surpass that,
Lord Neville.”
“Flattery!” he exclaimed, as if hurt. “It is no flattery, it is the honest
truth. And, Miss Marlowe, I do not ask you to believe—” he saw her
start and lift her head as if listening, and looking up to ascertain the
cause, saw that her eyes were fixed upon some spot behind him,
and he heard the sound of footsteps.
“I must go,” she said, as if suddenly awakened to a sense of the
situation.
“Ah, no,” he breathed; then he leaned toward her with half-timid
eagerness. “Will you come to-morrow?”
The footsteps came nearer.
“I promise—nothing,” said Doris, her brows coming together, and
with a half glance at his earnest face she glided away from him.
Lord Neville rose and looked after her with the expression which
encompassed the desire to follow her; but in that moment a hand
fell lightly upon his shoulder, and a voice exclaimed:
“What, Cissy!”
Lord Neville swung round.
“Hallo, Spenser!” he said. “Why, what on earth brought you here?”
CHAPTER VIII.
SPENSER CHURCHILL.
The new comer was a man apparently of middle age; I say
apparently, because opinions on that subject were extremely
conflicting. Some persons regarded Spenser Churchill as quite a
young man, others declared that he had reached the meridian of life,
and there were some who were inclined to think that he was, if
anything, on the verge of old age. His appearance was singular. He
was of medium height, with a figure that was either naturally
youthful, or admirably preserved. He was fair almost to effeminacy,
and he wore his hair long and brushed back from his face; and he
was close shaven. But it was not the length of the hair that lent him
his singularity, but the expression of his face and his manner.
If he was not the most amiable of men, his countenance belied him.
There was always a smile, soft and bland, and good-tempered in his
eyes, on his lips, and as the Irishman said, “all over him.” The smile,
in conjunction with the fair face and long hair, gave him as confiding
and benevolent an expression that the world had long ago come to
the conclusion that Spenser Churchill was the epitome of all the
virtues.
Most women were fond of confiding in him; most men—not all—
trusted him; he was regarded by crossing-sweepers, waiters and
beggars generally as their natural prey, and so effective was his
smile, that even when he did not bestow his alms, he always
received a blessing from the disappointed ones.
Whenever his name was mentioned, some one was sure to say:
“Oh, Spenser Churchill! Yes! Awfully good-natured fellow, you know.
No end of a good soul. Share his last crust with you. Kind of cherub
with legs, don’t you know.”

You might also like