100% found this document useful (3 votes)
144 views

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

Pollock

Uploaded by

garoujhazza6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
144 views

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

Pollock

Uploaded by

garoujhazza6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Download Full Version ebookmass - Visit ebookmass.

com

JavaScript: A Beginner's Guide, Fifth Edition


Pollock

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

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmass.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

JavaScript A Beginner's Guide Fifth Edition John Pollock

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

ebookmass.com

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/
ebookmass.com

The Norton Field Guide to Writing (Fifth Edition)

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

ebookmass.com

Mosaics of Knowledge: Representing Information in the


Roman World Andrew Riggsby

https://ebookmass.com/product/mosaics-of-knowledge-representing-
information-in-the-roman-world-andrew-riggsby/

ebookmass.com
eTextbook 978-0470444047 Facilities Planning 4th Edition

https://ebookmass.com/product/etextbook-978-0470444047-facilities-
planning-4th-edition/

ebookmass.com

That Night Cyn Balog

https://ebookmass.com/product/that-night-cyn-balog-2/

ebookmass.com

(eBook PDF) A Speaker’s Guidebook 7th Edition

https://ebookmass.com/product/ebook-pdf-a-speakers-guidebook-7th-
edition/

ebookmass.com

Quentin Tarantino and Film Theory: Aesthetics and


Dialectics in Late Postmodernity 1st ed. Edition Federico
Pagello
https://ebookmass.com/product/quentin-tarantino-and-film-theory-
aesthetics-and-dialectics-in-late-postmodernity-1st-ed-edition-
federico-pagello/
ebookmass.com

El oscuro adiós de Teresa Lanza Toni Hill

https://ebookmass.com/product/el-oscuro-adios-de-teresa-lanza-toni-
hill-2/

ebookmass.com
The Insurgent's Dilemma David H. Ucko

https://ebookmass.com/product/the-insurgents-dilemma-david-h-ucko/

ebookmass.com
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


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

2 JavaScript: A Beginner’s Guide

Key Skills & Concepts


● Using Text Editors, WYSIWYG Editors, and Web Browsers
● Defining JavaScript
● Differences Between Client-Side and Server-Side Programming

W elcome to JavaScript: A Beginner’s Guide, Fifth Edition! You’re obviously interested in


learning JavaScript, but perhaps you’re not sure what you need to know to use it. This
chapter answers some basic questions about what JavaScript is, provides a brief history of the
language, and discusses the various environments that can use JavaScript for programming.
JavaScript is ubiquitous on the World Wide Web. You can use JavaScript both to make
your Web pages more interactive, so that they react to a viewer’s actions, and to give your Web
pages some special effects (visual or otherwise). JavaScript can now even be used to perform
input/output operations or build Web servers by using Node.js!
JavaScript often gets included with Hypertext Markup Language (HTML) and Cascading
Style Sheets (CSS) as the three recommended languages for beginning Web developers
(whether you build Web sites for business or pleasure). Of course, you can build a Web page
by using only HTML and CSS, but JavaScript allows you to add additional features that a
static page of HTML can’t provide without some sort of scripting or programming help.

What You Need to Know


Before you begin learning about JavaScript, you should have (or obtain) a basic knowledge of
the following:

●● HTML and Cascading Style Sheets (CSS)


●● Text editors and Web browsers
●● The different versions of JavaScript
●● Differences in client-side and server-side programming

If you have this basic knowledge, you’ll do just fine as you work through this book. Knowing
another programming/scripting language or having previous experience with JavaScript isn’t
required. This book is a beginner’s guide to JavaScript.
If you think you don’t have enough experience in one of the aforementioned areas, a closer
look at each one may help you decide what to do.

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


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

Chapter 1: Introduction to JavaScript 3

Basic HTML and CSS Knowledge


While you don’t need to be an HTML guru, you do need to know where to place certain
elements (like the head and body elements) and how to add your own attributes. This book
will reference scripts in the head section (between the <head> and </head> tags) and the body
section (between the <body> and </body> tags).
Sometimes, you will also need to add an attribute to a tag for a script to function properly.
For example, you may need to name a form element using the id attribute, as shown in the
following code:
<input type="text" id="thename">

If you know the basics of using tags and attributes, the HTML portion shouldn’t pose any
problems in learning JavaScript.
If you don’t have a basic knowledge of HTML, you can learn it fairly quickly through
a number of media. For example, you can buy a book or look for some helpful information
on the Web. A good book is HTML: A Beginner’s Guide, Fifth Edition by Wendy Willard
(McGraw-Hill, 2013). To find information about HTML on the Web, check out developer
.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Getting_started.
Occasionally, you will need to use CSS to add or change presentation features on a Web page.
We will mainly use CSS for the purposes of dynamically changing CSS properties via
JavaScript in this book. A good place to learn CSS is developer.mozilla.org/en-US/docs/Learn/
CSS/Introduction_to_CSS/How_CSS_works.

Basic Text Editor and Web Browser Knowledge


Before jumping in and coding with JavaScript, you must be able to use a text editor or HTML
editor, and a Web browser. You’ll use these tools to code your scripts.

Text Editors
A number of text editors and HTML editors support JavaScript. If you know HTML, you’ve
probably already used an HTML editor to create your HTML files, so you might not have
to change.
However, some HTML editors have problems related to adding JavaScript code (such as
changing where the code is placed or altering the code itself when you save the file). This is
more often the case when using WYSIWYG (What You See Is What You Get) editors. It is
best to use a code editor such as Sublime or Visual Studio Code (recommended) or a plain text
editor. Some examples of text editors are Notepad, TextPad, and Simple Text.

Web Browsers
Again, if you’ve been coding in HTML, you probably won’t need to change your browser.
However, some browsers have trouble with the newer versions of JavaScript. The choice of

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


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

4 JavaScript: A Beginner’s Guide

Web browser is ultimately up to you, as long as it’s compatible with JavaScript. I recommend
the latest version of one of following browsers to test your JavaScript code:

●● Google Chrome
●● Mozilla Firefox
●● Microsoft Edge

New versions of these browsers continue to be produced. The newest versions will continue
to support more features.
To give you an idea of what some browsers look like, Figure 1-1 shows a Web page when
viewed in Chrome, and Figure 1-2 shows a Web page when viewed in Mozilla Firefox.
If you have an older browser and you can’t upgrade, a number of features (mostly discussed
later in the book) may not work in that browser. Even so, the book can still help you learn the
JavaScript language itself (especially when you’re using Node.js instead of a browser), so you
don’t need to give up if you have an older browser.

Figure 1-1 A Web page viewed in Google Chrome


 
01-ch01.indd 4 27/09/19 9:56
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0

Chapter 1: Introduction to JavaScript 5

Figure 1-2 A Web page viewed in Mozilla Firefox


 
NOTE
Even if you have one of the latest browsers, your web site viewers may not, so it
is always appropriate to understand what features may not be supported in older
browsers. This book will cover how to handle a number of these issues.

Which Version?
The version of JavaScript being used by a browser is typically associated with what version
of ECMAScript it supports. You can see what ECMAScript versions are supported by each
browser at kangax.github.io/compat-table/es6/.
ECMAScript is the international standard name and specification used for the JavaScript
language, so it’s not a new language but a standard that is set for JavaScript, JScript, and other
implementations of the language. For more on ECMAScript, see www.ecma-international.org/
publications/standards/Ecma-262.htm.
At the time of this writing, the browsers recommended earlier in this chapter should support
at least ECMAScript 7. Node.js uses the V8 JavaScript engine, and the latest version of Node.js
should also support at least ECMAScript 7.

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


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

6 JavaScript: A Beginner’s Guide

Remember, It’s Not Java


JavaScript and Java are two different languages. Java is a programming language that must be
compiled (running a program through software that converts the higher-level code to machine
language) before a program can be executed. More information on the Java language can be
found at docs.oracle.com/javase/tutorial/.

Similarities to Other Languages


JavaScript does have similarities to other programming and scripting languages. If you
have experience with Java, C++, or C, you’ll notice some similarities in the syntax, which
may help you to learn more quickly. Because it’s a scripting language, JavaScript also has
similarities to languages like PHP—which can also be run through an interpreter rather than
being compiled.
If you have programming or scripting experience in any language, it will make learning
JavaScript easier—but it isn’t required.

Client-Side and Server-Side Programming


The addition of Node.js allows JavaScript to be run on the server side in addition to its traditional
space on the client side. Learning a little about these different environments will help you to
understand the type of programming that will need to be done when working on the client side
versus working on the server side.
A client-side language is run directly through the client being used by the viewer. In the
case of client-side JavaScript, the client is typically a Web browser. Therefore, client-side
JavaScript is run directly in the Web browser and doesn’t need to handle any requests on a Web
server. The limitation is that client-side JavaScript cannot directly save information (though it
can send information to a server-side program to do so).

NOTE
Using the XMLHttpRequest object and the Fetch API allows JavaScript to send and
request data from the server. These will be covered briefly in Chapter 14.

A client-side language is useful for tasks that deal with the content of a document or that
allow information to be validated before it is sent to a server-side program or script. For instance,
JavaScript can change the content of one or more elements on a Web page when the user clicks a
link or presses a button (many other user actions can also be activated).
JavaScript can also be used to check the information entered into a form before the form
is sent to a server-side program to be processed. This information check can prevent strain
on the Web server by not allowing submissions with inaccurate or incomplete information.
Rather than the program running on the server until the information is correct, that data can
be sent to the server just once with correct information. This also benefits the user, since
client-side validation can provide feedback much more quickly to allow the user to make any
corrections.

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


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

