100% found this document useful (1 vote)
3 views

JavaScript: A Beginner's Guide, Fifth Edition Pollock download

The document provides links to download various educational ebooks, including 'JavaScript: A Beginner's Guide, Fifth Edition' by John Pollock and other related titles. It emphasizes the availability of high-quality ebooks on ebookmass.com. Additionally, it includes copyright information and terms of use for the content provided.

Uploaded by

jidungaihera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3 views

JavaScript: A Beginner's Guide, Fifth Edition Pollock download

The document provides links to download various educational ebooks, including 'JavaScript: A Beginner's Guide, Fifth Edition' by John Pollock and other related titles. It emphasizes the availability of high-quality ebooks on ebookmass.com. Additionally, it includes copyright information and terms of use for the content provided.

Uploaded by

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

Visit https://ebookmass.

com to download the full version and


browse more ebooks or textbooks

JavaScript: A Beginner's Guide, Fifth Edition


Pollock

_____ Press the link below to begin your download _____

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

Access ebookmass.com now to download high-quality


ebooks or textbooks
We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmass.com
to discover even more!

JavaScript A Beginner's Guide Fifth Edition John Pollock

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

Javascript: Crash Course - The Complete Beginners Guide To


Learning JavaScript Programming In No Time! (Angularjs,
jQuery, JavaScript Visually) Nathaniel Hill
https://ebookmass.com/product/javascript-crash-course-the-complete-
beginners-guide-to-learning-javascript-programming-in-no-time-
angularjs-jquery-javascript-visually-nathaniel-hill/

The Norton Field Guide to Writing (Fifth Edition)

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

The Norton Field Guide to Writing: with Readings (Fifth


Edition) Fifth Edition – Ebook PDF Version

https://ebookmass.com/product/the-norton-field-guide-to-writing-with-
readings-fifth-edition-fifth-edition-ebook-pdf-version/
Creating Effective Teams: A Guide for Members and Leaders
(NULL) Fifth

https://ebookmass.com/product/creating-effective-teams-a-guide-for-
members-and-leaders-null-fifth/

Technical Analysis Explained, Fifth Edition : The


Successful Investoru2019s Guide

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

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


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

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

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


Spanish) Fourth Edition Christina Latham-Koenig

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

Writing Conversational Korean Book Two Katarina Pollock

https://ebookmass.com/product/writing-conversational-korean-book-two-
katarina-pollock/
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Folio: i

JavaScript
A Beginner’s Guide

Fifth Edition
John Pollock

New York Chicago San Francisco


Athens London Madrid Mexico City
Milan New Delhi Singapore
Sydney Toronto

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


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

ISBN: 978-1-26-045769-8
MHID: 1-26-045769-9

The material in this eBook also appears in the print version of this title: ISBN: 978-1-26-045768-1,
MHID: 1-26-045768-0.

eBook conversion by codeMantra


Version 1.0

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trade-
marked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringe-
ment of the trademark. Where such designations appear in this book, they have been printed with initial caps.

McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in
corporate training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com.

Oracle and Java are registered trademarks of Oracle Corporation and/or its affiliates. All other trademarks are the property of
their respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these
marks.

Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of Oracle Corpora-
tion and/or its affiliates.

Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human
or mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness
of any information included in this work and is not responsible for any errors or omissions or the results obtained from the use
of such information.

TERMS OF USE

This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work
is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit,
distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill Education’s prior consent. You
may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to
use the work may be terminated if you fail to comply with these terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES
OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED
FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD-
ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE. McGraw-Hill Education and its licensors do not warrant or guarantee that the functions contained in the work will
meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors
shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages
resulting therefrom. McGraw-Hill Education has no responsibility for the content of any information accessed through the work.
Under no circumstances shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive,
consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of
the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or
cause arises in contract, tort or otherwise.
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Foli iii

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


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

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


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

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


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

About the Author


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

About the Technical Editor


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

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


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

Contents
ACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
.
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
.
1 Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
.
What You Need to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
.
Basic HTML and CSS Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
.
Basic Text Editor and Web Browser Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
.
Which Version? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
.
Client-Side and Server-Side Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
.
Beginning with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Prototype-Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Interpreted Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Numerous Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
.
Try This 1-1: Use JavaScript to Write Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
.
Chapter 1 Self Test ................................................................. 11
.
2 Placing JavaScript in an HTML File ..................................... 15
.
Using the HTML Script Tags ....................................................... 16
.
Identifying the Scripting Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
.
Calling External Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
.
v

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


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

vi JavaScript: A Beginner’s Guide

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

.
Using <noscript></noscript> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

.
Creating Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

.
Writing a “Hello World” Script ............................................... 20

.
Creating an HTML Document for the Script .................................. 21

.
Inserting the Script into the HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

.
Try This 2-1: Insert a Script into an HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

.
Using External JavaScript Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

.
Creating a JavaScript File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

.
Creating the HTML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

.
Viewing the Pages in Your Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

.
Try This 2-2: Call an External Script from an HTML Document . . . . . . . . . . . . . . . . . . . . . 27

.
Using JavaScript Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
.
Inserting Comments on One Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
.
Adding Multiple-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
Chapter 2 Self Test ................................................................. 30
.
3 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Understanding Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
.
Why Variables Are Useful .......................................................... 35
.
Variables as Placeholders for Unknown Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
.
Variables as Time-Savers ..................................................... 35
.
Variables as Code Clarifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
.
Defining Variables for Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
.
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
.
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
.
Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
.
Understanding Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
.
Number ...................................................................... 41
.
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
.
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Undefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
.
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
.
Try This 3-1: Declare Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
.
Using Variables in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Making a Call to a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Adding Variables to Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Writing a Page of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Creating the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Defining the Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Adding the Commands ....................................................... 55
.
Modifying the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
.
00-FM.indd 6 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents vii

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

.
Chapter 3 Self Test ................................................................. 60

.
4 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

.
What a Function Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

.
Why Functions Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

.
Structuring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

.
Declaring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

.
Defining the Code for Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

.
Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
.
Adding Arguments to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

.
Adding Return Statements to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

.
Calling Functions in Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
.
Script Tags: Head Section or Body Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

.
Calling a Function from Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

