Beginning Javascript Programming: Lab 1: Integrating Javascript and HTML
Beginning Javascript Programming: Lab 1: Integrating Javascript and HTML
Outline
Discuss how to integrate JavaScript and HTML
Insert SCRIPT tags on a Web page
Write beginning and ending SCRIPT tags
Define and use flickering to draw attention
Describe the background color property of the document object
Set the background color of a Web page using JavaScript
Save the HTML file
Test the Web page
Discuss JavaScript variables
3
Outline
Extract the system date
Use several variables to construct a message
Describe the write() method of the document object
Write a user-defined function that displays a message and links
viewers to a new site
Describe how the setTimeout() method works
Use the lastModified property to display the last modified
document date
Print an HTML Notepad file
4
Introduction
Introduction
www.bmwusa.com.
Introduction
Electronic commerce(E-commerce)
• Conducting business on-line. This includes, for
example, marketing, buying and selling products and
services with digital cash and via Electronic Data
Interchange (EDI).
Introduction
Domain Name
A name that identifies one or more IP addresses. For
example, the domain name microsoft.com represents
about a dozen IP addresses.
Introduction
. For example:
• gov - Government agencies
• edu - Educational institutions
• org - Organizations (nonprofit)
• mil - Military
• com - commercial business
• net - Network organizations
• ca - Canada
• th - Thailand
Introduction
Introduction
Introduction
• There will have a link to the new web sit and also,
within seconds will generate a alert message to inform
the user to transfer to the new location
12
</HEAD>
15
<CENTER><HR Width="75%"></CENTER>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers
document.bgColor="red"
document.bgColor="white"
document.bgColor="blue"
document.bgColor="white"
document.bgColor="green"
document.bgColor="white"
document.bgColor="blanchedalmond"
16
//-->
</SCRIPT>
</BODY>
</HTML>
18
Common Agreed:
1. User-defined JavaScript functions should place at
the HEAD section.
2.Even handlers or other JavaScript code should place
at the BODY section.
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers
document.bgColor = value
document.bgColor="red"
document.bgColor="white"
document.bgColor="blue"
document.bgColor="white"
document.bgColor="green"
document.bgColor="white"
document.bgColor="blanchedalmond"
29
Using a Flicker on a Web Page to Draw
Attention
document.bgColor="red"
document.bgColor="white"
//-->
</SCRIPT>
30
Using a Flicker on a Web Page to Draw
Attention
//-->
</SCRIPT>
document.bgColor="blue"
document.bgColor="white"
document.bgColor="green"
document.bgColor="white"
document.bgColor="blanchedalmond“
33
Using a Flicker on a Web Page to Draw
Attention
JavaScript Variables
var
A statement that declares a variable, optionally
initializing it to a value. The scope of a variable is the
current function or, for variables declared outside a
function, the current application.
JavaScript Variables
Syntax
var varname [= value] [..., varname [= value] ]
Arguments
varname is the variable name. It can be any legal
identifier.
value is the initial value of the variable and can be any
legal expression.
37
JavaScript Variables
JavaScript Variables
•Literals
•You use literals to represent values in JavaScript. These
are fixed values, not variables, that you literally provide
in your script. There are Integers literals (Decimal,
Octal, and Hexadecimal), Floating-point literals,
Boolean literals (true and false), and String
literals( enclosed in double (") or single (') quotation
marks.
39
JavaScript Variables
stringName.substring(indexA, indexB)
Parameters
stringName is any string or a property of an existing
object.
indexA is any integer from zero to stringName.length - 1,
or a property of an existing object.
indexB is any integer from zero to stringName.length, or
a property of an existing object.
Description
Characters in a string are indexed from left to right. The
index of the first character is zero, and the index of the
last character is stringName.length - 1.
45
M M / D D / Y Y Y Y H H : MM : S S
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
47
getDate
Method. Returns the day of the month for the specified
date.
Syntax
dateObjectName.getDate()
Parameters
dateObjectName is either the name of a Date object or a
property of an existing object.
50
Description
The value returned by getDate is an integer between one
and 31.
Examples
The second statement below assigns the value 25 to the
variable day, based on the value of the Date object
Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00")
day = Xmas95.getDate()
51
getMonth
Method. Returns the month in the specified date.
Syntax
dateObjectName.getMonth()
Parameters
dateObjectName is either the name of a Date object or a
property of an existing object.
Description
The value returned by getMonth is an integer between
zero and 11. Zero corresponds to January, one to
February, and so on.
52
Examples
The second statement below assigns the value 11 to the
variable month, based on the value of the Date object
Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00")
month = Xmas95.getMonth()
53
getYear
Method. Returns the year in the specified date.
Syntax
dateObjectName.getYear()
Parameters
dateObjectName is either the name of a Date object or a
property of an existing object.
Description
The getYear method returns either a two-digit or four-
digit year:
54
Examples
The second statement below assigns the value 95 to the
variable year, based on the value of the Date object
Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00")
year = Xmas95.getYear()
55
writeln
Method. Writes one or more HTML expressions to a
document in the specified window and follows them with
a newline character.
Syntax
document.writeln(expression1 [,expression2], ...
[,expressionN])
Parameters
•expression1 through expressionN are any JavaScript
expressions or the properties of existing objects.
57
Description
The writeln method displays any number of expressions
in a document window. You can specify any JavaScript
expression, including numerics, strings, or logicals.
Examples
All the examples used for the write method are also valid
with the writeln method.
window2=window.open('','window2')
beginComment="\<!--"
endComment="--\>"
window2.document.write(beginComment)
window2.document.write(" This some text inside a
comment. ")
window2.document.write(endComment)
59
Concatenate
It means to joint or link together strings.
document.write("<H2><CENTER>Welcome, today is
"+tDate+"</CENTER></H2>")
60
var intro1 = "Hi, thanks for visiting our Web site, but we have
moved. Please make a note “
document.write("<H4><Font Color='firebrick'>“ +
introMsg + "</H4></FONT>“ )
64
An user-defined function
A statement that declares a JavaScript function name
with the specified parameters param. Acceptable
parameters include strings, numbers, and objects.
Arguments
name is the function name.
param is the name of an argument to be passed to the function.
A function can have up to 255 arguments.
Examples
//This function returns the total dollar amount of sales, when
//given the number of units sold of products a, b, and c.
function calc_sales(units_a, units_b, units_c) {
return units_a*79 + units_b*129 + units_c*699
}
71
Writing a JavaScript User-Defined
Function
alert
Method of window object. Displays an Alert dialog
box with a message and an OK button.
Syntax
alert("message")
Parameters
message is any string or a property of an existing
object.
75
Writing a JavaScript User-Defined
Function
Description
Use the alert method to display a message that does
not require a user decision. The message argument
specifies a message that the dialog box contains.
Although alert is a method of the window object,
you do not need to specify a windowReference
when you call it. For example,
windowReference.alert() is unnecessary.
76
Writing a JavaScript User-Defined
Function
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers
function chngSite() {
alert("You are about to be transported to the new site
location!")
location = "http://www.scsite.com/"
}
//-->
</SCRIPT>