SlideShare a Scribd company logo
1 of 68
HTML5, CSS3,
and JavaScript
6th Edition
Getting Started with JavaScript
Tutorial 9
Carey
XPXPXPXPXPObjectives
• Insert a script element
• Write JavaScript comments
• Display an alert dialog box
• Use browser debugging tools
• Reference browser and page objects
• Use JavaScript properties and methods
2New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPObjectives (continued)
• Write HTML code and text content into a page
• Work with a Date object
• Use JavaScript operators
• Create a JavaScript function
• Create timed commands
3New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPServer-Side and Client-Side Programming
• Server-side programming: Program code runs
from the server hosting the website
• Advantage
– Connects a server to an online database containing
information not directly accessible to end users
• Disadvantages
– Use server resources and requires Internet access
– Long delays in cases of system over-load
4New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Server-Side and Client-Side Programming
(continued 1)
5New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Server-Side and Client-Side Programming
(continued 2)
• Client-side programming: Programs run on the
user’s computer using downloaded scripts
with HTML and CSS files
• Distributes load to avoid overloading of
program-related requests
• Client-side programs can never replace server-
side programming
6New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Server-Side and Client-Side Programming
(continued 3)
7New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPThe Development of JavaScript
• JavaScript is a programming language for
client-side programs
• It is an interpreted language that executes a
program code without using an application
• Compiler is an application that translates a
program code into machine language
• JavaScript code can be directly inserted into or
linked to an HTML file
8New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with the script Element
• JavaScript code is attached to an HTML file
using the script element
<script src=”url”></script>
where url is the URL of the external file
containing the JavaScript code
• An embedded script can be used instead of an
external file by omitting the src attribute
<script>
code
</script>
9New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPLoading the script Element
• script element can be placed anywhere
within an HTML document
• When a browser encounters a script, it
immediately stops loading the page and begins
loading and then processing the script
commands
• async and defer attributes can be added to
script element to modify its sequence of
processing
10New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPLoading the script Element (continued)
• async attribute tells a browser to parse the
HTML and JavaScript code together
• defer attribute defers script processing until
after the page has been completely parsed
and loaded
• async and defer attributes are ignored for
embedded scripts
11New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPInserting the script Element
12New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPCreating a JavaScript Program
• JavaScript programs are created using a
standard text editor
• Adding Comments to your JavaScript Code
– Comments help understand the design and
purpose of programs
– JavaScript comments can be entered on single or
multiple lines
13New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Creating a JavaScript Program
(continued 1)
– Syntax of a single-line comment is as follows:
// comment text
– Syntax of multiple-line comments is as follows:
/*
comment text spanning
several lines
*/
14New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Creating a JavaScript Program
(continued 2)
15New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Creating a JavaScript Program
(continued 3)
• Writing a JavaScript Command
– A command indicates an action for a browser to
take
– A command should end in a semicolon
JavaScript command;
16New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Creating a JavaScript Program
(continued 4)
17New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Creating a JavaScript Program
(continued 5)
• Understanding JavaScript Syntax
– JavaScript is case sensitive
– Extra white space between commands is ignored
– Line breaks placed within the name of a JavaScript
command or a quoted text string cause an error
18New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPDebugging your Code
• Debugging: Process of locating and fixing a
programming error
• Types of errors
– Load-time errors – occur when a script is first
loaded by a browser
– Run-time errors – occur during execution of a
script without syntax errors
– Logical errors – are free from syntax and
executable mistakes but result in an incorrect
output
19New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPOpening a Debugger
• Debugging tools locate and fix errors in
JavaScript codes
• Shortcut to open a debugging tool is F12 key
• The tools can also be opened by selecting
Developer Tools from the browser menu
20New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPInserting a Breakpoint
• A useful technique to locate the source of an
error is to set up breakpoints
• Breakpoints are locations where a browser
pauses a program to determine whether an
error has occurred at that point during
execution
21New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPInserting a Breakpoint (continued)
22New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPApplying Strict Usage of JavaScript
• Strict mode enables all lapses in syntax to
result in load-time or run-time errors
• To run a script in strict mode, add the
following statement to the first line of the file:
“use strict”;
23New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPIntroducing Objects
• Object: Entity within a browser or web page
that has properties and methods
• Properties: Define objects
• Methods: Act upon objects
• JavaScript is an object-based language that
manipulates an object by changing one or
more of its properties
24New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPIntroducing Objects (continued 1)
• Types of JavaScript objects
– Built-in objects – intrinsic to JavaScript language
– Browser objects – part of browser
– Document objects – part of web document
– Customized objects – created by a programmer to
use in an application
• Browser object model (BOM) and document
object model (DOM) organize browser and
document objects in hierarchical structures,
respectively
25New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPIntroducing Objects (continued 2)
26New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPObject References
• Objects within the object hierarchy are
referenced by their object names such as
window, document, or navigator
• Objects can be referenced using the notation
object1.object2.object3 ...
where object1 is at the top of the hierarchy,
object2 is a child of object1, and so on
27New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPReferencing Object Collections
• Object collections: Objects organized into
groups
• To reference a specific member of an object
collection, use
collection[idref]
or collection.idref
where collection is a reference to the object
collection and idref is either an index number
or the value of id attribute
28New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Referencing Object Collections
(continued)
29New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Referencing an Object by ID and
Name
• An efficient approach to reference an element
is to use its id attribute using the expression
document.getElementById(id)
where id is the value of id attribute
30New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Changing Properties and Applying
Methods
• Object Properties
– Object property is accessed using
object.property
where object is a reference to an object and
property is a property associated with that
object
– Read-only properties cannot be modified
31New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Changing Properties and Applying
Methods (continued)
• Applying a Method
– Objects can be modified using methods
– Methods are applied using the expression
object.method(values)
where object is a reference to an object,
method is the name of the method applied to the
object, and values is a comma-separated list of
values associated with the method
32New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWriting HTML Code
• HTML code stored within a page element is
referenced using
element.innerHTML
where element is an object reference to an
element within a web document
33New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWriting HTML Code (continued 1)
• HTML code stored within a page element is
referenced using
element.innerHTML
where element is an object reference to an
element within a web document
• For example,
/* Display the current date and time */
document.getElementById(“dateNow”).inne
rHTML = “m/d/y<br />h:m:s”;
34New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWriting HTML Code (continued 2)
35New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWriting HTML Code (continued 3)
36New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with Variables
• Variable: Named item in a program that stores
a data value
• Declaring a Variable
– Introduced into a script by declaring the variable
using the var keyword
var variable = value;
where variable is the name assigned to the
variable and value is the variable’s initial value
37New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with Variables (continued)
• Conditions to assign variable names in
JavaScript
– First character must be either a letter or an
underscore character ( _ )
– The characters after the first character can be
letters, numbers, or underscore characters
– No spaces
– No using names that are part of JavaScript
language
38New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPVariables and Data Types
• Data type: Type of information stored in a
variable
• Supported data types
– Numeric value
– Text string
– Boolean value
– Object
– null value
39New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPVariables and Data Types (continued)
• Numeric value: Any number
• Text string: Group of characters enclosed
within either double or single quotation
marks
• Boolean value: Indicates the truth or falsity of
a statement
40New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPVariables and Data Types (continued 1)
• Object – Simplifies code by removing the
need to rewrite complicated object
references
• null value – Indicates that no value has yet
been assigned to a variable
41New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with Date Objects
• Date object: Built-in JavaScript object used to
store information about dates and times
42New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with Date Objects (continued 1)
43New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with Date Objects (continued 2)
44New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPSetting Date and Time Values
45New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with Operators and Operands
• Operator: Symbol used to act upon an item or
a variable within an expression
• Operands: Variables or expressions that
operators act upon
• Types of operators
– Binary operators – require two operands in an
expression
46New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Working with Operators and Operands
(continued)
– Unary operators – require only one operand
oIncrement operator (++) – increases the value of an
operand by 1
oDecrement operator (--) – decreases the value of an
operand by 1
47New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPUsing Assignment Operators
• Assignment operator: Assigns a value to an
item
48New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with the Math Object
• Math object: Built-in object used to perform
mathematical tasks and store mathematical
values
• Syntax to apply a Math method is
Math.method(expression)
where method is the method applied to a
mathematical expression
49New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Working with the Math Object
(continued 1)
50New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Working with the Math Object
(continued 2)
51New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPUsing Math Constants
• Math functions refer to built-in constants
stored in JavaScript Math object
• Syntax to access mathematical constants is
Math.CONSTANT
where CONSTANT is the name of one of the
mathematical constants supported by Math
object
52New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPUsing Math Constants (continued)
53New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPWorking with JavaScript Functions
• Function: Collection of commands that
performs an action or returns a value
• A function name identifies a function and a set
of commands that are run when the function
is called
• Parameters: Variables associated with the
function
54New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Working with JavaScript Functions
(continued)
• General syntax of a JavaScript function is
function function_name(parameters){
commands
}
where,
– function_name is the name of the function
– parameters is a comma-separated list of
variables used in the function
– commands is the set of statements run by the
function
55New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPCalling a Function
56New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Creating a Function to Return a
Value
• Functions return values using return
statement
function function_name(parameters){
commands
return value;
}
where value is the calculated value that is
returned by the function
57New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPRunning Timed Commands
• Methods to update the current and the
remaining time constantly
– Time-delayed commands
– Timed-interval commands
• Working with Time-Delayed Commands
– Time-delayed commands: JavaScript commands
run after a specified amount of time has passed
58New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPRunning Timed Commands (continued 1)
– Time delay is defined using
setTimeout(“command”, delay);
where command is a JavaScript command and
delay is the delay time in milliseconds before a
browser runs the command
• Running Commands at Specified Intervals
– The timed-interval command instructs browsers to
run a command repeatedly at a specified interval
59New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPRunning Timed Commands (continued 2)
– Timed-interval commands are applied using
setInterval() method
setInterval(“command”, interval);
where interval is the interval in milliseconds
before the command is run again
60New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPRunning Timed Commands (continued 3)
61New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Controlling How JavaScript Works
with Numeric Values
• Handling Illegal Operations
– Mathematical operations can return results that
are not numeric values
– JavaScript returns NaN if an operation does not
involve only numeric values
62New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Controlling How JavaScript Works
with Numeric Values (continued)
– isNaN() function returns a Boolean value of
true if the value is not numeric and false if
otherwise
– Infinity value is generated for an operation
whose result is less than the smallest numeric
value and greater than the largest numeric value
supported by JavaScript
63New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPDefining a Number Format
• JavaScript stores a numeric value to 16
decimal places of accuracy
• The number of digits displayed by browsers is
controlled using toFixed() method
value.toFixed(n)
where value is the value or variable and n is
the number of decimal places displayed in the
output
64New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXPDefining a Number Format (continued)
• toFixed() limits the number of decimals
displayed by a value and converts the value
into a text string
• toFixed() rounds the last digit in an
expression rather than truncating it
65New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Converting Between Numbers and
Text
• + operator adds a text string to a number
• For example,
testNumber = 123; // numeric value
testString = testNumber + “”; // text
string
where + operator concatenates a numeric
value with an empty text string resulting in a
text string
66New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Converting Between Numbers and Text
(continued 1)
• parseInt() function extracts the leading
integer value from a text string
• It returns the integer value from the text string
by discarding any non-integer characters
• Example,
parseInt(“120.88 lbs”); // returns 120
parseInt(“weight equals 120 lbs”); //
returns NaN
67New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XPXPXPXPXP
Converting Between Numbers and Text
(continued 2)
68New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