.
Calling Functions with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
76
Calling Functions with Return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
.
Other Ways to Define Functions .............................................. 82
.
Try This 4-1: Create an HTML Page with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
.
Scope/Context Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
.
Global Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
.
Function Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.
Block Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
.
Try This 4-2: Write Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
.
Chapter 4 Self Test ................................................................. 91
.
5 JavaScript Operators ..................................................... 95
.
Understanding the Operator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
.
Understanding Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
.
The Addition Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
.
The Subtraction Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.
The Multiplication Operator (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.
The Division Operator (/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
The Modulus Operator (%) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
The Increment Operator (++) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
.
The Decrement Operator (– –) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
.
The Unary Plus Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
.
The Unary Negation Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
.
The Exponentiation Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.
Understanding Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.
The Assignment Operator (=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
.
The Add-and-Assign Operator (+=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
.
The Subtract-and-Assign Operator (–=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
.
The Multiply-and-Assign Operator (*=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
The Divide-and-Assign Operator (/=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
00-FM.indd 7 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

viii JavaScript: A Beginner’s Guide

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

.
The Exponent-and-Assign Operator (**=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

.
Try This 5-1: Adjust a Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

.
Understanding Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

.
The Is-Equal-To Operator (==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

.
The Is-Not-Equal-To Operator (!=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

.
The Strict Is-Equal-To Operator (===) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

.
The Strict Is-Not-Equal-To Operator (!==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

.
The Is-Greater-Than Operator (>) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

.
The Is-Less-Than Operator (<) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

.
The Is-Greater-Than-or-Equal-To Operator (>=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

.
The Is-Less-Than-or-Equal-To Operator (<=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

.
Understanding Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
117
The AND Operator (&&) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
.
The OR Operator (||) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
.
The NOT Operator (!) ........................................................ 118
.
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
.
Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
.
Understanding Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
.
Try This 5-2: True or False? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
.
Chapter 5 Self Test ................................................................. 123
.
6 Conditional Statements and Loops ....................................... 125
.
Defining Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
.
What Is a Conditional Statement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
.
Why Conditional Statements Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using if/else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
.
Using the Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
.
User Input from a Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
.
Try This 6-1: Work with User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
.
Defining Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
.
What Is a Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
.
Why Loops Are Useful ....................................................... 144
.
Using Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
.
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
.
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
.
do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
.
for in, for each in, and for of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
.
Using break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
.
Try This 6-2: Work with for Loops and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
.
Chapter 6 Self Test ................................................................. 160
.
00-FM.indd 8 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents ix

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

.
What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

.
Why Arrays Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

.
Defining and Accessing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

.
Naming an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

.
Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Accessing an Array’s Elements ............................................... 167

.
Using the length Property and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

.
Changing Array Values and Changing the Length ............................. 169

.
Try This 7-1: Use Loops with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

.
Array Properties and Methods ...................................................... 172
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
.
Nesting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
.
Defining Nested Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
.
Loops and Nested Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
.
Try This 7-2: Nested Arrays Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
.
Chapter 7 Self Test ................................................................. 193
.
8 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
.
Defining Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Single Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
.
Try This 8-1: Create a Computer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
.
Object Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
.
Constructor Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
.
Using Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
.
The class Keyword ........................................................... 209
.
Helpful Statements for Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
.
The for-in Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
.
The with Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
.
Try This 8-2: Practice with the Combination Constructor/Prototype Pattern . . . . . . . . . . 212
.
Understanding Predefined JavaScript Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
.
The Navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
.
The History Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
.
Chapter 8 Self Test ................................................................. 218
.
9 The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
.
Defining the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
.
Using the Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Using the Properties of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
.
The cookie Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
.
00-FM.indd 9 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

x JavaScript: A Beginner’s Guide

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

.
The lastModified Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

.
The referrer Property ......................................................... 227

.
The title Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

.
The URL Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

.
The URLUnencoded Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Using the Methods of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

.
The get Methods for Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

.
The open() and close() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

.
The write() and writeln() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

.
Using DOM Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
.
DOM Node Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
238
DOM Node Methods ......................................................... 241
.
Try This 9-1: Add a DOM Node to the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

.
Creating Dynamic Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
.
Styles in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
.
Simple Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
.
Coding a Dynamic Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
.
Try This 9-2: Try Out Property Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Chapter 9 Self Test ................................................................. 253
.
10 Event Handlers ........................................................... 255
.
What Is an Event Handler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
.
Why Event Handlers Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
.
Understanding Event Handler Locations and Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
Using an Event Handler in an HTML Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
Using an Event Handler in the Script Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
.
Learning the Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
.
The Click Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
.
Focus and Blur Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
.
The Load and Unload Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
.
The Reset and Submit Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
.
The Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
The Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
Try This 10-1: Focus and Blur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
.
Other Ways to Register Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
.
The addEventListener() Method .............................................. 272
.
The attachEvent() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
The Event Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
DOM and Internet Explorer: DOM Level 0 Registration . . . . . . . . . . . . . . . . . . . . . . . 274
.
Using event with Modern Event Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
.
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
.
Event Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
.
Try This 10-2: Using addEventListener() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
.
00-FM.indd 10 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents xi

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

.
Show Hidden Content ........................................................ 279

.
Change Content .............................................................. 280

.
Custom Events ............................................................... 284

.
Chapter 10 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

.
11 Introduction to Node.js ................................................... 289

.
Introducing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
.
Installing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
.
Check for a Current Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

.
Install Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
.
Write a “Hello World” Script ................................................. 292

.
Using Node Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
.
Using Native Node Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
295
Asynchronous Execution ..................................................... 296
.
Non-Native Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
.
Try This 11-1: Use a Custom Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
.
Installing a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
.
Database Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
.
Install PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
.
Create a Database Using pgAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
.
Try This 11-2: Test Some SQL Queries ............................................. 312
.
Creating a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
.
Chapter 11 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
.
12 Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
.
Using the Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
What Is the Math Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
How the Math Object Is Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
.
Try This 12-1: Display a Random Link on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
.
Understanding the Number Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
.
Using the Date Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
.
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
.
Methods That Get Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
.
Methods That Set Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
.
Other Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
.
How About Some Date Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
.
Try This 12-2: Create a JavaScript Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
.
Continuing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
.
Getting to the Needed Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
.
Running Some Calculations on the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
.
Chapter 12 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
.
00-FM.indd 11 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

xii JavaScript: A Beginner’s Guide

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

.
Introduction to the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

.
The String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

.
The String Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

.
What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

.
Using the Properties and Methods of the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

.
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

.
Methods of the String Object ....................................................... 360

.
Try This 13-1: Use indexOf() to Test an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

.
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
.
Setting a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
.
Reading a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
.
Try This 13-2: Remember a Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
376
Using Regular Expressions ......................................................... 377
.
Creating Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
.
Testing Strings Against Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

.
Adding Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
.
Creating Powerful Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
.
Grouping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
.
The replace(), match(), matchAll(), and search() Methods . . . . . . . . . . . . . . . . . . . . . 384

.
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.
Continuing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.
Chapter 13 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
.
14 Browser-Based JavaScript ................................................ 391
.
Window: The Global Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
.
Using the Properties of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
.
The closed Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
The frames Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The innerWidth and innerHeight Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The location Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The name Property ........................................................... 396
.
The opener Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
.
The parent, self, and top Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
The status and defaultStatus Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
Try This 14-1: Use the location and innerWidth Properties .......................... 398
.
Using the Methods of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
.
The alert(), prompt(), and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
The print() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
.
The setInterval() and clearInterval() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
The setTimeout() and clearTimeout() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
.
Try This 14-2: Use the setTimeout() and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . 406
.
00-FM.indd 12 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents xiii

The Main Window and New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

.
The Tale of Pop-up Windows ................................................. 407

.
Opening New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

.
Closing New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

.
Moving, Resizing, and Scrolling New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

.
The resizeBy() and resizeTo() Methods ....................................... 416

.
The scrollBy() and ScrollTo() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418

.
Working with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
Rollovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
JavaScript and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
420
Purpose of Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
.
Accessing Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
.
Breaking Out of Frames ...................................................... 423
.
Using iFrames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
.
Chapter 14 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
.
15 JavaScript Forms and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
.
Accessing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
.
Using the forms Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
.
Using an ID .................................................................. 431
.
Using the Properties and Methods of the Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
.
Ensuring the Accessibility of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
.
Using Proper Element and Label Order ....................................... 438
.
Using <label></label> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
.
Using <fieldset></fieldset> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
.
Not Assuming Client-Side Scripting .......................................... 439
.
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
.
Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
.
Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
.
Check Boxes and Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
.
Try This 15-1: Request a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
.
HTML5 and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
.
New Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
.
New Input Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
.
New Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
.
HTML5 Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
.
Try This 15-2: Validate a Phone Number with HTML5 or JavaScript ................ 455
.
AJAX and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
.
AJAX ........................................................................ 456
.
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
.
Chapter 15 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
.
00-FM.indd 13 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

xiv JavaScript: A Beginner’s Guide

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

.
Using jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

.
Obtaining jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

.
Getting Started: document.ready() ............................................ 471

.
Using Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

.
Altering Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

.
Methods for Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475

.
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
.
Try This 16-1: Use jQuery to Create Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

.
Debugging Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
.
Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
.
Using the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
.
Using a Lint Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
.
Browser Developer Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
.
JavaScript and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
.
Separate Content from Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
486
Enhancing Content ........................................................... 488
.
Try This 16-2: Make This Code Accessible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
.
JavaScript Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
.
Page Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
.
JavaScript and APIs from HTML ................................................... 492
.
The <canvas> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
.
Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
.
Try This 16-3: Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
.
Node.js App Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
.
Update the Node.js Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Update the Front-end Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
.
Need Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
.
Chapter 16 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
.
A Answers to Self Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
.
Chapter 1: Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
.
Chapter 2: Placing JavaScript in an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
.
Chapter 3: Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
Chapter 4: Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
Chapter 5: JavaScript Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
.
Chapter 6: Conditional Statements and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
.
Chapter 7: JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
.
Chapter 8: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
.
Chapter 9: The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
.
00-FM.indd 14 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents xv

Chapter 10: Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

.
Chapter 11: Introduction to Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

.
Chapter 12: Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

.
Chapter 13: Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

.
Chapter 14: Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

.
Chapter 15: JavaScript Forms and Data ....................................... 521

.
Chapter 16: Further Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

.
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

.
00-FM.indd 15 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Folio vi

Acknowledgments
I would like to begin by thanking my wonderful wife, Heather Pollock, for all of her love,
support, and encouragement in all I do. I love you! I would also like to thank my three
daughters, Eva, Elizabeth, and Elaine, as well as my son, Evan. I love all of you!
I would like to thank my parents, Bruce and Joy Anderson, for their love and guidance, and
for always supporting my endeavors.
I would like to thank Dr. J. D. and Linda Andrews for their love, guidance, and support.
In addition, I would like to thank Richard Pollock (brother) and family, Misty Castleman
(sister) and family, Warren Anderson (brother) and family, Jon Andrews (brother) and family,
Lisa and Julian Owens (aunt/uncle) and family, and every aunt, uncle, cousin, or other relation
in my family. All of you have been a great influence in my life.
I would like to thank all of my editors at McGraw-Hill for their outstanding help and
support throughout the writing of this book. Thanks to Lisa McClain, Emily Walters, Claire Yee,
Snehil Sharma, Sarika Gupta, Bart Reed, and to all the editors who worked on this and previous
editions of the book.
Thanks to my technical editor, Christie Sorenson, for editing and checking over all the
technical aspects of the book and for helping me provide clear explanations of the topics that
are covered.
I would like to thank God for the ability He has given me to help and teach people by my
writing. “In all your ways acknowledge Him, and He shall direct your paths.” (Proverbs 3:6)

xvi

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


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

Introduction
W elcome to JavaScript: A Beginner’s Guide, Fifth Edition! Years ago, I was surfing
the Web and noticed that people were publishing pages about themselves and calling
them homepages. After viewing a number of these, I decided to create a homepage myself. I
had no idea where to begin, but through trial and error I figured out how to code HTML and
publish my documents on a Web server. Over time, I saw some interesting effects used on other
homepages (like alert messages that popped up out of nowhere or images that would magically
change when I moved my mouse over them). I was curious and just had to know what was being
done to create those effects. Were these page creators using HTML tags I did not know about?
Eventually, one site revealed what was being used to create those effects: JavaScript. I
went in search of information on it and came across a few tutorials and scripts on the Web.
Since I had programmed in other languages (such as a relatively obscure language called Ada),
I was able to catch on to JavaScript fairly quickly by looking at these tutorials and scripts.
I learned enough that I decided to create a Web site that would teach HTML and JavaScript
to beginners. As soon as I began the project, I received questions from visitors that were
way over my head—forcing me to dig deeper and learn more about JavaScript. As a result,
I became completely familiar with this scripting language and what it can do. Not only can
you add fun effects to a Web page, you can create scripts that will perform useful tasks, like
validate form input, add navigational elements to documents, and react to user events.
The goal of this book is to help you to learn the basics of the JavaScript language with as
little hair pulling and monitor smashing as possible. You do not need any prior programming
experience to learn JavaScript from this book. All you need is knowledge of HTML and/or
XHTML, Cascading Style Sheets (CSS), and how to use your favorite text editor and Web
browser (see Chapter 1 for more information).

xvii

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


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

xviii JavaScript: A Beginner’s Guide

What This Book Covers


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

● Key Skills & Concepts Each chapter begins with a set of key skills and concepts that
you will understand by the end of the chapter.
● Ask the Expert The Ask the Expert sections present commonly asked questions about
topics covered in the preceding text, with responses from the author.
● Try This These sections get you to practice what you have learned using a hands-on
approach. Each Try This will have you code a script through step-by-step directions
on what you need to do to in order to accomplish the goal. You can find solutions to
each project on the McGraw-Hill Professional Web site at www.mhprofessional.com/
computingdownload.
● Notes, Tips, and Cautions These elements call your attention to noteworthy statements
that you will find helpful as you move through the chapters.
● Code Code listings display example source code used in scripts or programs.
● Callouts Callouts display helpful hints and notes about the example code, pointing to the
relevant lines in the code.
● Self Test Each chapter ends with a Self Test, a series of 15 questions to see if you have
mastered the topics covered in the chapter. The answers to each Self Test can be found in
the appendix.

That is it! You are now familiar with the organization and special features of this book to
start your journey through JavaScript. If you find that you are stuck and need help, feel free to
contact me with your questions. To contact me, you can reach me on LinkedIn (https://www
.linkedin.com/in/john-pollock-82a2b074) or you can find me on Twitter (@ScripttheWeb).
Now it is time to learn JavaScript. Get ready, get set, and have fun!

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


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

Chapter 1
Introduction to JavaScript

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


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


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

22 JavaScript: A Beginner’s Guide

Figure 2-1 The test.html file in a Web browser


 
Save the test1.html document again. You should now be able to open the document in your
Web browser to see the results of the script. Figure 2-1 shows how the text should look in your
browser when you load the Web page.
Congratulations, you have now finished your first script!

NOTE
The example code in this section uses the entire HTML document and all of its tags. In
order to keep things as relevant as possible, from this point on the example code will
use only the HTML tags involved with the scripts rather than the entirety of its tags.
Project code may use entire HTML documents as needed.

Ask the Expert


Q: Why is there a dot (.) in the document.write() command?
A: Document is one of JavaScript’s predefined objects, and write() is a predefined method
of the document object. The dot puts the object and the method together to make the
function work. Chapter 10 explains JavaScript objects, and Chapter 13 introduces front-end
JavaScript and the document object in more depth.

Q: How do I know when to add the script inside the head section and when to add it
inside the body section?
A: In the past, JavaScript code was almost always placed inside the head section, which kept it
in a separate area from the rest of the HTML code. Modern coding practice is typically to
place all JavaScript code in an external .js file and to place the <script> tag(s) right before
the closing </body> tag. This ensures that the HTML page has loaded in the browser (since
large scripts can delay the loading of the page if placed elsewhere), giving the user a better
experience.

02-ch02.indd 22 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 23

Try This 2-1 Insert a Script into an HTML Document


pr2_1.html This project gives you practice adding a script to your page. You will create an
HTML document and insert a script that displays a short sentence in the browser
window when the page loads.

Step by Step
1. Set up an HTML document so that you have a simple file with nothing between the <body>
and </body> tags yet.
2. Put the following line of text into the Web page within a <p> tag:
I am part of the HTML document!
3. Insert a <p> tag after this line.

4. After the <p> tag, insert a script that will write the following line on the page:
This came from my script, and is now on the page!
5. After the script, add a </p> tag. Add another opening <p> tag.

6. Put the following line of text into the Web page after the last <p> tag, and make it
emphasized (using <em></em> tags):
I am also part of the HTML document, after the script results!
7. Add a </p> tag to complete the paragraph.

8. Here is what your HTML document should look like:


<!DOCTYPE html>
<html>
<head>
<title>JavaScript Project 2-1</title>
</head>
<body>
<p>I am part of the HTML document!</p>
<p>
<script>
document.write("This came from my script, and is now on the page!");
</script>
</p>
<p><em>I am also part of the HTML document, after the script results!</em></p>
</body>
</html>

9. Save the file as pr2_1.html and view the page in your browser to see the results.

(continued)

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


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

24 JavaScript: A Beginner’s Guide

Try This Summary


In this project, you created an HTML file. Using the knowledge that you acquired thus far in
this chapter, you inserted within the HTML file a script that writes a specific line of text on the
page. When the HTML page is opened in a Web browser, the result of the script is displayed
between two lines of text.

Using External JavaScript Files


Now suppose that you want to use your “Hello World” script (the one you created earlier in this
chapter) on more than one page, but you do not want to write it out on each page. You can do this
by putting the script in an external script file and calling it with the src attribute of the script tag.
For this method, you need to create a JavaScript text file to hold your script. You also need one or
more HTML files into which you will place the script tags to call your external script file.

Creating a JavaScript File


For this example, you will create a JavaScript file that contains only one line. For practical
applications, you would use this approach for lengthier scripts—the longer the script is, the
more useful this technique becomes (especially if you are trying to validate your Web pages or
you are separating your script code from your markup).
Open a new file in your text editor and insert only the JavaScript code (the document.write()
statement) itself. The script tags are not needed in the external JavaScript file. The file should
appear like this:
document.write("Yes! I am now a JavaScript coder!");

Save the file as jsfile1.js in your text editor. To do this, you may need to use the Save As option
on the File menu and place quotation marks around your filename, as shown in Figure 2-2 (using
Notepad with Windows). If you are using an editor such as VS Code or Sublime, you won’t need
to add quotes around the filename since .js files are already supported.
Once the file has been saved, you can move on to the next step, which is to create the HTML
files in which to use the script.

Creating the HTML Files


You will create two files in which to place your script. The technique should work for any
number of HTML files, though, as long as you add the required script tags to each file.
For the first file, create your base HTML document and insert the script tags into the body
section of the document, using the src attribute to point to the jsfile1.js file, and add some HTML
text to the body of the page to identify it as the first HTML document:
<body>
<script src="jsfile1.js"></script>
<p>
This is page 1, and the script works here!
</p>
</body>

02-ch02.indd 24 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 25

Figure 2-2 An example of saving a file with a .js extension using quote marks so it will save
 
with the correct file extension

Save this file as jsext1.html in your text editor. Be sure to save it in the same directory as
your jsfile1.js file.
The second HTML document looks the same as the first one, except that the HTML text
says that it’s page 2:
<body>
<script src="jsfile1.js"></script>
<p>
This is page 2, and the script also works here!
</p>
</body>

Save this file as jsext2.html in your text editor. Again, be sure to place it in the same
directory as the other files.

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


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

26 JavaScript: A Beginner’s Guide

Figure 2-3 The result of calling the script in the jsext1.html file, the first HTML page
 
Viewing the Pages in Your Browser
Open the jsext1.html file in your Web browser. It should appear as shown in Figure 2-3, with
the JavaScript inserted in the page from the external script file.
Next, open the jsext2.html file in your Web browser. It should appear as shown in Figure 2-4,
with only the small difference of the text you added to the HTML file to say that this is page 2.
The JavaScript should write the same text to this page as it did to the first HTML page.
Although we used a short script in this example, it should give you an idea of how using an
external file could be a great time-saver when you have a large script.

Figure 2-4 The result of calling the script in the jsext2.html file, the second HTML page

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


Other documents randomly have
different content
"Good God!" I exclaimed, "I had not the slightest idea that the
baron had gone to Saumur."

"Oh! you mistake, you mistake," said the prince. "My father did
not speak of sending you to Champigné-le-sec, which, as its name
implies, would be a dry residence for you enough, but to Champigny
near Paris, where we have estates, and an old chateau of which we
are all fond. But still I must say it is not in my power to affect at all
my father's determination about your imprisonment. Indeed, I must
confess, I think it best for you that it should be as it is; and, at all
events, I have no authority in the matter. What I alluded to was
something quite different. The day before yesterday, as we were
riding down towards Cognac, my father and I were talking of you,
and we determined, in memory of the day when you and I first met,
to make you a present of a little farm that we lately bought, for the
purpose of giving it to an old friend of ours, but who was
unfortunately killed in the first skirmish of this campaign. It lies close
to our own place at Champigny, and is called by his own name,
which was the cause of our buying it for him. That name is Les Bois.
It remains just as we had it all arranged to give him. The old
chateau, though but small, is, I think you will admit, as sweet a spot
as well could be chosen to repose in after the toils of war. We have
had it tapestried and furnished afresh throughout in the very last
mode; and the annual rent amounts to about five thousand livres
per annum.

"Oh, my lord, my lord, mention not such a thing to me," I cried.


"Although your rank and mine might well permit me to accept your
bounty, yet such a gift as that I am utterly undeserving of."

"Not at all, De Cerons, not at all," replied the prince. "You must
recollect the circumstances under which it is offered. If, on the
occasion you speak of, you had chosen to have killed me, you might
have done so; but you were too generous for that. You might
equally have made me your prisoner; but the truth was, you thought
me a mere boy, and let me escape. I have no objection, De Cerons,
to remain under obligations to you; and, even in offering you this
little gift, both my father and myself are still your debtors. You forget
what would have been the ransom of the Prince d'Auvergne. I know
well what it would be if Montluc had to fix it. Certainly not less than
fifty thousand gold Henris, or a hundred thousand crowns of the
sun. The estate we give, in all cost but a third of that sum; and
therefore, my good friend, I still bear a great portion of my ransom
to the credit of gratitude. The deeds of the estate my father has left
with me to make over to you, and, if we can find a notary within ten
miles of our halting-place, they shall be made your own this very
night."

It may be easily conceived what were my feelings upon the


present occasion. The tone in which he spoke, his whole manner
and look, left no opportunity of refusing even with courtesy, had I
been so inclined. But when I looked upon his offer, and thought that
that which was given so generously might be but the foundation of
my future fortunes, I felt no such inclination to refuse. I thought of
Louise, too, my own bright Louise, and I felt the letter which she
had sent me, and which I had placed in my bosom to read when
alone, glow warm upon my heart when new hopes and expectations
entered into it.

The eye of the prince was upon me as I thus thought, and he


seemed to read all the feelings that were passing in my bosom, for a
smile came up upon his countenance, and he said, "Come, De
Cerons, you accept it. Prithee, not a word more. At Champigny you
will have the opportunity of visiting your new estate, or even of
dwelling there if you so will, for the limits of the two properties
touch, and, of course, you may reside at which you will. It is better,
perhaps, that you should go to Champigny at first, where everything
is prepared and ready for you; and, in the mean time, as it is
somewhat dangerous just now for a Protestant to appear in the
neighbourhood of Paris, you may take with all safety the name of
Des Bois, as you have made that of De Cerons somewhat too well
known."
Thus conversing, we went on our way, and in the evening arrived
at the camp under the walls of Angoulême. Persons were waiting for
us at the quarters marked out for the Prince d'Auvergne, inviting us
to sup with the Duke of Montpensier, and not a moment was allowed
me to read the letter of Louise till I retired to rest for the night.

In the mean time, however, two circumstances happened which I


must notice briefly. The first was the actual transfer of the chateau
and property of Les Bois to myself, which was executed that night in
the presence of a notary, both the Duke of Montpensier and the
prince signing the act. The next occurred as we were pausing round
the table for a moment after supper. There was no one in the
chamber but the duke, his son, and myself, and we were about to
separate, when an attendant announced that the Jew, Solomon
Ahar, waited without. Probably each of the three thought that the
business of the Jew was with himself; but the duke said, "It is only
that usurious Jew, who comes to tell me, I suppose, that the Duke
of Anjou cannot have the money that he wants. In fact, I saw it
would be so last night; and I suppose that the man is afraid of
telling the duke himself, lest he should lose his ears, so comes to put
the unpleasant task on me. Send him in, however."

In a moment after poor Solomon Ahar entered, cringing and


bending down to the ground.

"Well, Solomon," said the duke, "you have come sooner than I
expected to see you; and I suppose this promptitude shows that you
have no very good news to bring me."

"Not so, my most gracious lord," replied the Jew, bending again to
the very ground. "On the contrary, I come to say I think it can be
done. I trust it can be managed. I have good hope that we can
accept the terms of the noble prince; for, as I came along but now, I
have had much talk and conversation with some of the gentle
leaders about arms, and spoils, and ransoms, and what not, and I
have done a little commerce by the way, so that I think the matter
can be done to the prince's contentment; and I came to tell you
first, monseigneur, because I thought it would do you a pleasure to
tell his highness yourself."

"On my life it does!" cried the duke; "for there is many a thing I
want the prince to do, which I dare not even ask when he is in such
a humour as at present."

"It is all owing, my very good and excellent lord," said the Jew, "it
is all owing to these two noble gentlemen, my excellent good lord
your son, and that very respectable knight who sits by him; for, had
it not been for their protection, and my lord the prince's permission
to come with the main battle, I should never have seen these worthy
traders, and done the little commerce that enables me to pleasure
the prince."

"It cannot be a little commerce, good Solomon," said the duke,


"Which enables you to furnish a sum of two hundred thousand
crowns, when you declared you could not find it in all Paris."

"On my life and soul!" cried the Israelite, "it will but pay the
interest of the money in case I be a loser."

But both the duke and his son laughed, and Solomon himself
grinned silently, as if he did not in the least degree expect to be
believed. He produced from under his robe, however, two small
packets, one containing the most exquisitely beautiful pair of gloves
for a lady that I ever beheld, being formed of peach-coloured velvet,
embroidered on the back with gold and pearls, which he laid before
the Prince d'Auvergne, begging his acceptance of them as a present
for any lady that he loved. The other was a small plain dagger, about
two hands' breadths in length, the haft of which was as plain as it
well could be, being distinguished by nothing but a few lines of gold
inlaid in the steel. The blade, which he drew from the plain steel
sheath, was thick and dull in colour, as if it had been rusty and ill
cleaned. Nevertheless, this somewhat coarse-looking implement he
laid upon the table before the duke with great reverence, saying,
"Let me beg your noble acceptance of that which, though it looks
but a poor gift, may be considered as invaluable. That dagger is
made of one cake of pure Damascus steel. It will pass through the
finest-tempered corslet that can be produced in the camp, even
when struck by a weak arm; and with that dagger the Emperor
Hassan, caliph of the Moors, killed no less than ten Spanish cavaliers
at the great battle of the Salado."

The Duke of Montpensier seemed to value the gift highly, and the
Jew then turned towards me, bowing lowly, and saying, "I have not
forgotten to be grateful to Monsieur de Cerons."

"The only gratitude I wish, good Solomon," I replied, "is, that you
would find for me a certain dagger that you know of, and which I
fear may be lost to me for ever by the death of the person to whom
you delivered it."

"I feared so, I feared so," said the Jew; "but it shall be found if it
be on this side of Constantinople. I have heard, good sir, that you
are going towards Paris; so Monsieur Arnon, the intendant of good
Monsieur d'Auvergne, told me; and I would fain travel in such safe
company, especially as I go on the business of his Highness of
Anjou," he added, looking at the duke.

"Be it so, be it so," said the Duke of Montpensier; "and the sooner
you arrive in the capital the better."

"On the twenty-fifth day of the present month," said the Jew, "his
highness may draw on me bills of exchange through any of the
merchants of Poitiers. They will not refuse him the money when they
see the name of Solomon Ahar."

The duke seemed not a little pleased with this intelligence, and, a
few words more having passed, Solomon retired from the room, and
the duke hastened to communicate the news he had received as fast
as possible to the Duke of Anjou.
In the mean time, the Prince d'Auvergne and I returned to our
quarters, and bidding me kindly adieu, as I was to depart early on
the following morning, he left me, as he thought, to repose. Sleep,
however, was not destined to visit my eyes that night. It was with
difficulty, my right hand and arm being still bound up in its wooden
case, that I was able to open the letter of Louise; and oh! when I
did open it, what pain did it inflict! The letter has been since
destroyed, so that I cannot give it accurately; but it informed me
that the baroness had notified to her that her father had concluded
upon a marriage between her and the Lord of Blaye. Her consent,
she said, had never been asked and the marchioness had
immediately left her stupified and thunderstruck. The only
consolations she had, the poor girl said, were, in the first place, that
the man himself was absent with the army, and likely to be absent
for long; and, in the second, that La Tour assured her that the baron
himself had fixed that the marriage should not take place for some
time. To give me some comfort under such circumstances, she said,
"You know me, Henry, and know that I would rather die. But, oh!
that I could see you, and speak with you now, if it were but for a
few hours!"

It may well be conceived that the time now seemed to lag; and,
when I at length set off upon my journey towards Champigny, every
league seemed extended to two or three, every minute was
protracted into days. I was the first in the saddle in the morning, the
last to feel fatigue at night. But still, as all the various military
movements had disturbed the posts, and we rode our own horses,
our journey was in reality slow, and seemed to me still slower.

There were but few events in that journey which I need dwell
upon. The party which went through it was divided by their
particular circumstances, by their religion and habits, and each kept
much apart from the other. I, belonging to the higher class of the
land, was separated from the rest both by my rank and by my faith;
and my servants, being Protestants, were, of course, not sought by
the attendants of the Duke of Montpensier. The intendant, indeed, of
the Prince d'Auvergne generally rode by my side, a step farther back,
endeavouring to beguile the way with different stories of the scenes
which he had seen in a long life, and the descriptions of objects
which I had never beheld. He told a tale pleasantly enough, and his
descriptions were vivid and accurate. I showed a sufficient degree of
interest in what he said to flatter his vanity a little, and induce him
to go on.

But he saw that I was deeply melancholy, and sometimes


appeared to suppose that his conversation wearied me, and ceased
it for an hour or two. Thus, however, some little conversation took
place between the Catholics and Protestants; but it was very
different with the Jews, who formed the third division of our party.
They were spoken to, indeed, by both the Catholics and Protestants
from time to time, and were treated with great kindness and with
substantial courtesy, having every protection and assistance given to
them whenever they needed it; but the servants, like their masters,
looked upon them evidently as an inferior race, and kept up as little
communication with them as possible. To ensure that they were well
treated and had nothing to complain of--for the Prince d'Auvergne
had given me authority to regulate such matters on the march--I
generally made Solomon and Miriam come and sit with me for an
hour after our day's journey was over, somewhat to the scandal, I
believe, of good Master Arnon the intendant, who thought it strange
that a French nobleman should permit a Jew to sit in his presence.

By this means an intimacy--if that can be so called which


consisted almost altogether in tokens of respect and reverence on
the one side, and protection on the other--took place between me
and the Jew and his daughter; they clung to me as the only being
that treated them with real kindness, and Miriam used to strive to
amuse me with a thousand little engaging youthful ways: she would
dance to me to the sound of her own singing, which was very sweet,
though in a tongue that I did not understand; and she would play to
me at other times, either upon a small instrument which she called a
cithern, or upon a lute, with a skill and perfection that I had never
heard before. She used to watch my looks, too, as if to see whether
she amused me; but she was too young for idle thoughts to enter
into the head of any one with regard to her; and I do not think I was
of a character, even if she had been two or three years older, to
fancy that she was in love with me, because she had a grateful
regard for me.

The Jew himself, I believe, would have trusted her anywhere with
me, as by this time he would have trusted me with any jewel of his
store; and one evening, when he himself had arrived at the inn,
weary and somewhat unwell, he sent his daughter to amuse me,
and to tell me that he himself had retired to rest. Well might he do
so; and yet the conversation that we had together was as tender
and as full of thrilling interest as it is possible to conceive. I had
been musing sadly over my fate and that of Louise, and my eyes
were buried in my hands when her entrance roused me, so that it
was evident enough to her that she had just recalled me from a
painful dream.

"You are sad, seigneur," she said, drawing a seat close up beside
me, and laying her small, clear, olive hand upon mine. "You are sad,
and you do not tell Miriam what you are sad about."

"Oh, you would not care to hear, Miriam," I replied, "and could do
me no good if you did hear."

"Oh, but I should care to hear," she said, "for I love you very
much, seigneur. I loved you, from the first moment I saw you,
almost as much--no, not so much as I love him."

"Were you going to say your father, Miriam?" I said.

"No," she said, "Not him. I was going to say as Martin Vern." And
the girl coloured a little as she spoke, but added immediately, "But
he loves you too, and told me how kind you had been to him when
he was at the siege of Angoulême, and how you had given him your
hand to help him up into the breach, and how you had carried him
down in your arms when he was wounded, and saved his life, and
been to him like a brother; which, for a lord and a soldier like you,
he thought very kind indeed."

"You seem to have talked very much about me, Miriam," I said.
"When was all this?"

"Oh, it was when we were last in Paris," replied the girl; "when
we were staying at the house of Levi, my father's cousin, who has
become a Christian, you know; and then I would go and see the lady
that you had written to, which he told me about, and who had
written to you again, and sent it to my fathers house at Bordeaux for
the old merchant. And when the Baron de Blancford wanted the
Persian silver brocade for his wife, I went with Martin Vern, that is,
with the old merchant, and saw the young lady too, and spoke with
her in the cabinet behind the great saloon. I told her then that if she
would write you a letter, and send it to Levi's house, it should be
conveyed to you; but I did not think then that I should carry it
myself."

"And was it so the letter came to me?" I said. "I had fancied,
Miriam, that your father had got it when he was in the Protestant
camp."

"Oh, no," she replied; "I carried it all the way in my bosom. And
now I wish you would tell me why you are so sad, and why she
looked so sad too. Perhaps I could do more than you know."

"Oh, no, Miriam," I answered, "You could do no thing, my good


girl. That which makes me sad would need a more skilful surgeon
than you are to cure."

She looked in my face for a moment, as if to see whether I was


speaking plainly or metaphorically, and she then cried, "Ay, now I
understand you. You love her, and she loves you, and they will not
give her to you in marriage."
"Ay, Miriam," I answered, with a sigh, as she came so near the
truth; "and they talk of giving her to another."

"Who to? who to?" cried the girl, eagerly. "I heard something
once which makes me suspect."

"Oh, no," I replied, "You know him not, Miriam. His name is the
Seigneur de Blaye."

"I hate him!" cried the girl, bounding up from her seat as if I had
pronounced some talismanic word; "I hate him! He dared to take
hold of me when my father was gone to get him the money he
wanted from the other room, and asked me if I would go and live
with him; and when I told him no, I would rather be catching-wench
to a butcher's wife, he struck me on the face with his fingers, and
called me a name that I must not speak. I never told my father, or I
believe he would have stabbed him; but I hate him, and I shall ever
hate him. Oh, seigneur!" she continued, turning towards me and
clasping her hands together, "You have been very good and kind
indeed to me and mine, and to all that I ever heard mention your
name. It is such people as you that make us know what good people
there can be; and I will try to show you that there can be gratitude
in a poor little Jewish girl. I told my father, when he knew the people
intended to murder him on the march from Jarnac, that if he would
let me go and speak to you, you would be kind to him. He would not
believe me for a long while; but he said that, if you were, you would
be the first Christian that ever looked upon a Jew as anything but a
dog. My father, however, can be grateful too, seigneur; and, though
you may think that poor little Miriam has no power, yet in this
business she may have more power than you know of."

Our conversation went on for some time; and the girl, young as
she was, spoke with a depth of feeling, a tenderness, an experience
of the world and the world's ways, which was very extraordinary,
mingled as it was with a sort of eager and imaginative wildness of
manner and language, which probably she had acquired in the
somewhat wandering and irregular life to which her father's pursuits
subjected her. I looked upon the hopes and expectations that she
tried to fill me with, of being able to do something in my behalf, as
quite idle and vain; but still the gratitude that she showed was
something pleasant to meet with, and I sent her away with thanks,
and many a kindly speech in return.

At the village of Berny, a short distance from Paris, the Jew, his
daughter, and the innumerable packhorses which followed him, were
to part with their companions of the way, he proceeding to the
capital, and we by a side road to Champigny. He now, however,
considered himself quite safe; and, when I had mounted to depart,
he came up to the side of my horse, followed by Miriam, and prayed
a blessing from God upon my onward journey.

"I have heard from Monsieur Arnon," he said, in a low voice, "that
the estate of Les Bois is yours, and that, for the time, I am only to
call you Monsieur des Bois; but, whether you be at Champigny or at
Les Bois, I hope you will not refuse to let me within your gates; for
you have shown me kindness such as I have seldom found, and
such as I shall never forget."

Thus saying, he kissed my hand after his fashion, and Miriam,


coming up, did the same. There was something in the poor people's
gratitude that made my eyes glisten though they were Jews, and,
bidding them adieu, I rode on. As I turned my horse into the road at
the right, I looked back, and saw that they were standing before the
inn door, gazing after me still.

CHAPTER V.
I was well pleased to arrive at Champigny, and certainly a very
beautiful and charming spot it was; but, of course, the sight of Les
Bois was still more agreeable to me as its proprietor. The chateau
was a small house, built in the antique fashion, but still in the most
perfect repair; certainly not so large as the duke's own mansion at
Champigny, yet large enough for my ambition. It was seated on a
hill, in the midst of fine old woods, from which it derived its name;
and there was an aspect of peace, and calm, and tranquillity, which
was pleasant to the eye and to the heart after the scenes of
anguish, care, and excitement which war had lately presented to my
sight. The interior of the chateau was, as the Prince d'Auvergne had
told me, well furnished, and newly furnished throughout. To my
eyes, indeed, it was splendid; for in those day there was perhaps,
even more than now, a marked difference in the grace, taste, and
execution of everything in the neighbourhood of the capital and in
the remote provinces.

The good intendant of the Prince d'Auvergne insisted upon taking


me all over the chateau, and showing me every hole and corner,
though I was most anxious, I confess, to go into Paris itself, and
take some means for obtaining an interview with Louise. I did not
know well how to explain my inclinations to my worthy companion,
and, to break the subject to him, I made some inquiries regarding
the capital; but, the moment he heard that I had never seen Paris,
nothing would serve him but that I must go there immediately. To
his imagination it was the chief wonder of the world; and, after
descanting upon its merits, beauties, and excellences for half an
hour, he said, "If it were not presuming too far, my lord, I would
propose to accompany you thither immediately, and show you some
of the beauties of the place, though even to notice them all would
require many weeks, I might say months."

I instantly caught at this proposal; and, mounting fresh horses at


Champigny, we rode on into the city, where, giving our horses to the
boys, we proceeded to walk through the streets of the capital. At
any other moment, when my mind was not so occupied by one
predominant subject, everything that I saw would have been a
matter of interest to me. The long ranges of shops, covered over
with awnings to keep the merchandises there exposed from the sun
and the air; the people reading aloud pieces of poetry and satire at
the corners of the streets; the different shows and exhibitions that
attracted the sight at every step, all would have amused, detained,
and interested me; but now my great desire and object was to
discover the abode of the Baron de Blancford, and obtain some
means of communicating with her I loved. The multitude of houses,
and streets, and people that increased upon me at every minute,
confused and puzzled me, and made me fancy the attempt almost
impracticable, not knowing the address, and having no clew in such
a labyrinth as that.

Suddenly, however, I called to mind that, from Miriam's account,


Martin Vern was still in the custom of visiting the house of the Baron
de Blancford, and judging that he, as a great merchant, must be
known to everybody, I asked Arnon the intendant if he could lead
me to his dwelling.

"I do not know him," said the intendant. "Is he a Huguenot?"

"No," I replied, with a smile at the sort of horror that came over
the man's countenance at the very idea of visiting a Huguenot in
Paris. "No, Monsieur Arnon, he is a Catholic, and a great merchant
who has money of mine in his hands."

"Oh, then the case is very different," replied Arnon. "We will
inquire after him immediately." And, entering a large goldsmith's
house by the door close to the shop, he asked for Martin Vern the
merchant.

We had now no difficulty in finding the dwelling, which was up a


flight of steps, and the goods were not exposed in the streets, as
among the ordinary shopkeepers, but spread out in rooms within
doors. Neither good Martin Vern, however, nor his son was to be
found at home; and I left a message, under the name of Des Bois,
asking to see one or both of them at the chateau at Champigny.

Although by this time the days had lengthened, and we were in


the height of summer, it was now time that we should turn our steps
homeward, as the distance we had to go was nearly four leagues;
and during the whole of the following day I waited in anxious
expectation for the appearance of one of the two merchants. No one
came, however, and another and another day succeeded, during
which I scarcely stirred out, and left directions for finding me
whenever I did so. At the end of the third day my patience became
quite exhausted, and on the following morning I begged Arnon to
send off one of the prince's servants, who knew the capital well, to
ask why Master Martin Vern had not been to Champigny. Arnon did
as I directed immediately; and, on bearing me the answer, which
was, that neither Martin Vern nor his nephew had yet returned from
Blois, where they had gone to attend upon the king, added, in order
to put my mind at rest upon the subject which he thought troubled
me, that I might make myself quite easy about the money; for that,
having made inquiries, he found that the house of Martin Vern was
one of the most wealthy and respectable in Paris.

I could not help exclaiming, "Pshaw! it is not the money, my good


friend." And it was evident, from that moment, that Arnon's curiosity
was not a little excited to find out what it could be that I sought with
the merchants, if it was not the money that they owed me. My
determination, however, was now taken to seek the house of the
Baron de Blancford myself; but not all my efforts could discover it,
and it was equally in vain that I attempted to discover the abode of
Solomon Ahar: that he was going to lodge at the house of his cousin
Levi I knew; but his cousin Levi was not to be discovered; and, on
making inquiries concerning him, I was always met by a demand of
"Levi who?" there being a thousand in Paris of the name of Levi, but
all with some surname attached.
In the mean time, the news that daily came in from the scene of
the war was anything but such as to give me gratification. The
feeble attack on Poitiers by the Protestants; the gallant defence of
the young Duke of Guise; the siege of St. Jean d'Angely; the death
of poor Martigue, whom I could not help regretting; the fatal battle
of Moncontour, which, although the defeat of the Protestants was as
complete, and the success of the Catholics as surprising as well need
be, was magnified in Paris in a very great degree; all these things
grieved and pained me, while week after week went by in fruitless
inquiries; and at length, with that sort of scorn of one's self, which is
a true part of misanthropy, for giving a moment's credit to the Jew's
professions of gratitude, I sat me down in bitterness of spirit, and
tried to fancy that I hated the whole human race.

The autumn of the year was now approaching; there could be


little or no doubt that, during the ensuing winter, the young Lord of
Blaye would be free to return to Paris, and pursue the project of
marriage which was held out to him; and the thoughts of poor
Louise, and the privations to which she would be subjected,
tormented me like an army of fiends, and re-enforced themselves by
every power of imagination.

The news that St. Jean d'Angely had been recaptured by the
Protestants, and that the Prince d'Auvergne, who had held it out for
some time against them, had been forced to capitulate for want of
supplies, had reached us some days, when, as I was sitting one
night in the cabinet at Champigny, I heard the clattering of horses'
feet in the courtyard below; and in a moment after, to my great
astonishment, the prince himself entered the room. He embraced
me kindly; and, after a few minutes' conversation upon general
things, remarked that I neither looked well nor happy.

"Come," he said, "De Cerons, tell me what is the cause of this. I


think by this time you might fully confide in your friend."
Before I could answer, one of his officers had entered for some
directions; and, while he gave them, I made up my mind to
unbosom my whole thoughts to him. In the course of the evening I
accordingly did so; and, as was much the character, both of his
father and himself, he heard me fully out with scarcely any
observation or reply.

When I had done completely, however, and he had a complete


view of my past Life and present situation, he said, "There are a
good many strange parts in your tale, De Cerons; but neither you
nor I, I fancy, know so much of the laws as to know whether these
acts of your father and your cousin were legal. However, I see it is
not that which pains you now. It is the matter of your fair cousin;
and I grieve to say, that any news I may have for you is not
calculated to sooth you. No wonder that you have not found them in
Paris, for they are all still at Blois with the court, which gladly keeps
your cousin from joining the admiral and the Prince de Bearn. I saw
them all there at a grand fête given by the king, and talked for some
time with Mademoiselle de Blancford. I talked of you, De Cerons, so
you may suppose that she heard me willingly; and, indeed, it was
impossible to mistake her looks, ay, or even her words when you
were mentioned. If Monsieur de Blaye were to marry her, he would
certainly wed a woman knowing that she loved another man.
However, when the baron came up too, I mentioned you to him also,
and somewhat startled him, I believe, by calling you my dear and
most intimate friend. But he did not look displeased, De Cerons, nor
do I think that he bears any ill-will towards you in his heart, though
he be wayward and moody, and entirely ruled by that worst of all
women, his present wife."

"Was Monsieur de Blaye there?" I demanded, somewhat sharply.

"He was," replied the prince; "and giving himself out rather more
decidedly than Monsieur de Blancford seemed to like, I thought, as
the promised husband of your Louise."
I started up with an exclamation and a threat that I am now
ashamed of.

"Hush, hush," cried the prince, with a reproving smile "do not give
way so, my good friend. By this conduct he is doing more harm than
good with the baron, at least, for I heard him questioned upon the
subject; and, turning upon his heel with a sort of sneer, he replied,
'Monsieur de Blaye is somewhat sanguine in his nature.' However, I
did not forget you, De Cerons, and I told the whole story to my
father, who, of course, is more competent to act than I am. I do not
very well know what my father did; but I see the result, which is,
that Monsieur de Blaye has received a high appointment, which he
solicited more than a year ago, namely; to go with our military
embassy to the court of the sultan. This was done, I am sure, for
the purpose of removing him for a time from the scene, and of
allowing you to have a fair opportunity--"

"But how, my dear prince," I said, "can I have a fair opportunity,


when I am held a prisoner here, unable to advance myself or
signalize my name?"

"You shall hear, De Cerons, you shall hear," replied the prince. "My
father was not a man to forget any point under such circumstances.
He empowered me to offer you your liberty, freely and without
ransom, upon one condition, that you should go join the Prince of
Orange or Prince Ludovic, who are now waging war in the Low
Countries, my father undertaking to obtain for you a high command
in their army. You would thus be enabled to distinguish yourself in a
Protestant cause without bearing arms against your native country.
You would not be farther from Mademoiselle de Blancford nor even
so far, as carrying on this fatal contest in Guyenne or Poitu: you
would be serving the king rather than opposing him, for it is his wish
to give some support to the Prince of Orange; and my father only
requires you to remain in the Low Countries till a peace is
established in the internal affairs of France, which, we trust, will
soon be the case; he, at the same time, promising to you that you
shall have permission to return to France, freed from all restriction,
the moment that it is ascertained that Monsieur de Blaye is about to
return from the East."

"Your father, my lord," I said, "is most noble, generous, and


considerate; and, foreseeing everything that I could desire or wish,
of course, not only prevents the possibility of my refusing such an
offer, but binds me to him by gratitude for ever."

"I told him that such would be the case," replied the prince; "but,
alas! De Cerons, an unexpected event is likely to obstruct all our
proceedings. The embassy was to set off in ten days, and everything
was arranged. Monsieur de Blaye, though looking very much
mortified when he heard his appointment, of course could not refuse
it; and I proposed to stay another week at Blois, and then come and
confer with you regarding the whole affair, when suddenly, one
evening, as I was returning home, I met with three women in the
street, the principal of whom, for the other two were evidently
servants, asked to speak with me without taking off her mask. I had
a number of people about me, but it was close to the door of the
hotel; and, taking her into the porter's chamber, I asked her to
explain what it was she wanted. As soon as we were alone, she took
off the mask and showed me the face of the Jewish girl, Solomon
Ahar's daughter, whom I found talking with you one day at Jarnac.
She told me, at the same time, that she came to speak to me about
you, and seemed to know your whole history, and every secret of
your heart. But to the facts that she told me: they were these: that
Monsieur de Blaye had gone straight to the king, and had asked and
obtained leave to remain six weeks in Paris before he set out, for the
express purpose of concluding his marriage before he went. The
baron, the girl said, had not given his absolute consent, but made it
dependant upon his daughter's inclination; but the baroness had
positively promised that the baron and herself should at least sign
the contract of marriage, even if their daughter, as she said,
preferred waiting till the return of Monsieur de Blaye. Should this
event take place, however, you may consider your Louise as lost to
you for ever; for her father puts it out of his own power to dispose
of her hand or withdraw his consent. The girl was really agitated
about the whole business; and she made some wild exclamations,
declaring that she would stop it if I would get permission for her
father, and some persons who have been trading in partnership with
him, to quit the court, where they have been detained for several
weeks in regard to some negotiations now going on for loans of
money. This was easily done, as the thing was nearly concluded;
and, as soon as I had seen this arranged, I came away hither, with
my father's consent, to consult with you in regard to what can be
done."

"You are most kind, most kind," I said. "How can I ever thank
you, D'Auvergne? but, alas! I fear that I am doomed to misery and
to despair."

"Not so, not quite so," replied the prince. "As I came hither from
Blois I considered the matter maturely; and we have to recollect that
you, as a near relation of the lady, have every right to oppose the
signature of the contract, if you think fit so to do. In the first place,
you must make perfectly sure that she herself is brought to yield by
no means of persuasion or intimidation that can be used towards
her; and, at the same time, things must be suffered to take their
course till the contract is on the very eve of being signed by the
baron. You must then, by some form of law which I can inquire into,
give him formal intimation of your opposition, which will
consequently be brought before the courts. The fact is, you are
fighting for delay; for your opposition against her own father cannot,
of course, be successful, and you may perhaps be fined in some
small sum for having made it; but, long before that time, this young
libertine, for such he is, must be in Constantinople, and the matter
secure."

I mused for a moment in thought, the intensity of which


approached to agony: I saw before me the blasting of all my best
hopes, and I felt at that moment, more than I had ever yet done,
not only how deeply, how truly, how ardently I loved poor Louise,
but how completely and thoroughly, without my knowing it, her
image had been mingled with all my dreams and aspirations; how
intimately the thought of winning her had mingled with all my
motives for energy, exertion, and endeavour. I felt at that moment
that to lose her was to lose my whole hold on life--my whole
inducement to struggle onward in the course I was pursuing. There
was no scheme so wild, so improbable, so daring, that I would not
have undertaken at that moment to frustrate the schemes that could
but tend to her misery and my own: there was no step so dangerous
to myself, even had it been planted on the crumbling edge of an
open grave, that I would not have taken to make her mine; yet, as I
mused, I could not help thinking--I may say I could not help being
convinced--that the scheme of the Prince d'Auvergne was likely to be
frustrated by some impetuous act of the Baron de Blancford.

"With many men," I said, "The whole might succeed admirably;


but I, who know his determined and passionate character well, feel
perfectly certain that, if there be a way of frustrating us, he will find
it."

"I see none," replied the prince dauphin, "if we can by any means
ensure that the signing of the contract is put off to the last moment.
However, De Cerons, the whole party are coming to Paris
immediately; the Jew, and the merchants who are with him, will
most probably arrive to-morrow morning, and your cousin, with his
train, on the morning after. Obstacles of various kinds, I am sure,
will keep this Monsieur de Blaye for a day or two after them; and let
us do the best we can in the mean while. At all events, we shall gain
some intelligence; and what I should propose is, to ride out on the
day after to-morrow on the road to meet them, and, bringing them
to your chateau of Les Bois, give the baron a little entertainment and
repose ere he goes into Paris."

I smiled at the thought, saying, "I much fear, my excellent friend,


that you will find the baron would neither accept the invitation nor
thank the giver."

"Pshaw! De Cerons," replied the prince; "You are older than I am


in years, but younger a great deal in experience of the world. The
baron undervalued and undervalues you simply because he thought
and thinks you poor. He thought you the creature of his bounty: he
will now come here and find you the creature of your own sword,
renowned in arms, independent in fortune, and seeking no aid from
him or any man. His view will be quite different now, depend upon
it. As for the arrangements of your little regale, leave that all to me:
you, on your part, cast off the rough and somewhat negligent
apparel in which your despondency has brought you to remain, trim
your beard, bring forth your best brocade, and look as gay and
gallant as if you were going into the tiltyard."

It is needless to pause upon all the minute incidents at this time.


Martin Vern and his nephew had scarcely arrived in Paris before they
were at Champigny, bringing with them little Miriam, who seemed to
have her own will with all of them. Not knowing that the prince was
there, I found that his high rank and connexion with the royal blood
of France somewhat abashed and confounded the two merchants.
He, on his part, did not so much unbend as perhaps I had expected;
but he treated them kindly and without haughtiness, though with
dignity: but he soon left them alone with me; and a few words
showed me that both the elder and the younger Martin Vern, what
between all they had observed of the conduct of myself and Louise,
and the information of the young Jewess, were perfectly aware of
how we stood towards each other, and took a kindly interest in my
fate. Miriam, for her part, seemed to me to have gone quite mad.
She said it was just what she had wished, all that she could wish,
that had happened and would happen, and seemed quite as happy
and elevated as I was bereaved and depressed. Her conduct
somewhat annoyed me; and, after some short conversation about
the money, which I still determined to leave in the hands of Martin
Vern, I saw them depart without any effort to detain them.
On the following morning, with a splendid train, comprising at
least twenty persons, dressed, as far, at least, as the prince himself
was concerned, in the height of the then existing fashion,
D'Auvergne and myself set out upon the road towards Blois; and,
after riding for some eight miles on a fine autumnal morning, we
came within sight of a large party advancing slowly, which proved,
as we expected, to be that of the Baron de Blancford. Putting our
spurs to our horses' sides, we rode up at a quick pace, and the
baron thought fit, in those dangerous times, to halt his troop upon
seeing such a body of horsemen coming down upon him. His
surprise, when he beheld me and the prince dauphin, however, I
shall not easily forget: nor need I say much more of this interview,
as far as it regarded him, than that I readily perceived that the
prince's view of the baron's character was correct, and that I had
grown wonderfully in his opinion since I had ceased to need his
assistance. The fête at Les Blois was accepted at once; but it
required some persuasion on the part of the prince dauphin to make
him believe that I was really the lord of the estate to which he was
now conducted. The baroness, on her part, gazed at me with some
surprise, and throughout the day I forced myself to show her as
much civility and attention as possible: but there were some others
in that group where there were deeper interests at work. Louise met
me with eyes full of deep and intense affection, and a manner from
which the sudden surprise seemed to have taken all confidence, but
not all tenderness; and her two brothers, whom I had not seen for
more than a year, clung round me as if their affections had found no
object since we parted.

In the course of the day I had an opportunity of speaking more


than once with Louise, and in a few brief words I gave her an
account of all that was taking place in our plans and purposes. Her
only reply was by words of affection that could never pass from my
heart, and by the solemn assurance that no power on earth should
ever make her consent to become the wife of the Seigneur de Blaye.
The day went over, in short, as brightly as it was possible under such
circumstances; and, during the three weeks that followed,
everything seemed to combine to favour the plan which the prince
had laid down for me.

It fortunately occurred that I never met with the Seigneur de


Blaye during the whole of that period. Such a meeting could have
been followed but by one result, and that result must have been
fatal to myself; for it must be remembered that I was a Protestant
and he a Catholic, and the survivor in a duel, under such
circumstances, could only expect death. My visits to the hotel of
Monsieur de Blancford were generally short; for I soon saw that, if I
did not find Louise when first I went, means were taken to prevent
her appearing while I was there. The baron, however, was all
condescension, and declared that he was proud of his cousin. The
baroness, on her part, seemed to make herself somewhat more
tender and amiable than was needful.

But, at length, the fatal minute, which was to dissipate such a


state of things altogether, arrived; and, just on the day preceding
that which was fixed ultimately as the last for Monsieur de Blaye's
stay in Paris, a messenger from the baron invited me, in courteous
terms, to come and witness his signature of the contract of marriage
between my cousin Louise and the Seigneur de Blaye. We had
already ordered a notary to prepare in due form my opposition to
the baron's signature, upon the plea both of relationship and never
having been consulted, and of having a prior claim to the hand of
Mademoiselle de Blancford. The note requested the honour of the
prince dauphin's company on the same occasion as my friend; and,
on reading it, he exclaimed, "Oh, certainly, certainly! I will go, De
Cerons, and, not only that, but we will take a sufficient body of
retainers with as to guard against all chances, and we will have
likewise our own notary to take act of your opposition."

All this being settled, we set out, and reached the house at the
hour appointed. I was somewhat surprised to find going up the
stairs good Martin Vern, accompanied by a boy carrying several
packages, and another man not so burdened. On entering the great
saloon, we found the baron with Monsieur de Blaye, the baroness,
and some of her kindred, both male and female; besides whom, the
room contained Louise, with the tears already in her eyes, and
several notaries and lawyers. Immediately on our entrance, Monsieur
de Blaye came forward with his hand extended towards me, as if
imagining that we were the best possible friends; but I drew myself
up and bowed stiffly, and he fell back with a heavy frown.

The baron looked somewhat surprised, but the presence of the


Prince d'Auvergne acted as a restraint upon him, and he welcomed
his distinguished guest with courtesy, if not with so free and
unrestrained a demeanour as usual. He looked two or three times
suspiciously at the notary who accompanied us, and who was one of
the most distinguished of his class, and received far more attention
and marks of reverence from his brethren than either D'Auvergne or
I wished or expected. Sweetmeats and some choice wines, however,
were handed round before the destined explosion began; but at
length the baron, prefacing the matter by a little eulogy upon
Monsieur de Blaye, which had wellnigh made some of those who
knew him laugh, directed the contract to be read.

That document began by setting forth that, "as an alliance was


intended at a future period between the Seigneur de Blaye and
Mademoiselle de Blancford, it had been judged expedient that the
Baron de Blancford should sign the contract to that effect previous
to the departure of the said seigneur for foreign lands; and
therefore," &c. It went on to express the usual agreements in such
cases, but took care to omit the express consent of the bride, and
also made no provision for the freedom of her religion. She was
declared heiress of the lands of Blancford and Cerons in the event of
her two brothers' death without children; and the baron promised
with her a dowry which to me, who knew his habits of expense, and,
in some degree, the true nature of his property, seemed enormous.

As soon as the whole was read, he took the pen in his hand to
sign, and I could see my poor Louise clasp her two hands together
and raise her eyes to me with a look of anguish and supplication.

At that moment, however, the notary we had brought, who had


been consulting with the others, stepped forward, and laid his hand
upon the spot where the baron was about to sign, saying,

"Your pardon, Monsieur le Baron de Blancford; I think that


Monsieur de Cerons has something to say on this matter, and a short
paper to read, to which I beg your attention, and of which,
gentlemen, you will all bear witness."

He then handed me the paper, saying, at the same time in a


whisper, "Neither more nor less."

I followed his directions to the letter, and read the paper of


objections through without pausing. When I came to the end,
however, and found there stated that I would sustain my right upon
the grounds therein stated, and upon several other legal grounds of
objection, to all and sundry parts and clauses of the said contract, in
warranty of which I produced as my surety the Prince Dauphin
d'Auvergne, I laid, I know not well why, considerable emphasis upon
the words "several other objections."

At the same time, I remarked the baron turn very pale; but he
recovered himself immediately, and, with an angry gesture,
exclaimed to the notary, who had continued to hold his hand on the
paper, "Remove your hand, Master Jean! I will sign it at all risks."

"It is useless, Monsieur le Baron," replied one of the lawyers;


"after this solemn protest in due and legal form, no act that you can
do in this matter is lawful until the Parliament shall have considered
the matter to render justice therein."

"But I shall take care to render justice to myself," exclaimed


Monsieur de Blaye, advancing towards me furiously: "We all know
that you lawyers love to see all things plunged into the quagmire of
the courts, round the edges of which you toads sit and croak at
leisure; but gentlemen have a shorter means of settling such
transactions, and to such, Monsieur de Cerons, do I appeal. Nor, sir,
must there be delay of any kind. Tomorrow I depart from Paris; the
rest of this day is our own."

"Oh! no, no!" cried the voice of Louise, while, with her arms
extended towards me as if for protection, she ran forward.

But, ere she reached me, she fell fainting on the ground, and the
marchioness, with other ladies present, prevented my approach. All
was now a scene of confusion; the gentlemen of the party came
forward, each talking, each offering his opinion, towards the spot
where De Blaye and myself stood face to face, and the baron
seemed divided between us and his daughter, for whom I saw that
he was not without feeling, though he struggled not to show it.

In the midst of this Babel, however, the clear, fine-toned voice of


the prince dauphin suddenly made itself heard, saying,

"Your pardon, gentlemen, your pardon! I have one word to say;


but that one word is an important one, which must settle all this
matter between my excellent good acquaintance Monsieur de Blaye
and my friend Monsieur de Cerons."

All were instantly silent except De Blaye himself, who repeated


more than once, in a tone of authority, to keep silence, and let the
prince speak. When he stopped and bowed, D'Auvergne went on:
"What I have to say, De Cerons, is, that you will be good enough to
remember you are my father's prisoner, and therefore can lie under
a challenge from no man. Monsieur de Blaye, I must call upon you
to retract your challenge, as no man of honour can offer one to a
gentleman incapable of accepting it."

De Blaye, who was both really enraged and really brave, blustered
a good deal at this notification, and said something rather offensive
to the prince about his father the duke being afraid of losing my
ransom. D'Auvergne answered coolly, however, saying, "That is not
his fear or mine, Monsieur de Blaye; but our fear might well be that
the Catholic army might lose a very tolerable soldier and brave
young gentleman in yourself; because, as we all know, Monsieur de
Cerons would kill you like a rat. Come, De Cerons, I must beg you to
accompany me."

If the first part of the prince's speech had pleased Monsieur de


Blaye, and made him simper and look modest, the unpleasant simile
in the latter part caused him to swell and colour with anger. But
D'Auvergne took no farther notice; the fact of my not being at liberty
was without reply, and, after one look to my poor Louise, I quitted
the room. Martin Vern was at the door, and to him the prince
whispered a word as we passed. The merchant made a low
inclination of the head, and, mounting our horses, we rode away.

CHAPTER VI.

I had remarked particularly, in the painful interview just past, that


neither good old La Tour, nor the two dear boys who were daily
growing up more and more like their angel of a mother, had been
present; but I learned afterward that many painful efforts had been
made to induce Louise to wed a man she abhorred, and that her
brothers had broken forth with somewhat rash expressions of
indignation, while La Tour had remonstrated in milder but as forcible
terms. The consequence had been, that the baron had sent them all
three to some distance, and probably was not a little glad, when the
scene terminated as it did, that he had taken that precaution. I
received from him that night a threatening note, but it was so
worded as evidently to court a lengthened reply; and, after
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookmasss.com

You might also like