Chapter 1: Introduction to JavaScript 7

NOTE
While client-side JavaScript is able to help validate information sent to the server,
it cannot replace server-side validation since users may have JavaScript disabled
or unavailable in the device being used (which allows them to bypass the client-
side validation). For security reasons, you should always use server-side validation,
regardless of whether or not you incorporate client-side validation.

For client-side JavaScript, the Document Object Model (DOM) is provided so that you can
access the different elements in a document. This is typically accessible within a browser for
HTML documents.
A server-side language runs on a server. For example, a server-side language can be used
to receive information from the user of a Web browser and then take an action using that
information. The server-side program can send information back to the Web browser, save it
to a database, or do any number of other things that the Web browser cannot do on its own.
However, a server-side language is likely to be limited in its ability to deal with special
features of the client that can be accessed with a client-side language (like the width of the
browser window or the contents of a form before it’s submitted to the server).
Traditionally, server-side programming was done in languages other than JavaScript,
such as PHP, Perl, Python, Ruby, Java, and a number of others. When Node.js came along, it
allowed the opportunity to use JavaScript on the server side in addition to the client side.
Depending on the needs of you or your company, you may or may not use Node.js, but this
book will cover it in order to provide a more complete introduction to the JavaScript language
and the different environments in which JavaScript can be used.

Ask the Expert


Q: You mentioned that I could use a text editor or HTML editor of my choice, but I’m
not quite sure what that means. What is a text editor and where can I find one?
A: A text editor is a program that you can use to save and edit written text. Text editors range
from simple to complex, and a number of choices are available: Notepad, WordPad, and
Simple Text, to name a few. You can also purchase and download some from the Web, like
NoteTab or TextPad.
An HTML editor is either a more complex text editor or an editor that allows you to
add code by clicking buttons or by other means—often called a What You See Is What You
Get (WYSIWYG) editor.
For the purposes of JavaScript coding, you may decide to use a more code-oriented
program that can offer features such as code highlighting, completion, debugging tools, and
more, such as Visual Studio or Sublime. I recommend one of these tools since they offer
more features to assist with programming.
(continued)

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


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

8 JavaScript: A Beginner’s Guide

Q: What exactly do I need to know about using a text editor?


A: Basically, you only need to know how to type plain text into the editor, save the file with
an .html, .css, or .js extension, and be able to open it again and edit it if necessary. Special
features aren’t needed because HTML, CSS, and JavaScript files are made up of plain text,
but the features of coding tools like Visual Studio and Sublime can be extremely helpful as
you write HTML, CSS, and JavaScript code.

Q: What do I need to know about using a browser?


A: All you absolutely need to know is how to open a local HTML file on your computer (or on
the Web) and how to reload a page. If you don’t know how to open an HTML file from your
own computer, open your browser and go to the address bar. Type in file:///C:/ and press
enter. If you are using a drive letter other than C, type that letter instead of C in the example.
The browser will display files and folders from the drive and allow you to navigate to the file
you want to open. Click an HTML file to open it. The following illustration shows how this
might look after navigating into a few folders from the C drive using Google Chrome:

Q: Where do I get those browsers you mentioned?


A: Here are links for the browsers:
●● Google Chrome www.google.com/chrome/
●● Mozilla Firefox www.mozilla.com/firefox
●● Microsoft Edge https://www.microsoft.com/en-us/windows/microsoft-edge

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


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

Chapter 1: Introduction to JavaScript 9

Beginning with JavaScript


JavaScript came about as a joint effort between Netscape Communications Corporation and Sun
Microsystems, Inc. The news release of the new language was issued on December 4, 1995,
back when Netscape Navigator 2.0 was still in its beta version. JavaScript version 1.0 became
available with the new browser. (Before its release as JavaScript, it was called LiveScript.)
JavaScript is a prototype-based interpreted language that can be used in numerous
environments. To expand on this definition, let’s look at its important parts one by one.

Prototype-Based
Prototype-based means that JavaScript is an object-oriented programming language that can use
items called objects. However, the objects are not class-based, so no distinction is made between
a class and an instance; instead, objects inherit from other objects via the prototype property.
JavaScript has made changes in ES6 to allow you to use the class keyword, but the language is
not technically class-based. You’ll learn how to work with JavaScript objects in Chapter 10. You
don’t need to understand them in detail until you know a few other features of the language.

Interpreted Language
An interpreted language doesn’t require a program to be compiled before it is run. All the
interpretation is done on-the-fly by the client. The client, such as a Web browser or the Node.js
environment, is what is being used to interpret the language.
With a compiled programming language, before you can run a program you have written,
you must compile it using a special compiler to be sure there are no syntax errors. With a
scripting language, the code is interpreted as it is loaded in the client. Thus, you can test the
results of your code more quickly. However, errors won’t be caught before the script is run and
could cause problems with the client if it can’t handle the errors well. In the case of JavaScript,
the error handling is up to the client being used by the viewer.

Numerous Environments
JavaScript can be run in numerous environments. Most commonly, the environment is a Web
browser since JavaScript has basically become the default scripting language used when
adding dynamic interactivity to a Web page.
While JavaScript is often thought of in relation to Web browsers, a number of other
environments use it for programming, such as Adobe Acrobat and Flash (based on ECMAScript).
In addition to this, Node.js allows JavaScript to be used as a server-side language or to perform
input/output operations on an operating system. This book will cover JavaScript in Node.js
and in the Web browser at a beginner level to help you get started with programming in both
environments.

Putting It All Together


To begin, you might wonder how JavaScript is run in a browser. Where should you write your
JavaScript code, and what tells the browser it is different from anything else on a Web page?
The answers are general for now, but the next chapter provides more details.

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


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

10 JavaScript: A Beginner’s Guide

JavaScript runs in the browser by being added into an existing HTML document (either
directly or by referring to an external script file). You can add special tags and commands to
the HTML code that will tell the browser that it needs to run a script. When the browser sees
these special tags, it interprets the JavaScript commands and will do what you have directed
it to do with your code. Thus, by simply editing an HTML document, you can begin using
JavaScript on your Web pages and see the results.
For example, the following code adds some JavaScript to an HTML file that writes some
text onto the Web page. Notice the addition of <script> and </script> tags. The code within
them is JavaScript.
<html> This tag tells the browser
<body> that JavaScript follows
<script> This line writes the
document.write("This writes text to the page"); text inside the quote
</script> marks on the page
</body> This line tells the browser that
</html> this is the end of the script

The next chapter looks at how to add JavaScript in an HTML file by using the <script> and
</script> HTML tags. This will be your first step on the road to becoming a JavaScript coder!

Online Resources
To find additional information online to help you with JavaScript, here are some useful resources:

●● Projects/code for this book: github.com/JohnPollock/JSABG-ED-5


●● An excellent tutorial site that includes cut-and-paste scripts: www.javascriptkit.com
●● A place where you can address questions about JavaScript to fellow coders: stackoverflow
.com/questions/tagged/javascript

Try This 1-1 Use JavaScript to Write Text


pr1_1.html This project shows you JavaScript in action by loading an HTML document in
your browser. The script writes a line of text in the browser using JavaScript.

Step by Step
1. Copy and paste the code shown here into your text editor:
<html>
<body>
<script>
document.write("This text was written with JavaScript!");
</script>
</body>
</html>

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


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

Chapter 1: Introduction to JavaScript 11

2. Save the file as pr1_1.html and open it in your Web browser. You should see a single line
of text that was written with JavaScript. (To open a file in your Web browser, go to the File
menu and look for an option that says something like Open, or Open File, and select it. You
should be able to browse for the file you want to open as you would with other programs.)

Try This Summary


In this project, you copied and pasted a section of code into a text editor and saved the file.
When you opened the saved file in your Web browser, a line of text was displayed in the
browser. This text was written in the browser window using JavaScript. You will see more
about how this type of script works in Chapter 2.


Chapter 1 Self Test
1. You must know which of the following to be able to use JavaScript in a Web browser?

A. Perl

B. C++

C. HTML

D. SGML

2. Which of the following is something you should have to use JavaScript in a Web page?

A. A Web browser

B. A C++ compiler

C. A 500GB hard drive

D. A DVD-RW drive

3. The choice of a Web browser is up to you, as long it’s compatible with __________.

A. Flash

B. VBScript

C. JavaScript

D. Windows XP

4. JavaScript and Java are the same language.

A. True

B. False

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


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

12 JavaScript: A Beginner’s Guide

5. __________ is the international standard name and specification used for the JavaScript
language.
A. JScript

B. LiveScript

C. ECMAScript

D. ActionScript
6. JavaScript has similarities to other programming and scripting languages.

A. True

B. False

7. Before its release as JavaScript, JavaScript was called __________.

A. Java

B. JavaCup

C. LiveScript

D. EasyScript

8. JavaScript is __________.

A. prototype-based

B. class-based

C. object deficient

D. not a language that uses objects

9. A fancy code editor is necessary to edit JavaScript files.

A. True

B. False

10. In what ways can a fancy code editor help a JavaScript programmer?

A. It may offer features such as movies and video games.

B. It may offer features such as code highlighting, completion, debugging tools, and more.

C. It may offer features such as electronics repairs and warranties.

D. It can’t help at all.

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


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

Chapter 1: Introduction to JavaScript 13

11. A(n) __________ language doesn’t require a program to be compiled before it is run.

A. programming

B. server-side

C. interpreted

D. computer