More Related Content

What's hot

Presentation about html5 css3
Presentation about html5 css3Presentation about html5 css3
Presentation about html5 css3Gopi A
 
CSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive DesignCSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive DesignZoe Gillenwater
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScriptBala Narayanan
 
Bootstrap - Basics
Bootstrap - BasicsBootstrap - Basics
Bootstrap - BasicsFirosK2
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
Introduction to JSX
Introduction to JSXIntroduction to JSX
Introduction to JSXMicah Wood
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Hatem Mahmoud
 
Dynamic HTML (DHTML)
Dynamic HTML (DHTML)Dynamic HTML (DHTML)
Dynamic HTML (DHTML)Himanshu Kumar
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Adnan Sohail
 
HTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts BasicsHTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts BasicsSun Technlogies
 
Bootstrap 5 whats new
Bootstrap 5   whats newBootstrap 5   whats new
Bootstrap 5 whats newSandun Perera
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media QueriesRuss Weakley
 
Web Application Development Tools for Creating Perfect User Experience
Web Application Development Tools for Creating Perfect User ExperienceWeb Application Development Tools for Creating Perfect User Experience
Web Application Development Tools for Creating Perfect User ExperienceChromeInfo Technologies
 
Elements of html powerpoint
Elements of html powerpointElements of html powerpoint
Elements of html powerpointAnastasia1993
 
