JavaScript A Beginner's Guide Fifth Edition John Pollock instant download
JavaScript A Beginner's Guide Fifth Edition John Pollock instant download
or textbooks at https://ebookmass.com
_____ Follow the link below to get your download now _____
https://ebookmass.com/product/javascript-a-beginners-guide-
fifth-edition-john-pollock/
https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-pollock/
https://ebookmass.com/product/150-ecg-cases-fifth-edition-john-
hampton/
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/
https://ebookmass.com/product/creating-effective-teams-a-guide-for-
members-and-leaders-null-fifth/
https://ebookmass.com/product/technical-analysis-explained-fifth-
edition-the-successful-investors-guide/
https://ebookmass.com/product/learning-web-design-a-beginners-guide-
to-html-css-javascript-and-web-graphics-5th-edition-ebook-pdf/
https://ebookmass.com/product/english-file-beginner-teachers-guide-
for-speakers-of-spanish-fourth-edition-christina-latham-koenig/
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.
TERMS OF USE
1 Introduction to JavaScript
What You Need to Know
Basic HTML and CSS Knowledge
Basic Text Editor and Web Browser Knowledge
Which Version?
Client-Side and Server-Side Programming
Beginning with JavaScript
Prototype-Based
Interpreted Language
Numerous Environments
Putting It All Together
Online Resources
Try This 1-1: Use JavaScript to Write Text
Chapter 1 Self Test
3 Using Variables
Understanding Variables
Why Variables Are Useful
Variables as Placeholders for Unknown Values
Variables as Time-Savers
Variables as Code Clarifiers
Defining Variables for Your Scripts
Declaring Variables
Assigning Values to Variables
Naming Variables
Understanding Data Types
Number
String
Boolean
Null
Undefined
Symbol
Object
Try This 3-1: Declare Variables
Using Variables in Scripts
Making a Call to a Variable
Adding Variables to Text Strings
Writing a Page of JavaScript
Creating the Framework
Defining the Variables
Adding the Commands
Modifying the Page
Try This 3-2: Create an HTML Page with JavaScript
Chapter 3 Self Test
4 Using Functions
What a Function Is
Why Functions Are Useful
Structuring Functions
Declaring Functions
Defining the Code for Functions
Naming Functions
Adding Arguments to Functions
Adding Return Statements to Functions
Calling Functions in Your Scripts
Script Tags: Head Section or Body Section
Calling a Function from Another Function
Calling Functions with Arguments
Calling Functions with Return Statements
Other Ways to Define Functions
Try This 4-1: Create an HTML Page with Functions
Scope/Context Basics
Global Context
Function Context
Block Context
Try This 4-2: Write Your Own Functions
Chapter 4 Self Test
5 JavaScript Operators
Understanding the Operator Types
Understanding Arithmetic Operators
The Addition Operator (+)
The Subtraction Operator (–)
The Multiplication Operator (*)
The Division Operator (/)
The Modulus Operator (%)
The Increment Operator (++)
The Decrement Operator (– –)
The Unary Plus Operator (+)
The Unary Negation Operator (–)
The Exponentiation Operator
Understanding Assignment Operators
The Assignment Operator (=)
The Add-and-Assign Operator (+=)
The Subtract-and-Assign Operator (–=)
The Multiply-and-Assign Operator (*=)
The Divide-and-Assign Operator (/=)
The Modulus-and-Assign Operator (%=)
The Exponent-and-Assign Operator (**=)
Try This 5-1: Adjust a Variable Value
Understanding Comparison Operators
The Is-Equal-To Operator (==)
The Is-Not-Equal-To Operator (!=)
The Strict Is-Equal-To Operator (===)
The Strict Is-Not-Equal-To Operator (!==)
The Is-Greater-Than Operator (>)
The Is-Less-Than Operator (<)
The Is-Greater-Than-or-Equal-To Operator (>=)
The Is-Less-Than-or-Equal-To Operator (<=)
Understanding Logical Operators
The AND Operator (&&)
The OR Operator (||)
The NOT Operator (!)
The Bitwise Operators
Special Operators
Understanding Order of Operations
Try This 5-2: True or False?
Chapter 5 Self Test
7 JavaScript Arrays
What Is an Array?
Why Arrays Are Useful
Defining and Accessing Arrays
Naming an Array
Defining an Array
Accessing an Array’s Elements
Using the length Property and Loops
Changing Array Values and Changing the Length
Try This 7-1: Use Loops with Arrays
Array Properties and Methods
Properties
Methods
Nesting Arrays
Defining Nested Arrays
Loops and Nested Arrays
Try This 7-2: Nested Arrays Practice
Chapter 7 Self Test
8 Objects
Defining Objects
Creating Objects
Naming
Single Objects
Try This 8-1: Create a Computer Object
Object Structures
Constructor Functions
Using Prototypes
The class Keyword
Helpful Statements for Objects
The for-in Loop
The with Statement
Try This 8-2: Practice with the Combination
Constructor/Prototype Pattern
Understanding Predefined JavaScript Objects
The Navigator Object
The History Object
Chapter 8 Self Test
9 The Document Object
Defining the Document Object
Using the Document Object Model
Using the Properties of the Document Object
Collections
The cookie Property
The dir Property
The lastModified Property
The referrer Property
The title Property
The URL Property
The URLUnencoded Property
Using the Methods of the Document Object
The get Methods for Elements
The open() and close() Methods
The write() and writeln() Methods
Using DOM Nodes
DOM Node Properties
DOM Node Methods
Try This 9-1: Add a DOM Node to the Document
Creating Dynamic Scripts
Styles in JavaScript
Simple Event Handling
Coding a Dynamic Script
Try This 9-2: Try Out Property Changes
Chapter 9 Self Test
10 Event Handlers
What Is an Event Handler?
Why Event Handlers Are Useful
Understanding Event Handler Locations and Uses
Using an Event Handler in an HTML Element
Using an Event Handler in the Script Code
Learning the Events
The Click Event
Focus and Blur Events
The Load and Unload Events
The Reset and Submit Events
The Mouse Events
The Keyboard Events
Try This 10-1: Focus and Blur
Other Ways to Register Events
The addEventListener() Method
The attachEvent() Method
The Event Object
DOM and Internet Explorer: DOM Level 0 Registration
Using event with Modern Event Registration
Properties and Methods
Event Information
Try This 10-2: Using addEventListener()
Creating Scripts Using Event Handlers
Show Hidden Content
Change Content
Custom Events
Chapter 10 Self Test
11 Introduction to Node.js
Introducing Node.js
Installing Node.js
Check for a Current Installation
Install Node.js
Write a “Hello World” Script
Using Node Modules
Using Native Node Modules
Asynchronous Execution
Non-Native Modules
Try This 11-1: Use a Custom Module
Installing a Database
Database Options
Install PostgreSQL
Create a Database Using pgAdmin
Try This 11-2: Test Some SQL Queries
Creating a Web Server
Chapter 11 Self Test
13 Handling Strings
Introduction to the String Object
The String Object
The String Literal
What’s the Difference?
Using the Properties and Methods of the String Object
The length Property
Methods of the String Object
Try This 13-1: Use indexOf() to Test an Address
Using Cookies
Setting a Cookie
Reading a Cookie
Try This 13-2: Remember a Name
Using Regular Expressions
Creating Regular Expressions
Testing Strings Against Regular Expressions
Adding Flags
Creating Powerful Patterns
Grouping Expressions
The replace(), match(), matchAll(), and search() Methods
More Information
Continuing Project
Chapter 13 Self Test
14 Browser-Based JavaScript
Window: The Global Object
Using the Properties of the Window Object
The closed Property
The frames Property
The innerWidth and innerHeight Properties
The length Property
The location Property
The name Property
The opener Property
The parent, self, and top Properties
The status and defaultStatus Properties
Try This 14-1: Use the location and innerWidth Properties
Using the Methods of the Window Object
The alert(), prompt(), and confirm() Methods
The print() Method
The setInterval() and clearInterval() Methods
The setTimeout() and clearTimeout() Methods
Try This 14-2: Use the setTimeout() and confirm() Methods
The Main Window and New Windows
The Tale of Pop-up Windows
Opening New Windows
Closing New Windows
Moving, Resizing, and Scrolling New Windows
The resizeBy() and resizeTo() Methods
The scrollBy() and ScrollTo() Methods
Working with Images
Rollovers
JavaScript and Frames
Purpose of Frames
Accessing Frames
Breaking Out of Frames
Using iFrames
Chapter 14 Self Test
Index
Acknowledgments
• Key Skills & Concepts Each chapter begins with a set of key
skills and concepts that you will understand by the end of the
chapter.
• Ask the Expert The Ask the Expert sections present commonly
asked questions about topics covered in the preceding text, with
responses from the author.
• Try This These sections get you to practice what you have
learned using a hands-on approach. Each Try This will have you
code a script through step-by-step directions on what you need to
do to in order to accomplish the goal. You can find solutions to
each project on the McGraw-Hill Professional Web site at
www.mhprofessional.com/computingdownload.
• Notes, Tips, and Cautions These elements call your attention
to noteworthy statements that you will find helpful as you move
through the chapters.
• Code Code listings display example source code used in scripts
or programs.
• Callouts Callouts display helpful hints and notes about the
example code, pointing to the relevant lines in the code.
• Self Test Each chapter ends with a Self Test, a series of 15
questions to see if you have mastered the topics covered in the
chapter. The answers to each Self Test can be found in the
appendix.
That is it! You are now familiar with the organization and special
features of this book to start your journey through JavaScript. If you
find that you are stuck and need help, feel free to contact me with
your questions. To contact me, you can reach me on LinkedIn
(https://www.linkedin.com/in/john-pollock-82a2b074) or you can
find me on Twitter (@ScripttheWeb).
Now it is time to learn JavaScript. Get ready, get set, and have
fun!
Chapter 1
Introduction to JavaScript
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.
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.
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 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
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.
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.
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.
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.
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.
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
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.
Online Resources
To find additional information online to help you with JavaScript, here are
some useful resources:
pr1_1.html
Step by Step
1. Copy and paste the code shown here into your text editor:
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.)
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.
<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.
NOTE
If you are using XHTML, set this attribute using defer=“defer”.
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+.
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.
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.
*****
*****
*****
ebookmasss.com