12. JavaScript can only be used in a Web browser environment.


A. True

B. False

13. In JavaScript, what handles errors in a script?

A. The operating system

B. A compiler

C. A program on the Web server

D. The client

14. How is JavaScript added to a Web page?

A. It isn’t. It must be compiled and loaded separately.

B. It is taken from a compiled program on the server.

C. You place the code in a file by itself and open that file.

D. It is added to an HTML document.

15. What is added to a Web page to insert JavaScript code?

A. <script> and </script> HTML tags

B. The JavaScript code word

C. <javascript> and </javascript> HTML tags

D. <java> and </java> HTML tags

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


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

This page intentionally left blank

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


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

Chapter 2
Placing JavaScript
in an HTML File

15

02-ch02.indd 15 27/09/19 10:05


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

16 JavaScript: A Beginner’s Guide

Key Skills & Concepts


● Using the HTML Script Tags
● Creating Your First Script
● Using External JavaScript Files
● Using JavaScript Comments

N ow that you have been introduced to JavaScript, you’re ready to start coding. This chapter
introduces front-end (browser-based) JavaScript, while Chapter 3 will introduce how
JavaScript can be used with Node.js.
Since front-end JavaScript code is included in HTML documents, you need to know how
to tell Web browsers to run your scripts. The most common way to set off a script is to use the
HTML <script> and </script> tags in your document. You can place your script tags in either
the head or the body section of an HTML document.
This chapter first shows you how to use the script tags to begin and end a segment of
JavaScript code. Then, you will get started creating and running your first scripts. At the end
of the chapter, you will learn how to add JavaScript comments to document your scripts.

Using the HTML Script Tags


Script tags are used to tell the browser where code for a scripting language will begin and end
in an HTML document. In their most basic form, script tags appear just like any other set of
HTML tags:
<script> Tells the browser where
script code begins
JavaScript code here
</script> Tells the browser where
script code ends

As you can see, there is the opening <script> tag, the JavaScript code, and then the closing
</script> tag. When you use just the basic opening and closing tags like this, almost all browsers
will assume that the scripting language to follow will be JavaScript.
In HTML, the script tag is not case sensitive. However, in XHTML, the script tag must be
in lowercase. JavaScript is case sensitive in all versions, so you will need to be more careful
with it. In this book, I will use HTML5 for the HTML code (even though HTML5 is not case
sensitive, I will write the tag and attribute names in lowercase). For the JavaScript code, I will
use the case that is needed for it to function correctly.
The <script> tag has six possible attributes: type, language (deprecated), charset, src, defer,
and async. These attributes give the browser additional information about when the script should
load, the scripting language, and the location of an external JavaScript file (if any).

02-ch02.indd 16 27/09/19 10:05


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

Chapter 2: Placing JavaScript in an HTML File 17

Identifying the Scripting Language


The scripting language between the opening and closing script tags could be JavaScript,
VBScript, or some other language, though JavaScript is almost always set as the default scripting
language in browsers. If desired, you can explicitly identify JavaScript as the scripting language
by adding the type attribute with the value of “text/javascript” to the opening script tag:
<script type="text/javascript"> Tells the browser the scripting
language will be JavaScript
JavaScript code here
</script>

NOTE
The type attribute in the opening script tag is required in XHTML in order for the Web
page to validate, but is optional in HTML.

In the past, the language attribute was used to identify the scripting language, but is ignored
in modern browsers and will cause the page to be invalid in XHTML and HTML5. It should no
longer be used.
The charset attribute, which allows for the character set of the JavaScript code to be specified,
is not recognized by most browsers and is not recommended.
You will see additional important attributes discussed in the following sections.

Calling External Scripts


Script tags allow you to call an external JavaScript file in your document. An external JavaScript
file is a text file that contains nothing but JavaScript code, and it is saved with the .js file extension.
By calling an external file, you can save the time of coding or copying a long script into each page
in which the script is needed. Instead, you can use a single line on each page that points to the
JavaScript file with all of the code.
You can call external scripts by adding the src (source) attribute to the opening script tag:
<script src="yourfile.js"></script>

This example calls a JavaScript file named yourfile.js from any HTML document in which
you place this tag. Be sure there are no spaces or code between the opening and closing script
tags, as this may cause the script call to fail.
If the script is extremely long, using the src attribute to add the script to multiple pages can
be much quicker than inserting the entire code on each page. Also, the browser will cache the
external JavaScript file the first time it is loaded, making subsequent Web pages that use the
script render faster. Using an external script is also helpful when dealing with page validation
and when trying to keep script code separated from markup (HTML) code. In addition, the src
attribute allows you to include other people’s script files as libraries in the very same way.
By default, script files are loaded in the order in which they are placed in the HTML code
(synchronously). There are some options for altering this behavior, which are described in the
next section.

02-ch02.indd 17 27/09/19 10:05


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

18 JavaScript: A Beginner’s Guide

Specifying when the Script Should Load


The last two attributes, defer and async, allow you to specify when an external script should be
loaded. These attributes are not fully supported by older browsers, or may behave differently,
so be aware that an older browser may not execute the script when it is expected to do so.

The defer Attribute


The defer attribute allows you to specify that an external script file should be loaded, but should
not execute until the page has completed parsing (the </html> tag has loaded). The following
<script> tag would defer the execution of the external JavaScript code:
<script src="file.js" defer></script>

NOTE
If you are using XHTML, set this attribute using defer=“defer”.

Support for this attribute is available in Internet Explorer 4+, Firefox 3.5+, and Chrome 7+.
Internet Explorer 4–7 will allow this attribute to work on inline scripts as well, but versions 8
and above only support this attribute on external scripts as other browsers do.

The async Attribute


When the async attribute is set, the page can continue to load without waiting for the script to
load, and the script will execute after it completely loads. Here is an example:
<script src="file.js" async></script>

NOTE
If you are using XHTML, set this attribute using async="async".

Support for this attribute is available in Firefox 3.5+ and Chrome 7+.

Using <noscript></noscript> Tags


One way of providing alternate content for those viewers without JavaScript (or with JavaScript
turned off) is to use the noscript tag. The <noscript></noscript> tags may be placed anywhere
in the HTML document and can contain any content needed for those viewers browsing without
JavaScript. For example:
<script>
Displays for those viewers
document.write("The color is red."); with JavaScript
</script>
<noscript> Begins noscript content for those
viewers without JavaScript
The color is red.
</noscript> Ends noscript content

This example displays the phrase “The color is red.” to the viewer either through JavaScript
or through the text within the <noscript></noscript> tags.

02-ch02.indd 18 27/09/19 10:05


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

Chapter 2: Placing JavaScript in an HTML File 19

CAUTION
Some older browsers may not handle the noscript tag correctly and won’t display the
content in either section. If your users have older browsers, another alternative is to
display the content on the page and then use JavaScript to enhance the content for
those who are able to display it with JavaScript on.

The <noscript> tag can be useful at times, but there are often better ways to provide the
same content to those without JavaScript (avoiding the document.write() method, for instance).
You will learn more about accessible JavaScript as you progress through this book.

Ask the Expert


Q: Do I always need to use script tags to add JavaScript to a page?
A: It’s possible to use event handlers that allow you to write short bits of script within the
event-handling attribute of an HTML tag. You’ll learn about event handlers in Chapter 13.

Q: What about the language attribute?


A: The language attribute once was used to specify the scripting language to be used,
and for a time some browsers allowed a JavaScript version to be specified (for
example, language=“JavaScript1.2”). This is no longer the case, and the attribute has been
deprecated. Since it is completely ignored in modern browsers and causes pages not to
validate in XHTML and HTML5, it should no longer be used.

Q: My page won’t validate in XHTML strict (or transitional) when I add a script to it.
How do I get the page to validate?
A: If the script contains characters used in XHTML such as < (which is used for “less than”
in JavaScript but is seen as the beginning of a new tag in XHTML), then the page won’t
validate with the script directly in the document without adding a CDATA section:
<script>
<![CDATA[ Begins the CDATA section
var x = 5;
var y = 10;
if (x < y) {
window.alert("x is less than y");
}
]]> Ends the CDATA section
</script>
(continued)

02-ch02.indd 19 27/09/19 10:05


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

20 JavaScript: A Beginner’s Guide

This will allow the page to validate, but because the <![CDATA[ and ]]> characters
are in the script, the script will no longer work. To fix this, you need JavaScript comments
(/* and */) around those characters when they are within the script tags:
<script>
Opening and closing JavaScript comments
/*<![CDATA[*/ are placed around <![CDATA[
var x = 5;
var y = 10;
if (x < y) {
window.alert("x is less than y");
}
Opening and closing JavaScript
/*]]>*/ comments are placed around ]]>
</script>

As you can see, this can get quite tedious very quickly! Typically, the better option is to
use an external script file if you are using XHTML, which eliminates this problem because
only the script tags themselves are needed in the XHTML document.

Creating Your First Script


Now that you know how to use the HTML script tags to tell browsers about the JavaScript in
a document, you’re ready to learn how to add the actual JavaScript code between those script
tags. The first coding example often given to teach any language is one that writes some sort
of text to the default output area, commonly known as a basic “Hello World” script. Following
that convention, your first script will write a string of text to a Web page.

Writing a “Hello World” Script


Rather than write “Hello World,” you’ll use another line of text for this script: “Yes! I am now a
JavaScript coder!” This requires only a single line of code, using the document.write() method,
which writes a string of text to the document:
<script>
document.write("Yes! I am now a JavaScript coder!");
</script>