Advanced mechanisms for dynamic content delivery
Advanced mechanisms for dynamic content deliveryAdvanced mechanisms for dynamic content delivery
Advanced mechanisms for dynamic content deliveryAakash587
 

What's hot (20)

Presentation about html5 css3
Presentation about html5 css3Presentation about html5 css3
Presentation about html5 css3
 
CSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive DesignCSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive Design
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Bootstrap - Basics
Bootstrap - BasicsBootstrap - Basics
Bootstrap - Basics
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Introduction to JSX
Introduction to JSXIntroduction to JSX
Introduction to JSX
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
 
Dynamic HTML (DHTML)
Dynamic HTML (DHTML)Dynamic HTML (DHTML)
Dynamic HTML (DHTML)
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
HTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts BasicsHTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts Basics
 
Bootstrap 5 whats new
Bootstrap 5   whats newBootstrap 5   whats new
Bootstrap 5 whats new
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
 
Reactjs
ReactjsReactjs
Reactjs
 
Web Application Development Tools for Creating Perfect User Experience
Web Application Development Tools for Creating Perfect User ExperienceWeb Application Development Tools for Creating Perfect User Experience
Web Application Development Tools for Creating Perfect User Experience
 
Elements of html powerpoint
Elements of html powerpointElements of html powerpoint
Elements of html powerpoint
 
Advanced mechanisms for dynamic content delivery
Advanced mechanisms for dynamic content deliveryAdvanced mechanisms for dynamic content delivery
Advanced mechanisms for dynamic content delivery
 

Similar to Chapter 9 Getting Started with JavaScript

9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web formDr. Ahmed Al Zaidy
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Dr. Ahmed Al Zaidy
 
Intro JavaScript
Intro JavaScriptIntro JavaScript
Intro JavaScriptkoppenolski
 
AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)Igor Talevski
 
WEB MODULE 3.pdf
WEB MODULE 3.pdfWEB MODULE 3.pdf
WEB MODULE 3.pdfDeepika A B
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryAndres Baravalle
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectJadson Santos
 
IE10 PP4 update for W3C HTML5 KIG
IE10 PP4 update for W3C HTML5 KIGIE10 PP4 update for W3C HTML5 KIG
IE10 PP4 update for W3C HTML5 KIGReagan Hwang
 
Mastering react with redux
Mastering react with reduxMastering react with redux
Mastering react with reduxGaurav Singh
 
025444215.pptx
025444215.pptx025444215.pptx
025444215.pptxRiyaJenner1
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSPratik Majumdar
 
A Beginner's Guide to Client Side Development with Javascript
A Beginner's Guide to Client Side Development with JavascriptA Beginner's Guide to Client Side Development with Javascript
A Beginner's Guide to Client Side Development with JavascriptSharePoint Saturday New Jersey
 
HTML5 features & JavaScript APIs
HTML5 features & JavaScript APIsHTML5 features & JavaScript APIs
HTML5 features & JavaScript APIsFisnik Doko
 