Notice the parentheses and the quotation marks around the text. The parentheses are required
because the document.write() method is a JavaScript function, which takes an argument contained
in parentheses. You will learn more about JavaScript functions in Chapter 5.
The quotation marks denote a string of text. A string is a data type defined in JavaScript
by placing the value inside quotation marks. Chapter 4 provides details on strings and other
JavaScript data types.
The last thing to notice about your script is the semicolon at the end of the line. The
semicolon signals the end of a JavaScript statement. A statement is a portion of code that does
not need anything added to it to be complete in its syntax (its form and order). A statement can
be used to perform a single task, to perform multiple tasks, or to make calls to other parts of

02-ch02.indd 20 27/09/19 10:05


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

Chapter 2: Placing JavaScript in an HTML File 21

the script that perform several statements. Most JavaScript statements end with a semicolon, so
it is a good idea to get in the habit of remembering to add one.

NOTE
In later chapters, you will see various lines that do not end in semicolons because
they open or close a block of code. Also, many scripts you encounter may not end
statements with semicolons. JavaScript is lenient about the use of a semicolon in most
cases; however, it is best to use the semicolon to end a statement because it can prevent
possible errors and aid in debugging (removing errors from) the script later.

So, to write a text string to the page, you use the document.write() method, followed by the
text, surrounded by quotation marks and enclosed in parentheses. End the line (the statement)
with a semicolon. JavaScript will handle the rest of the job.

Creating an HTML Document for the Script


In order to make this example complete and test the script, you need to insert it into an HTML
document. First, create the following HTML document with the basic tags (using any text
editor you prefer):
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
Save the document as test1.html in your text editor. You will open it later with a Web
browser to see the results of the script. Next, you’ll add your JavaScript code to this HTML
document, so leave the file open.

Inserting the Script into the HTML Document


Now you need to insert the script in the document. Where should it go? You can place a script
between the <head> and </head> tags, or between the <body> and </body> tags. Since this
example writes a text string directly to the page, you want to insert the script between the
<body> and </body> tags, wherever you want the text string to appear. It can come before,
after, or between any HTML code on the page.
To make it clear how the script results appear, you’ll add HTML code to write lines of text
before and after the script. The script tags and the script itself are inserted between those lines.
Add the lines shown next between the <body> and </body> tags:
<p>This is the first line, before the script results.</p>
<p>
<script>
document.write("Yes! I am now a JavaScript coder!");
</script>
</p>
<p>This line comes after the script.</p>

02-ch02.indd 21 27/09/19 10:05


Exploring the Variety of Random
Documents with Different Content
the other.
“I’ve never heard him complain of any kind of illness,” she heard the
landlady say in a low voice. “He never gave one the slightest reason
to suspect there was anything wrong.”
“How long has he lived here?” the doctor asked.
“He has occupied this room for more than twenty years.”
“An actor, I think you said?”
“Oh, yes. Mr. Falkland Vavasour, quite a celebrated actor.”
“I don’t seem to remember the name. No doubt he belongs to a
bygone generation.”
“He was a very distinguished man.”
“Where did he get his meals?”
“His meals?” The voice of the landlady grew a little vague. “Of late
years he always took his meals out.”
“Can you tell me where?”
“At one of his smart clubs in the West End, I believe.”
“Which one in particular did he frequent? Can you tell me?”
Mrs. Toogood, unfortunately, could not. But she understood that he
had been a member of several.
“Do I understand you to say, ma’am,” said the doctor, gently
releasing the hand of the old man, “that Mr. Falkland Vavasour never
took any food in this house?”
“When he came here first,” said the landlady, “he was usually in to
all his meals. Then he gave up having dinner in the evening because
of his digestion. After that he took to having his luncheon out. And
for the last year, for some reason or other—he was always a bit
faddy and peculiar in his ways—he used to go out for his breakfast.
But as he had been here so long and he was a sort of connexion of
my late husband’s—I don’t quite know what the relationship was but
my husband was always proud of him—I allowed him to keep on his
room.”
“It was duly paid for, I presume?”
“Always, punctually, until about three weeks ago. When he got
behind it seemed to trouble him a good deal, but I told him not to
worry.”
“Well, I am sorry to have to tell you that there will have to be a post
mortem. Mr. Falkland Vavasour has all the appearance of having died
of starvation.”
Mame waited to hear no more. She was deeply grieved. And she
was rather shocked. Yet she was not so shocked as she would have
been had not her swift mind leaped forward to the doctor’s verdict,
even before the worthy man had arrived to give it. Yes, the grisly
truth was plain for any who had eyes to see. Grand seigneur to the
end, too proud to eat a crust he could not pay for, his only means of
livelihood vanished long ago, he had passed out as he had lived, a
prince among four-flushers.
Upstairs, in the privacy of her dismal room, Mame wept. Something
had gone from Fotheringay House, something that could never
return. Among all the millions of people seething around, this dear
old man had been her only friend.
Shivering on the edge of her bed in that chill attic, she felt horribly
lonely now. Nostalgia came upon her, a longing for home. She did
not understand these people. A powerful craving for the hearty,
simple folks she knew and loved crept over her while she fought to
control her tears.
X
SUCH havoc had been played in a short time with Mame’s cash
balance by the life of London, England, that already the margin of
safety was nearly reached. By the end of that week—it was Tuesday
now—she would be compelled to take a bus to Cockspur Street and
see about a passage home.
The thought was not pleasant. A second failure, if hardly so painful
as the one in New York, was even more dire. For by the time she
looked again on the Statue of Liberty very nearly all the munitions of
war would have vanished. And what would remain to show?
Mame needed all her grit to bear up. The tragic end of Mr. Falkland
Vavasour was the writing on the wall. Did it not prove how fatally
easy it was for people, even of a certain position, to fall out of the
ranks?
The clouds were gathering. Since landing in England she had not
earned a dime. She had called on many editors and found them
inaccessible; she had mailed them her stuff; but there was nothing
doing. Her style was not what they were used to; and this nation of
snails prided itself on being conservative. But the worst blow of all
was the silence of Elmer P. Dobree.
Swift time flowed on. But the much-looked-for envelope, bearing the
magic postmark “Cowbarn, Iowa,” did not come. Friday by Friday,
with the super-optimism her high-spirited countrywomen have
elevated to a religion, the dauntless Mame mailed two columns to
the editor of the Cowbarn Independent. Each week as she registered
the packet and slipped the chit into her handbag she was as sure
that Elmer P. would fall as she was convinced she could turn off that
sort of junk until the cows came home. The stuff was good. Even a
simp with half an eye could see that. Not highbrow, but better; the
newspapers wanted things homely and plain. And there was any
amount of pep in it. Every word was hot from the mint of
experience.
All the same the weeks went and not so much as a line of
acknowledgment came from the man on whose friendship she had
counted. This silence was mysterious and exasperating. However,
she would not let herself be cast down. She went freely about this
comic town of London. There was more to it than at first she had
supposed. Her first impression had been of a slower New York; a
New York with a certain amount of moss on it. But when she had
spent a few weeks noting and recording the ways of this queer burg,
she began to see that it had its own standards, its own way of doing
things and that it would repay study.
She had really come to Europe to improve her knowledge of the
world. Once properly grounded in what to Mame’s mind was the first
of all sciences, she would return to New York, that city of four-
flushers which had mocked and derided her, with the ace of trumps
concealed up a fashionable sleeve.
To this end she must get around; and, if she could, contrive to see
the life of London from the inside. Among such self-contained and
stand-off folks this was not easy. That was why she had stayed on at
Fotheringay House. It was the best address she could afford. Nay, as
things went with her, it was a better address than she could afford.
But its portentous air of propriety was worth paying for. New York
had taught her that propriety, an elusive soulless thing, was
indispensable for a girl who had to play a lone hand.
She was living far beyond her means, but she managed to see just a
little of smart restaurants at luncheon and at tea time. Picture
galleries bored her, but she conscientiously did them. Culture was
always worth while; another card to keep up your sleeve. At
concerts and theatres she occupied a cheap seat; she saw all the
sights of the town. And now, at a tragic moment, came the
knowledge that she must pack up and go home.
Moving about the streets, she felt this day to be the worst she had
yet known. Even the humiliation put on her by the New York police
had not yielded a sensation of being so truly up against it. Not a
chance did there seem of making good. She would give herself till
Saturday and then arrange to quit.
For the first time since her arrival in London she perceived a touch of
spring in the air. Emboldened, she climbed on to the roof of Bus 56
and let it take her where it would. The line of route was along the
Embankment, past the Houses of Parliament, across Westminster
Bridge. Old Father Thames was lovely this morning, with a hint of
blue sky furtively peeping through soft grey mist.
As Mame looked back and saw the line of great hotels towering up
and dominating the river with their haughty façades, never to her
had they appeared so aloof, so magnetic, so inaccessible. Her desire
had been to storm those cosmopolitan portals, but moving now
towards the humbler purlieus of the southeast, she could not help
reflecting bitterly how ill-founded was that ambition.
Still she was a fighter. The trick was in her blood. And never had the
sense of her inheritance been more insurgent than on the top of Bus
56 this rare March morning, when for Mame Durrance the bottom
seemed out of things. She could not bear the thought of giving in.
Memories of great-uncle Nel rose in her heart. She could remember
that fine old warrior having said that when things looked blackest for
the Union in the Civil War, his chief, the famous General Sherman,
had declared, “If only we can stick it the clouds will lift.”
These words, of late, had been much with her. Since she had started
out to see the world and had known what it was to lie worried,
sleepless, heavy eyed, in an airless attic, she had often recalled just
how great-uncle Nel turned a thin but strong cigar over in his teeth
as he made his contribution to history.
Yes, all came back to the power of sticking it. As Bus 56 trundled on,
Mame kept repeating to herself that memorable phrase. If only she
could stick it! That odd faculty was the measure of her worth, as it
had been that of folks whose shoes she was not fit to tie.
XI
AS luck would have it, Bus 56 came at last to a stop opposite a
cinema in Camberwell Green. The posters outside were featuring the
fratricidal conflict in which great-uncle Nel had borne a part. Indeed,
there was a certain quaint old-timer who had a poster all to himself
immediately under the booking office window who was General
Sherman’s colleague to the very life. At the sight of this warrior
something thrilled in Mame. She was not superstitious and she
always made a point of believing as little as possible of what one
had no means of proving; but that picture went some way to
convince her that at this moment the occult was putting one over on
her.
Promptly she got off the bus and made for the booking office. But a
notice under the window said it was not open until two o’clock and
that the show did not begin until half an hour later. As yet it was
barely one o’clock, so there was nothing for it but to kill the time.
Mame took a walk up Denmark Hill. It was not a very inspiring
altitude. Nor did a glass of milk and a bath bun at a dairyman’s near
the tram terminus at East Dulwich station do much to raise her
spirits. Never had she felt so intensely that she was nearing a crisis.
Back again at Camberwell Green she entered the cinema just as the
orchestra was tuning up. It was an orchestra of two, a fiddle and a
piano, and it seemed to add to her depression. Out of deference to
the film, which was entitled “Scenes from the Great Civil War,” the
fiddle and the piano discoursed those melodies with which Mame’s
childhood had been most familiar.
They began with “Suwanee River” and kindred themes of de ole
plantation and went on to “John Brown’s Body” and “The Battle
Hymn of the Republic.” Mame soon wished that she had stayed
outside. With all respect to great-uncle Nel she was not in a mood to
enjoy this réchauffé of her youth. For she could not forget that her
youth had been hard and unhappy.
In the first place she had never known a mother’s love. At her birth
her father had been left a widower. But when Mame, an only child,
was five years old, he married a hard-natured, unsympathetic
woman. Good had come, indirectly, of the stepmother’s rule. It had
not made for joy; but those years had fanned a secret flame in
Mame’s ambitious heart. Resentment took the form of a passion for
self-improvement. With the help of the village schoolmarm, kindly
Miss Jenkins, she studied so hard in the hours when minds less
nimble were asleep, that on her eighteenth birthday she was able to
fill a vacant stool in the Independent office. And on that red-letter
day, life for Mame Durrance began.
The opening scenes of the film brought back the past vividly. A
hundred details, half-forgotten, reminded her of the farm four miles
from Cowbarn, where she had been brought up. She saw again, in
the types thrown on the screen, the dour, lean, Middle Western
farmer, her father. The sight of him was an intolerably painful
memory. An embittered, unsuccessful man, who in his later days had
often drunk more whiskey than was good for him, in Mame’s
recollection, he had never been happy in his work or in his home. He
had been years in his grave, yet time, the healer, did not allow his
daughter to feel affection for him. Still perhaps she had a little pity.
He was one of life’s miss-fires. Groping along from year to year in
the old rut, without vision, without initiative, a weak man rather than
a bad one, his sins mainly were the sins of omission. And the worst
of them, in the eyes of his child who had paid for it, was that he had
not been man enough to stand up to the selfish vixen he had taken
for his second wife.
It was no use pretending that the film’s poignant reminders of her
childhood were pleasant. The discomfort, the toil, the loneliness, all
came back to her. How had it been possible for a creature like
herself, with only a half-educated village dame to help her, to get
away from it all? That was the question now in her mind. And the
emotion aroused by these familiar scenes had little enough to do
with the heroic figure of her real mother’s Uncle Nel, although the
fine type to which he belonged was also there. Uppermost in Mame
were disgust and pity. But she had escaped. By some miracle she
had escaped. And no matter what happened to her now, she knew
that she could never go back to the drudgery and the boredom of
the place whence she came.
Memories of the past grew too painful to bear. Mame did not wait for
the battle pieces. Even great-uncle Nel’s General Sherman, who had
a picture all to himself, and the soldierly groups, in any one of whom
might be the rare old man she remembered so clearly, had not the
power to stay the panic rising in her heart. It was weak, this sense
of tumult; it was foolish and worse than foolish, it was cowardly; but
quite suddenly Mame cast all thought from her of great-uncle Nel.
She got up and fled from the cinema.
Outside, amid the dismal waste of bricks and mortar, which ironically
called itself Camberwell Green, a rather frosty March sun was
waning. Mame stood a few moments under the awning of the
cinema in a state of irresolution, not knowing what to do next. It
was as if she had become hypnotised by a sense of life’s vastness
and complexity. The world was far beyond all calculation; yet now
she felt just the meanest thing in it.
However, she caught sight of Bus 56, wheeling round to the opposite
kerb. It was about to return to Charing Cross. Mame lost no time in
climbing to a seat on the roof. Bus 56, at that moment, was the one
thing in her life that held the core and semblance of reality. All the
rest was chaos and old night. But this prosaic vehicle meant
something. Panic-stricken as Mame now was, it stood for will,
volition, force.
Yes, she was panic-stricken. It was very absurd. In the most illogical
and unexpected way, a subtle demon had sprung upon her for the
second time. The first had been in that epic moment when she had
driven in a cab to the police office in New York with the horrid
Detective Addelsee sitting by her side. But on that occasion there
had been some excuse for this feeling of dull and helpless terror. On
the present occasion there was none.
The shrewd air of the bus top revived her a bit. Her fighting spirit
began to rally. If once it deserted her she was done. Why this attack
of cold feet? There was nothing to be afraid of. She still had money
enough to get home. It would not be the Iowa farmhouse to which
she knew now she could never return. Home, for her, must be one
of the big and friendly cities of that republic of which she was proud
to be a daughter.
Big indeed were those cities. But were they so very friendly? Mame
had begun to ask herself that by the time Bus 56 had reached the
Elephant and Castle. Frankly, in her experience of them, they were
not. To a little hick, as raw as herself, New York, for example, had
been quite the reverse. Apart from Aunt Lou’s dollars, it had no use
for her. It had swallowed nine hundred of those dollars and lodged
her in jail before you could say knife. No, friendly was not quite the
word for New York.
Still, in this bleakly inhospitable island, which was gulping her dollars
just as quickly, even if it had refrained from putting her in jail, it
would not do to knock New York. It was where she belonged.
America had treated her pretty rough but it was the land she loved
and admired. She might hate her stepmother and deplore her father,
yet after all it was the home of her mother’s memory. No, in spite of
failures and bad breaks, it would not do to knock little old New York.
This sentiment, which she knew to be no more logical than the
others, was so vividly upon her by the time she left the bus at
Charing Cross, that she crossed at once to the office of the shipping
company in Cockspur Street. If a boat, by which she could afford to
travel, was leaving at the end of the week, she would book a
passage. Better say good-bye to London a week too soon, than stay
a week too long and find yourself stranded.
When, however, she reached the offices of the shipping company
she felt bound to pause before she went in. Was it wise to act so
precipitately? Why surrender to wild impulses? It was a big decision
to make on the spur of the moment. What she did now could not be
undone later. She had figured on staying another week in London.
Every day’s experience was valuable. Any day she might hear from
Elmer Dobree, telling her that her stuff was O.K., asking for more,
enforcing his demands with a cheque.
Unreasoningly as her cinema panic, an odd wave of optimism flowed
over Mame as she stood gazing into the shipping company’s window.
She had always yielded to this recurring wave that seemed to spring
from her higher nature. Had she not done so from the beginning she
would still be eating out her heart on her father’s farm. What could
have seemed more hopeless than for Mame Durrance to thirst after
Culture? Yet that craving, in the end, had taken her to the county
town, to the office of the Independent. And this simple faith in the
future had carried her to New York and finally three thousand miles
across the Atlantic as far as Europe. Was this the hour to go back on
the urge of nature?
“If only you can stick it, the clouds will lift.” She didn’t know where
the voice came from, but those familiar words sounded clear as a
bell. Yes, she must stick it. That was what life was for: to keep a stiff
upper lip; to face your luck; to go down fighting.
While she stood gazing at a model of a Cunarder in the window of
the shipping company, she was quickened by new power. Whence it
came there was no means of knowing; but just behind her was
Trafalgar Square, and the lions, and the mighty column a grateful
nation had raised to the memory of a Nelson even more remarkable
than the brother of her grandmother. Sure, it must have been from
the top of that monument the thought wave had come.
She appeared to be borne on the wings of inspiration. The time was
not yet to give in. She would stay another week. But an effort of the
will was needed to leave that too-enticing window. She crossed the
road as leisurely as the taxis and the buses would permit; yet brain
and heart were in conflict as she entered Pall Mall.
Outside the Carlton she paused. A line of smart cars was disgorging
brilliant occupants. Mame stood wistfully in the shadow of the
portico, observing, as she had done so many times in the last seven
months, the life of ease, luxury and wealth from the outside. She felt
like a peri at the gates of Paradise. If once she could gain a footing
within those charméd portals, the capacity was surely hers to enjoy
their delights.
This evening her thoughts seemed to make her desperate. Never
had the spirit of adventure burned so high. It was her duty to count
every dime, but this day, take it altogether, was the worst she had
met since landing in England. She was fed to the teeth with
disappointment and the sense of just being out of things. There had
been too much cold shoulder. But there was money still in her purse.
Before she realized what she was doing, she was mingling with the
smart mob and passing through the revolving doors. As the delicate
strains of an orchestra caught her ear, her little head went up and
she began to move more freely. She considered herself to be very
well dressed, if a little “tossed” from a series of rides on the roof of
divers plebeian buses. Even if she was down on her luck she was
free, white and twenty-one. And she could pay her shot; therefore
she had a right to show her nose among the plutes.
The large room, on whose threshold Mame found herself, without
quite knowing how she got there, seemed to be full already. Very
distinguished-looking females and equally distinguished-looking
males were standing around, in twos and threes. They were
scanning, as it were, the far horizon for vacant tables.
Vacant tables, however, there were none. It was the hour when the
theatre matinées yield up their tea-thirsty patrons. Standing room
only appeared to be the order of the moment. And truth to tell,
Mame did not feel altogether displeased. If she found a seat at one
of those seductive little tables, it would mean half a crown at the
very least. And in the present state of Wall Street half a crown was
money.
This was pusillanimity. She was out for adventure. And she really
wanted tea. Something in the much-abused British climate seems to
call for tea at five o’clock. Therefore Mame’s slim little body began to
insinuate itself nearer the cups and saucers and the elegant
confectionery; whereas bodies less slim and not so little remained
outside the sphere of their influence.
Gazing around on the crowded scene, Mame awoke to the fact that
an extremely smart-looking girl, seated alone some two tables off,
and smoking a cigarette in a long meerschaum holder, had fixed a
demure eye upon her. Some little time it had been there, but Mame
did not know that. Every detail was taken in already by a glance
candid yet wary. Clothes, hat, eyes, chin, the face of wistful emotion:
Mame was a rare butterfly with quaint markings, a new specimen for
the net of a collector. Suddenly the girl’s eye caught Mame’s. She
coolly signalled with the meerschaum holder that there was room at
her table.
As Mame moved towards it she was ready to believe, such was this
smart girl’s easy air, that she had been mistaken for one of her
friends. Mame felt that she must bear a likeness to somebody else.
But no, this was not the case. The girl at once began to treat her
choice “find” with the off-hand courtesy which seemed to be her
attitude towards the world at large.
She lifted a muff, a real sable affair, from a seat near by in order to
free a chair. As Mame subsided into it with her politest thanks, the
girl looked at her shrewdly and then said in a casual voice, “You
want a waiter.”
Before Mame could take steps to get a waiter, her new friend, who
was full of cheery competence, had attracted one. Her manner of
doing so was in nowise aggressive, yet it was quite successful. The
last word in waiters, all smiles and all ears, soon materialised at
Mame’s elbows.
“I can recommend the crumpets. They’re very good to-day.” The girl
followed her genial information with something in Italian or French
to the waiter which Mame did not understand. It was probably
Italian, for the waiter was an undoubted Wop. He crisply brushed
the tablecloth with his napkin, arranged cup and saucer, knife and
plate upon it, and then went smilingly off to execute Mame’s order.
“Some folks around,” said Mame conversationally.
“A regular beehive.” The girl had a slow, deep smile which at the
sound of Mame’s voice began to grow.
“All the old-timers, I’ll say, from way back.”
At that remark the girl laughed outright, but in a way that was
friendly. Mame felt encouraged to let her tongue run.
“Say, listen, who is the dame with the auburn wig and the Roman
nose?”
“Ah, you mean the old dreadnought.” The meerschaum holder
tactfully indicated the next table but one where the personage in
question sat in state. “Eighty-five if an hour. Blind as a bat, deaf as a
mole, but worth looking at, I always think.”
Mame’s laugh chimed with the girl’s. The old dreadnought, in a
Victorian bonnet and mantle, with a nose standing off from a craggy
face like a handle from a door, was a type. Mame was so much
interested that she repeated her question.
“Old Duchess Hattie,” said the girl lightly. “Everybody in England
knows her. Among other things she’s my godmother.”
“Oh!” said Mame. Warily and at once she withdrew her gaze from
the ancient duchess to this new friend who claimed her for a
godmother. Involuntarily her fingers clutched her vanity bag to make
sure it was still on her wrist. London as well as New York had its
four-flushers. Mame looked at the girl opposite with a new curiosity.
Was she the real thing? Or was she merely putting one over on an
obvious simp? Certainly she was smart. And if not exactly a looker,
she had heaps of style. Besides she had these high-grade waiters
feeding from the hand. The Wop had already interrupted these deep
reflections with Bohea in a china pot and crumpets fairly sizzling in
butter.
Followed more conversation in Italian. The girl then fitted an
eyeglass, very neat and inconspicuous, into her right eye and
glanced at the programme of music. “Don’t you think we might have
the Rosencavalier instead of this thing of Massenet’s for number
seven?” She looked at Mame. But Mame, out of her depth, merely
looked at the waiter. “Yes, I think so.” The girl provided the answer
for herself. “Give my compliments to M’sieu.” She turned quietly to
the Wop as if she owned him and continued her speech in Italian.
Virgilio bowed gracefully and made his way up the room towards the
band.
Mame, under cover of a bold attack on a crumpet, furtively watched
her new friend. She was puzzled and fascinated by her. This bird was
something new. Her clothes were of the best yet they were not
startling. Even her eyeglass and her meerschaum cigarette holder,
remarkable in anyone else, did not seem out of the picture. Her talk
was lively and clever; her attitude towards that world which ordinary
people only read about in the newspapers was one of an amused
familiarity; yet her manners were neither boastful nor loud. If four-
flusher she was, and Mame felt she must be, it was a more subtle
breed than any which had crossed her path up to now.
For the pleasure of drawing the girl out and perhaps in the hope that
she would give herself away in a handful large enough to set all
doubts at rest, Mame tentatively said over the edge of a teacup,
while marking the new acquaintance very closely indeed: “I s’pose
you know all the folks.”
“More or less.”
Somehow it was not the answer Mame expected. A real four-flusher
would have posed a bit in making it. She would have struck
something of an attitude, and tried to look like an oil painting of a
First Family. But this girl didn’t. Paula Wyse Ling, who had spent two
whole years studying European society and was now beginning to
get her stuff into some of the best journals in America, would never
have answered such a question in that casual style. Paula would
have preened her feathers and with her voice right up would have
looked down her long nose and said: “Oh, yes, I have had the
privilege of meeting some quite good people.”
Suddenly Mame’s eye lit on one other appurtenance of this new
friend which hitherto had escaped it. Peeping in the oddest way out
of a fashionable sleeve was the tiniest imaginable Pekingese. The
sight of the quaint creature was so unexpected and its air of
dignified aloofness so entirely charming that Mame could not repress
her delight.
“Ain’t he just cute!” She proceeded to offer sugar.
The small beast gazed haughtily at Mame. And then disdaining the
sugar in a most aloof manner, retired at least six inches further into
the sleeve of his mistress.
“Rather nice, isn’t he? But always apt to be stiff and formal unless
he feels he’s been properly introduced. You see he’s a Chinese
emperor’s sleeve-dog and his pedigree goes right back with a click to
the First Ming Dynasty.”
“What’s his name?” asked Mame partly for the sake of conversation,
partly to show that she was impressed.
“Fu Ching Wei. He was given me by the Emperor of Manchuria when
I attended his coronation last year at Mukden.”
In the opinion of Mame this was overdoing it. This girl was certainly
trying to put one over on her. And Mame had already come to like
her so much, although to be sure she had only known her five
minutes, that she felt sorry. If one must pull that sort of guff, one
might at least take pains and do it with art. Among “all the folks”
whom Mame had supposed this girl knew, emperors had not been
included.
XII
“YOU ain’t a newspaper girl, I’ll say?” Mame opened cautiously.
“Yes.” The new acquaintance replenished casually the meerschaum
holder.
She wrote for the papers. It was by way of being a solution of the
mystery. What these Britishers called a journalist. But a four-flusher
all the same. Yet Mame could not help liking her. There was
something so forthcoming, something so unstudied. She was so
much more natural than Paula Ling. You felt with Paula that if you
knew her a hundred years she would never let you catch her with
her hair down or without her pinko. But this girl was different.
“What journals you write for?”
“For a syndicate mostly.”
“A syndicate.” Mame blinked. Her strong financial instinct
automatically got busy. “Then you pull the big stuff, I guess?”
“Bread and butter.” As the bloated pluralist spoke she took a piece
from the plate in front of her and offered it delicately to Fu Ching
Wei.
The haughty animal suspiciously curled a lip and then condescended
to eat. “Nice, isn’t he?” His mistress tickled gently the top of his
head.
“Describe coronations for Reuter’s Agency?” Mame threw out a
feeler. The subject fascinated her. And though the mistress of Fu
Ching Wei might be a palpable bluffer, there was still a chance that
she was one of the mandarins of the profession into which Mame
herself was dying to force an entrance.
Awe was in Mame’s voice as she asked the question. Awe there was
none in the careless voice that answered it. “Describe any old
toomarsh from a dog fight to a royal marriage. Not that one does
those stunts often, although one gets about the world sometimes.”
“What’s your line, then?” Mame tried hard to mask her curiosity. But
rather conspicuously she failed.
“As a rule I write up the tea shops and hat shops and the
restaurants and the big stores. And I do the books and plays for the
women’s illustrateds.”
“But you do the big marriages too, I guess?” Mame’s voice throbbed.
“Not often. All marriages are so much alike they bore one.”
Mame’s expressive countenance showed that she could not imagine
herself being bored by doing marriages. “I’d just love that.”
“Love what?” The girl tickled the ear of Fu Ching Wei with the
meerschaum holder.
“I’d love to do the real class marriages for real class papers.”
The girl gave a shrug that Paula Wyse Ling would never have
permitted herself. But natural elegance carried it off.
Was she still putting it over on her? Or was she just trying to cheek
her? Not that it mattered. Even if she was a regular queen of
bluffers, she was also by a long sight the most interesting creature
Mame had yet found in London.
So far the girl had left to Mame the business of asking questions.
But in spite of an air of nonchalance, which Mame rather admired,
she was not above putting one or two questions of her own.
“Are you a writing person?” she said, offering Fu Ching Wei a little
milk in a saucer.
“You said it.” Of all the reams Mame had written since trekking east
hardly a line had found its way into print; but that did not prevent
her taking pride in the fact that the pen was her vocation. She
hesitated a moment. Then she opened her bag and produced a card.
By now she knew enough of the newspaper walks of Britain to doubt
the worth of this bit of pasteboard. At first it had given her real
pleasure to display it. But she had now reached the phase when she
was not sure that her card was not where she got off.
Still, there was nothing to lose by shooting it upon this girl. It would
be trying it, as it were, upon the dog. This smart skirt was the top of
her class. No matter what she might be, she was just as full of style
as she could hold. It would be worth while to note the effect of a
rather doubtful talisman upon her.
She did not say so, nor did her manner betray the fact, but it was a
sure thing that she had never heard of Cowbarn or its leading
newspaper. But Mame liked the kind and friendly way she handed
back the card with the remark: “You’re in journalism too, I see.”
No lugs. No frills. By her own account she was a he-one at the
game. It had been Mame’s instinct to doubt that, but this tone of
pleasant quietness, this we’re-all-friends-round-the-darned-old-
inkpot style was something new. This bird who was dressed to the
nines, and who behaved as if she just naturally owned London,
seemed to be quite disarmed by the European Correspondent of the
Cowbarn Independent.
Without getting gay or in any wise familiar, she became as chatty as
if she and Mame had begun their young lives together at the same
convent school. It was clear that Mame had aroused her interest.
The questions she put were shrewd and the answers she received
amused her.
Mame asked if she knew the States.
She got over there sometimes. “Great fun, the U. S., I always think.
Don’t you?”
Mame had never found the land of her fathers great fun, but she
had far too much pride in it to say so.
“The U. S. is so progressive.”
“You said it.”
The girl had a lot to say of America. And every word was well
disposed, without any touch of condescension.
“Stay, I guess, with the Vanderbilts and the Astors when you visit
New York?” Mame threw a plummet to bring her down to cases.
“The MacFarlanes are my particular friends.” She spoke off-handedly.
“And they always give one such a good time.”
“I’ll say, yes,” Mame remarked drily. She was not quite clear in her
mind whether the madam could be allowed to get away with that.
She would be saying next that in London her headquarters were
Buckingham Palace.
The girl produced a cigarette case. It was a wonderful piece of
chinoiserie in flowered purple silk. “Have a gasper?”
Mame had yet to acquire the habit of smoking gaspers. She declined
with thanks. But the girl fitted an amber-scented one to the
meerschaum holder so elegantly, that Mame decided to practise the
art at the first opportunity. Paula Ling had said that it was even more
chic in Europe than it was on Long Island. As usual Paula Ling was
right.
While Mame, out of the corners of a pair of very seeing eyes,
marked all that the smart piece did, she took a resolve to start in at
once to develop her own personality. Here was terrific personality. It
did not in any sense obtrude; it did not sort of hit you right in the
middle of the eye, as Paula’s did, but it was there all the time.
Moreover, it was earning dividends for its owner. This skirt was not in
the true sense of the word a looker, but there was jazz in her talk, in
her actions, in all her ways. She did not paint her face, use lip-stick
or bead her eyes; in clothes, although Mame guessed they were as
good as could be got for money, she was quiet; but her general
effect was as salt as a breeze from the sea. Mame could but envy
and admire and wonder how the trick was done.
“Staying long in England?”
“I’ll have to get off this side of the world pretty soon now.” Mame
spoke a little wistfully.
Without seeming to look at Mame, the girl, from behind the rampart
of the meerschaum holder, must somehow have read the true index
to her feelings. That index was Mame’s eyes. Very good eyes they
were; and, unknown to their owner, singularly expressive. Grey eyes,
large, serious, open, full of trouble. For all the orbs behind the
meerschaum holder were so impersonal, when as now they were
three-quarters lidded, they had a power of seeing into things that
might have astonished Mame considerably had she known the full
extent of their faculty.
“Anything I can do for you?”
It was one British journalist to one American or vice versa: a bit of
international courtesy. But to Mame it was more. There was a
genuine ring of kindness, as pure a note of music as Mame had yet
heard.
Her practical mind at once got busy. This might be a chance. Bluffer
as this girl most likely was, there could yet be no harm in trying her
out.
“Before I go back home,” said Mame, tentative as a kitten treading
on ice; “I’d like an invite to some mansion of real class. I’d like to do
a big wedding for my paper.”
“Do you mean this function next week at Clanborough House?” The
girl was journalist enough to own a mind which could move with
uncommon nimbleness.
“You’ve made it in one.” Quick in the uptake, this bird. Mame was
moved to say so.
“My dear Watson, really quite simple.” The meerschaum holder
received a Sherlock Holmes tilt. “George Rex and Consort are going
to honour the occasion. You saw it in the Times this morning.”
Mame breathed hard. This girl was no slouch. A four-flusher, yet she
might have strings to pull. And it would be one over on Paula Ling if
a little hick from Cowbarn, Iowa, got playing around among the
royalties; not to mention the Fleet Street gentleman who had said
the only way she would get to Clanborough House would be as a
hired girl. The insult still rankled.
“A dull affair!” The new friend butted pleasantly into a rather tense
pause. “But I ought to have a card somewhere that may get you in,
if you care to come.”
Mame’s heart seemed to miss a beat when the girl began a search
for an invitation to the terribly beparagraphed wedding the following
week at Clanborough House.
“Should be one here.” Calmly she produced the beautiful cigarette
case. Something leapt in Mame’s throat as the entire contents of the
case were toppled out on to the tablecloth. There were half a dozen
cigarettes and twice that number of cards of various shapes and
sizes.
“Private view Black-and-white Exhibition, Burlington House.” Mame
was seething with suspense, but the girl went calmly and leisurely
through the cards. “Arts and Handicrafts Exhibition. Admit Bearer.
British and Foreign Bible Society. Randal Cantuar in the Chair.
Opening of Royal School of Cookery, New Wandsworth. Annual
Meeting Dumb Friends’ League. Reception for Dr. Hyam Baines
Pennefather, Baltimore Third Church, Hotel Cecil. No—yes—no. It
almost looks as if we’ve drawn zero.”
Mame’s heart sank. It was no more than was to be expected of a
tinhorn, but it would have been cracker-jack to have sailed into
Clanborough House by the main entrance, along with the King and
Queen and half the real doughnuts in the island.
She bit her lip with disappointment, yet at the back of her mind was
the knowledge that these things did not happen. They were too
good to be true. But the melancholy privilege still remained to one
who aspired to close and accurate observation of the human comedy
of seeing what the four-flusher would do next.
The girl coolly returned the contents to the lovely silk case. And then
she said in that casual tone which Mame was now beginning to
resent rather more than she admired: “Give me your address.”
Part of her bluff, of course. Still Mame saw no reason why her
address should not be given. Truth to tell, she was just a little proud
of it. Like many things in this queer city, it sounded better than it
was. She promptly took from her bag a decidedly professional-
looking reporter’s note-book, tore out a leaf, and then wrote
carefully with an equally professional-looking fountain pen: Miss
Amethyst Du Rance, Fotheringay House, Montacute Square,
Bloomsbury.
“Thanks,” said the smart skirt. Then she gave a glance, cool and
impassive, at what Mame had written; and, then, with a lurking
smile, which Mame was quick to detect, she added this memento to
the others which adorned her case.
“I’ll be glad of an invite for Clanborough House,” said Mame with
irony.
“Right-o. You shall have one in the course of post.”
“I don’t think,” Mame confided mutely to the dregs of her teacup.
And then she said with a demure mockery that was rooted in the
heart’s bitterness, “I reckon you’ll be there.”
The answer was “Sure” in the way it is given in New York. Perhaps
the high-flyer guessed that Mame was trying to call her bluff. Yet
beyond a doubt she carried it off royally. “I suppose I’ll have to be.”
“To write a report for your syndicate.” Mame’s voice had something
terribly like a sneer in it.
The girl laughed and shook her head. “This binge is a bit too much
of a family affair.”
“Oh!” said Mame inadequately. It was not easy to call the bluff of
this girl.
While Mame, who had now begun to feel vindictive, was seriously
considering the best means of letting this short-sport know that she
was not quite such a sucker as she seemed, a young man who had
just risen from an adjacent table came stalking her stealthily from
behind. He patted her on the shoulder.
“Hulloa, Bill!” The tone was very light and whimsical. “I didn’t see
the cat bring you in.”
Mame listened keenly for Bill’s answer. But it amounted to nothing
beyond a cheery laugh. All the same, she was mightily interested in
Bill.
He was dressed to beat the band: braided morning coat, white
spats, the last word in neckties. Evidently a regular fellow. He was
one of those upstanding, handsome boys in which the West End of
London seems to abound. Perhaps he was twenty-seven, or a little
less, with a skin naturally fair burnt to a most attractive shade of
copper by the suns of foreign climes. There was something so
wholesome and clean, so manly and trim about Bill, that even a girl
of sense might be expected to fall in love with him on sight. Mame
was not in a position to think of love. But he looked such a white
man, and so faultless in his grace that even as it was she could not
repress a little sigh of envy. Some girls didn’t appreciate their luck in
having boys of that sort feeding from the hand.
“Going?” Mame heard him say.
The queen of the four-flushers answered with an unmistakable “Yep”
which might have come from the Bowery. She went on to discard
her cigarette, to put away her meerschaum holder and then to
examine the inside of her purse. “Dammitall!” she said. “No change
and I must leave a shilling under the plate for the waiter. Have you
one about you, Bill?”
Bill obliged. The girl laid the shilling under her plate and got up from
the table. As she did so she turned abruptly to Mame and held out
her hand in a most winning manner. “A-rivederci. I have your
address. I won’t forget that card. So glad to have met you.”
While Mame returned doubtful thanks for a favour she did not
expect to receive, the girl and her escort were already under way.
With mingled feelings Mame watched them pass along the line of
tables. She saw the girl blow a kiss to the old woman with the
Roman nose, who in return offered a most truculent scowl. But this
was effaced by the homage of the maître d’hôtel, who bestowed
upon the girl an exaggerated bow. Moreover, as she made a smiling
progress down the long room, many eyes seemed to follow her; or,
as Mame was inclined to think, the eyes of the feminine section of
the tea-drinking public were drawn by the escort Bill.
Indeed, as a pair they were distinctly “it” as they went along to the
door. The girl stopped at several tables just to pass the time of day,
while Bill stood by like a big and amiable Newfoundland dog.
Mame sighed again. Yes, some skirts had luck! Up till that moment
she had not realised the possibilities in writing for the newspapers.
She would get no card, of course, for Clanborough House. But she
was already resigned to that. Birds of that sort were much too busy
paddling their private canoes. And why not? You simply got nowhere
if you didn’t.
When the girl finally went out through the doors at the end of the
room Mame was sure that she had seen and heard the last of her.
That was the way of the world as already she had come to
understand it. The big cities were chock-full of interesting folks, but
unless you were just-so it was not worth while to take you up.
To be worth while, that was the open sesame to New York and
London. Paula Ling had grasped that truth. That was why she was a
mass of paint and powder and patchouli; that was why she screwed
herself like a manikin, into tight smart clothes. But this skirt left
Paula standing. The Paulas of life, for all their brains and their will-
power, could not live five minutes with this sort of girl, who had
every new trick, and who, like Cinquevalli the famous conjurer, was
so expert she could almost do them shut-eye.
So much was Mame occupied with these thoughts that it was not
until she had paid her bill and was out once more upon the cold
pavement of Pall Mall that she gave herself a mental shake. She was
a fool. Had she kept her wits about her she would at least have
asked the waiter the name of this queen among four-flushers.
XIII
MAME had quite made up her mind that she would not receive an
invitation to the wedding reception at Clanborough House. Why
should she? That the girl would prove as good as her word was not
on the cards. Such a promise was no more than a slick Londoner’s
way of showing how much she was in it, without really being quite
so much in it as she showed.
After all, however, it is a funny world. And this was Mame’s
reflection, when rather late the following afternoon, the little maid,
whose name was Janet, handed her a large, square, important-
looking envelope that had just come by post. At the sight of the
coronet on the back and the general air of quality Mame’s heart gave
a jump.
The unexpected had happened. Her Grace the Duchess of
Clanborough requested the honour—requested the honour, mark
you!—of the company of Miss Amethyst Du Rance at the marriage of
the Marquis of Belfield with her niece Miss Van Alsten at St.
Margaret’s, Westminster, at three o’clock on Thursday, April 6, and
afterwards at Clanborough House, Mayfair.
It was very odd. But it was distinctly thrilling. There was no need to
be so humble after all. The girl evidently was interested in Miss Du
Rance and had gone out of her way to do her a service. And Miss Du
Rance did not mind owning that she had been a little too ready to
suspect her of not being on the level.
With a sensation of deep but quiet triumph Mame listened now to
the tabbies faintly purring over their teacups. It called for self-control
not to ask the arch-puss, who gave herself out a bishop’s niece,
upon whom Mame had an especial down, whether she was going to
the ceremony at St. Margaret’s or to the reception at Clanborough
House, or whether she meant to do both?—although privately quite
sure that the old stiff was going to do neither. Happily she
remembered a text of the village preacher in the grim days when
she had to endure him every Sunday: “Be not exalted lest ye be cast
down.”
In spite of a glow in the centre of her being, the warning in those
words could not have been more salutary. So fully had Clanborough
House been dismissed from Mame’s thoughts that she had already
made up her mind to quit London as soon as possible. In fact, she
had just informed Mrs. Toogood that she would not require a room
beyond Saturday; and she had decided to go immediately after
breakfast to-morrow morning, Wednesday, to book a second-class
berth in the Vittoria, which was to sail three days later for New York.
The invitation to Clanborough House looked like changing all that. It
rather set Mame on the horns of a dilemma. A girl truly wise would
stick to the plan she had made, the voice of prudence told her.
Clanborough House would probably mean another fortnight in
London; it would involve her in a new hat and other expense; and if
she was not careful such a hole would be cut in her purse that
alarmingly few dollars would remain in it by the time she found
herself back on Broadway.
These reflections gave Mame a jolt. A lot of use an invite to
Clanborough House, if the price of it brought you to your uppers.
Aunt Lou’s legacy would be gone, along with the hundred and ten
dollars she had been able to save. Her job would be lost. And in a
place like New York it was no certainty that she would get another at
short notice. She had heard it described by those who should know
as the cruelest place on earth for persons who were up against it.
These were problems. Invitation in hand, Mame fiercely considered
them. Should she? Or should she not? The famous highbrow William
Shakespeare, according to the office calendar whose mottoes she
had by heart, the famous highbrow William Shakespeare had made
the statement that “there is a tide in the affairs of men, which, taken
at the flood, leads on to fortune.”
It might be so. That was true, no doubt, for some. But again, for
others it was quite likely not to be true. Circumstances alter cases.
William Shakespeare was writing in the spacious days of Queen
Elizabeth when there were not so darn many go-getters around. In
his time there were not more people than jobs and all the seats in
the public parks of the big cities were not overflowing with those
who couldn’t raise the price of a meal.
A problem, sure. On the one hand, prudence, foresight, a looking-
before-and-after; on the other, ambition, hope, adventure, all the
worth-while things. Such a chance would never recur. And if she had
brain enough to use it in the right way, there was no saying where it
might lead.
Mame spent a very restless night. But somewhere in the small
hours, when her mind was at its most lucid, she took the
momentous decision to follow her star.
If she turned back now, with the gates of her kingdom opening
wide, she never deserved to see them again. “Stick it, Mame.” That
had always been her slogan, even in the cold hour of sun-up before
the day’s work began or over a guttering candle after it was done,
when secretly she gave her whole mind to the hard and dry study of
stenography.
It was that power of giving her whole mind to things that in the end
had won freedom. If she had taken a line of least resistance or been
afraid to go all out for the things she wanted, she would still have
been doing chores upon the farm. No, she must stand up to her
luck. And if the worst came she could go home steerage.
Full of new resolve, Mame’s first act was to inform Mrs. Toogood that
she proposed to stay on at least another week. Then, after an
elaborate calculation of ways and means, she set out on a tour of
Oxford Street. A new hat she must have. When in Rome, etc. No use
looking a frump at Clanborough House. She would be mixing with
class. And if she was careful how she dressed and she watched her
step all the time, the folks might not be able to tell her from real.
A quiet mode was best suited to Miss Amethyst Du Rance. After
much observation of herself and other people, that was her
conclusion. Like most of her countrywomen she had a flair in the
matter of clothes. New York and London had taught her their value.
Already she was getting to know the worth of the mysterious
attribute, style.
The girl she had met at the Carlton was a revelation of what style
could do. It was a far better thing than mere looks. But Mame’s
ambition was to have both. And if she could only fulfil it, there was
no reason, so far as she could see, why she should not unlock the
most exclusive doors in Britain.
At all events, it should not be for want of trying. If the invite to
Clanborough House meant anything it was that she had found a
bonanza. The girl must be a regular high-flyer, and for some
mysterious reason, which Mame could not fathom, she was willing to
be a fairy godmother. It was up to Mame to prove her own mettle.
Here, at last, was a chance to pull the big stuff.
Many hours in Oxford Street were necessary before Mame’s
prudence could decide just how much to be bled. She had to get
home, if, in spite of Clanborough House, the stars in their courses
played her false. After she had duly paid for the hat on which she
had set her heart, and a captivating fox so near real that she fell for
it at the last moment, she was quite alarmed by the narrow margin
of safety.
At the end of the day she wrote an urgent letter to the editor of the
Cowbarn Independent. She told him how disappointed she was not
to have had a line all the time she had been in Europe. And she
hinted that a few dollars in exchange for the fifteen columns she had
already sent him would be welcome.
But what was the Independent anyway? At best a fourth-rate sheet,
a small-town rag. She would forget it. The time had surely come to
fly at higher game.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookmass.com

You might also like