Similar to Chapter 9 Getting Started with JavaScript (20)

IP Unit 2.pptx
IP Unit 2.pptxIP Unit 2.pptx
IP Unit 2.pptx
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5
 
Intro JavaScript
Intro JavaScriptIntro JavaScript
Intro JavaScript
 
AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)
 
WEB MODULE 3.pdf
WEB MODULE 3.pdfWEB MODULE 3.pdf
WEB MODULE 3.pdf
 
Java script
Java scriptJava script
Java script
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
IE10 PP4 update for W3C HTML5 KIG
IE10 PP4 update for W3C HTML5 KIGIE10 PP4 update for W3C HTML5 KIG
IE10 PP4 update for W3C HTML5 KIG
 
Mastering react with redux
Mastering react with reduxMastering react with redux
Mastering react with redux
 
Welcome to React.pptx
Welcome to React.pptxWelcome to React.pptx
Welcome to React.pptx
 
025444215.pptx
025444215.pptx025444215.pptx
025444215.pptx
 
Html,CSS & UI/UX design
Html,CSS & UI/UX designHtml,CSS & UI/UX design
Html,CSS & UI/UX design
 
Java script
Java scriptJava script
Java script
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJS
 
Dust.js
Dust.jsDust.js
Dust.js
 
A Beginner's Guide to Client Side Development with Javascript
A Beginner's Guide to Client Side Development with JavascriptA Beginner's Guide to Client Side Development with Javascript
A Beginner's Guide to Client Side Development with Javascript
 
HTML5 features & JavaScript APIs
HTML5 features & JavaScript APIsHTML5 features & JavaScript APIs
HTML5 features & JavaScript APIs
 

More from Dr. Ahmed Al Zaidy

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingDr. Ahmed Al Zaidy
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web formsDr. Ahmed Al Zaidy
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsDr. Ahmed Al Zaidy
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesDr. Ahmed Al Zaidy
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaDr. Ahmed Al Zaidy
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsDr. Ahmed Al Zaidy
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSDr. Ahmed Al Zaidy
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutDr. Ahmed Al Zaidy
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSDr. Ahmed Al Zaidy
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2Dr. Ahmed Al Zaidy
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk MitigationDr. Ahmed Al Zaidy
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityDr. Ahmed Al Zaidy
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access ManagementDr. Ahmed Al Zaidy
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementDr. Ahmed Al Zaidy
 
Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Dr. Ahmed Al Zaidy
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application SecurityDr. Ahmed Al Zaidy
 
Chapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityChapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityDr. Ahmed Al Zaidy
 

More from Dr. Ahmed Al Zaidy (20)

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based Programming
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheets
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and Styles
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimedia
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and Columns
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSS
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page Layout
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSS
 
Integer overflows
Integer overflowsInteger overflows
Integer overflows
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2
 
Fundamental of testing
Fundamental of testingFundamental of testing
Fundamental of testing
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access Management
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
 
Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application Security
 
Chapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityChapter 8 Wireless Network Security
Chapter 8 Wireless Network Security
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 

Chapter 9 Getting Started with JavaScript

  • 1. HTML5, CSS3, and JavaScript 6th Edition Getting Started with JavaScript Tutorial 9 Carey
  • 2. XPXPXPXPXPObjectives • Insert a script element • Write JavaScript comments • Display an alert dialog box • Use browser debugging tools • Reference browser and page objects • Use JavaScript properties and methods 2New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 3. XPXPXPXPXPObjectives (continued) • Write HTML code and text content into a page • Work with a Date object • Use JavaScript operators • Create a JavaScript function • Create timed commands 3New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 4. XPXPXPXPXPServer-Side and Client-Side Programming • Server-side programming: Program code runs from the server hosting the website • Advantage – Connects a server to an online database containing information not directly accessible to end users • Disadvantages – Use server resources and requires Internet access – Long delays in cases of system over-load 4New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 5. XPXPXPXPXP Server-Side and Client-Side Programming (continued 1) 5New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 6. XPXPXPXPXP Server-Side and Client-Side Programming (continued 2) • Client-side programming: Programs run on the user’s computer using downloaded scripts with HTML and CSS files • Distributes load to avoid overloading of program-related requests • Client-side programs can never replace server- side programming 6New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 7. XPXPXPXPXP Server-Side and Client-Side Programming (continued 3) 7New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 8. XPXPXPXPXPThe Development of JavaScript • JavaScript is a programming language for client-side programs • It is an interpreted language that executes a program code without using an application • Compiler is an application that translates a program code into machine language • JavaScript code can be directly inserted into or linked to an HTML file 8New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 9. XPXPXPXPXPWorking with the script Element • JavaScript code is attached to an HTML file using the script element <script src=”url”></script> where url is the URL of the external file containing the JavaScript code • An embedded script can be used instead of an external file by omitting the src attribute <script> code </script> 9New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 10. XPXPXPXPXPLoading the script Element • script element can be placed anywhere within an HTML document • When a browser encounters a script, it immediately stops loading the page and begins loading and then processing the script commands • async and defer attributes can be added to script element to modify its sequence of processing 10New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 11. XPXPXPXPXPLoading the script Element (continued) • async attribute tells a browser to parse the HTML and JavaScript code together • defer attribute defers script processing until after the page has been completely parsed and loaded • async and defer attributes are ignored for embedded scripts 11New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 12. XPXPXPXPXPInserting the script Element 12New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 13. XPXPXPXPXPCreating a JavaScript Program • JavaScript programs are created using a standard text editor • Adding Comments to your JavaScript Code – Comments help understand the design and purpose of programs – JavaScript comments can be entered on single or multiple lines 13New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 14. XPXPXPXPXP Creating a JavaScript Program (continued 1) – Syntax of a single-line comment is as follows: // comment text – Syntax of multiple-line comments is as follows: /* comment text spanning several lines */ 14New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 15. XPXPXPXPXP Creating a JavaScript Program (continued 2) 15New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 16. XPXPXPXPXP Creating a JavaScript Program (continued 3) • Writing a JavaScript Command – A command indicates an action for a browser to take – A command should end in a semicolon JavaScript command; 16New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 17. XPXPXPXPXP Creating a JavaScript Program (continued 4) 17New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 18. XPXPXPXPXP Creating a JavaScript Program (continued 5) • Understanding JavaScript Syntax – JavaScript is case sensitive – Extra white space between commands is ignored – Line breaks placed within the name of a JavaScript command or a quoted text string cause an error 18New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 19. XPXPXPXPXPDebugging your Code • Debugging: Process of locating and fixing a programming error • Types of errors – Load-time errors – occur when a script is first loaded by a browser – Run-time errors – occur during execution of a script without syntax errors – Logical errors – are free from syntax and executable mistakes but result in an incorrect output 19New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 20. XPXPXPXPXPOpening a Debugger • Debugging tools locate and fix errors in JavaScript codes • Shortcut to open a debugging tool is F12 key • The tools can also be opened by selecting Developer Tools from the browser menu 20New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 21. XPXPXPXPXPInserting a Breakpoint • A useful technique to locate the source of an error is to set up breakpoints • Breakpoints are locations where a browser pauses a program to determine whether an error has occurred at that point during execution 21New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 22. XPXPXPXPXPInserting a Breakpoint (continued) 22New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 23. XPXPXPXPXPApplying Strict Usage of JavaScript • Strict mode enables all lapses in syntax to result in load-time or run-time errors • To run a script in strict mode, add the following statement to the first line of the file: “use strict”; 23New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 24. XPXPXPXPXPIntroducing Objects • Object: Entity within a browser or web page that has properties and methods • Properties: Define objects • Methods: Act upon objects • JavaScript is an object-based language that manipulates an object by changing one or more of its properties 24New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 25. XPXPXPXPXPIntroducing Objects (continued 1) • Types of JavaScript objects – Built-in objects – intrinsic to JavaScript language – Browser objects – part of browser – Document objects – part of web document – Customized objects – created by a programmer to use in an application • Browser object model (BOM) and document object model (DOM) organize browser and document objects in hierarchical structures, respectively 25New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 26. XPXPXPXPXPIntroducing Objects (continued 2) 26New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 27. XPXPXPXPXPObject References • Objects within the object hierarchy are referenced by their object names such as window, document, or navigator • Objects can be referenced using the notation object1.object2.object3 ... where object1 is at the top of the hierarchy, object2 is a child of object1, and so on 27New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 28. XPXPXPXPXPReferencing Object Collections • Object collections: Objects organized into groups • To reference a specific member of an object collection, use collection[idref] or collection.idref where collection is a reference to the object collection and idref is either an index number or the value of id attribute 28New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 29. XPXPXPXPXP Referencing Object Collections (continued) 29New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 30. XPXPXPXPXP Referencing an Object by ID and Name • An efficient approach to reference an element is to use its id attribute using the expression document.getElementById(id) where id is the value of id attribute 30New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 31. XPXPXPXPXP Changing Properties and Applying Methods • Object Properties – Object property is accessed using object.property where object is a reference to an object and property is a property associated with that object – Read-only properties cannot be modified 31New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 32. XPXPXPXPXP Changing Properties and Applying Methods (continued) • Applying a Method – Objects can be modified using methods – Methods are applied using the expression object.method(values) where object is a reference to an object, method is the name of the method applied to the object, and values is a comma-separated list of values associated with the method 32New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 33. XPXPXPXPXPWriting HTML Code • HTML code stored within a page element is referenced using element.innerHTML where element is an object reference to an element within a web document 33New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 34. XPXPXPXPXPWriting HTML Code (continued 1) • HTML code stored within a page element is referenced using element.innerHTML where element is an object reference to an element within a web document • For example, /* Display the current date and time */ document.getElementById(“dateNow”).inne rHTML = “m/d/y<br />h:m:s”; 34New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 35. XPXPXPXPXPWriting HTML Code (continued 2) 35New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 36. XPXPXPXPXPWriting HTML Code (continued 3) 36New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 37. XPXPXPXPXPWorking with Variables • Variable: Named item in a program that stores a data value • Declaring a Variable – Introduced into a script by declaring the variable using the var keyword var variable = value; where variable is the name assigned to the variable and value is the variable’s initial value 37New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 38. XPXPXPXPXPWorking with Variables (continued) • Conditions to assign variable names in JavaScript – First character must be either a letter or an underscore character ( _ ) – The characters after the first character can be letters, numbers, or underscore characters – No spaces – No using names that are part of JavaScript language 38New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 39. XPXPXPXPXPVariables and Data Types • Data type: Type of information stored in a variable • Supported data types – Numeric value – Text string – Boolean value – Object – null value 39New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 40. XPXPXPXPXPVariables and Data Types (continued) • Numeric value: Any number • Text string: Group of characters enclosed within either double or single quotation marks • Boolean value: Indicates the truth or falsity of a statement 40New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 41. XPXPXPXPXPVariables and Data Types (continued 1) • Object – Simplifies code by removing the need to rewrite complicated object references • null value – Indicates that no value has yet been assigned to a variable 41New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 42. XPXPXPXPXPWorking with Date Objects • Date object: Built-in JavaScript object used to store information about dates and times 42New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 43. XPXPXPXPXPWorking with Date Objects (continued 1) 43New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 44. XPXPXPXPXPWorking with Date Objects (continued 2) 44New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 45. XPXPXPXPXPSetting Date and Time Values 45New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 46. XPXPXPXPXPWorking with Operators and Operands • Operator: Symbol used to act upon an item or a variable within an expression • Operands: Variables or expressions that operators act upon • Types of operators – Binary operators – require two operands in an expression 46New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 47. XPXPXPXPXP Working with Operators and Operands (continued) – Unary operators – require only one operand oIncrement operator (++) – increases the value of an operand by 1 oDecrement operator (--) – decreases the value of an operand by 1 47New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 48. XPXPXPXPXPUsing Assignment Operators • Assignment operator: Assigns a value to an item 48New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 49. XPXPXPXPXPWorking with the Math Object • Math object: Built-in object used to perform mathematical tasks and store mathematical values • Syntax to apply a Math method is Math.method(expression) where method is the method applied to a mathematical expression 49New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 50. XPXPXPXPXP Working with the Math Object (continued 1) 50New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 51. XPXPXPXPXP Working with the Math Object (continued 2) 51New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 52. XPXPXPXPXPUsing Math Constants • Math functions refer to built-in constants stored in JavaScript Math object • Syntax to access mathematical constants is Math.CONSTANT where CONSTANT is the name of one of the mathematical constants supported by Math object 52New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 53. XPXPXPXPXPUsing Math Constants (continued) 53New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 54. XPXPXPXPXPWorking with JavaScript Functions • Function: Collection of commands that performs an action or returns a value • A function name identifies a function and a set of commands that are run when the function is called • Parameters: Variables associated with the function 54New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 55. XPXPXPXPXP Working with JavaScript Functions (continued) • General syntax of a JavaScript function is function function_name(parameters){ commands } where, – function_name is the name of the function – parameters is a comma-separated list of variables used in the function – commands is the set of statements run by the function 55New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 56. XPXPXPXPXPCalling a Function 56New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 57. XPXPXPXPXP Creating a Function to Return a Value • Functions return values using return statement function function_name(parameters){ commands return value; } where value is the calculated value that is returned by the function 57New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 58. XPXPXPXPXPRunning Timed Commands • Methods to update the current and the remaining time constantly – Time-delayed commands – Timed-interval commands • Working with Time-Delayed Commands – Time-delayed commands: JavaScript commands run after a specified amount of time has passed 58New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 59. XPXPXPXPXPRunning Timed Commands (continued 1) – Time delay is defined using setTimeout(“command”, delay); where command is a JavaScript command and delay is the delay time in milliseconds before a browser runs the command • Running Commands at Specified Intervals – The timed-interval command instructs browsers to run a command repeatedly at a specified interval 59New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 60. XPXPXPXPXPRunning Timed Commands (continued 2) – Timed-interval commands are applied using setInterval() method setInterval(“command”, interval); where interval is the interval in milliseconds before the command is run again 60New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 61. XPXPXPXPXPRunning Timed Commands (continued 3) 61New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 62. XPXPXPXPXP Controlling How JavaScript Works with Numeric Values • Handling Illegal Operations – Mathematical operations can return results that are not numeric values – JavaScript returns NaN if an operation does not involve only numeric values 62New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 63. XPXPXPXPXP Controlling How JavaScript Works with Numeric Values (continued) – isNaN() function returns a Boolean value of true if the value is not numeric and false if otherwise – Infinity value is generated for an operation whose result is less than the smallest numeric value and greater than the largest numeric value supported by JavaScript 63New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 64. XPXPXPXPXPDefining a Number Format • JavaScript stores a numeric value to 16 decimal places of accuracy • The number of digits displayed by browsers is controlled using toFixed() method value.toFixed(n) where value is the value or variable and n is the number of decimal places displayed in the output 64New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 65. XPXPXPXPXPDefining a Number Format (continued) • toFixed() limits the number of decimals displayed by a value and converts the value into a text string • toFixed() rounds the last digit in an expression rather than truncating it 65New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 66. XPXPXPXPXP Converting Between Numbers and Text • + operator adds a text string to a number • For example, testNumber = 123; // numeric value testString = testNumber + “”; // text string where + operator concatenates a numeric value with an empty text string resulting in a text string 66New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 67. XPXPXPXPXP Converting Between Numbers and Text (continued 1) • parseInt() function extracts the leading integer value from a text string • It returns the integer value from the text string by discarding any non-integer characters • Example, parseInt(“120.88 lbs”); // returns 120 parseInt(“weight equals 120 lbs”); // returns NaN 67New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
  • 68. XPXPXPXPXP Converting Between Numbers and Text (continued 2) 68New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition