Professional Documents
Culture Documents
INE2720
Web Application Software Development
Essential Materials
Outline – Part A
Overview of JavaScript
– Versions, embedding, comments
JavaScript Basics
– Variables and Data Types
– Operators
– Expressions
JavaScript Control Structures
– Conditional Statements
– Looping Statements
INE2720 – Web Application Software Development 2 All copyrights reserved by C.C. Cheung 2003.
1
Outline – Part B
Outline – Part C
2
Outline – Part D
Introduction
3
JavaScript Capabilities
JavaScript Versions
JavaScript 1.0
– Supported by Netscape 2.0 and IE 3.0
JavaScript 1.1
– Supported by Netscape 3.0 and IE 4.0
JavaScript 1.2
– Supported by Netscape 4.0 and IE 4.0
JavaScript 1.3
– Supported by Netscape 4.5 and IE 5.0
JavaScript 1.5
– Supported by Netscape 6.0 and IE 5.5 and later
INE2720 – Web Application Software Development 8 All copyrights reserved by C.C. Cheung 2003.
4
The Future of JavaScript
A Simple Script
<html>
<head> <title>First JavaScript Page</title> </head>
<body>
<h1>First JavaScript Page</h1>
<script type="text/javascript
type="text/javascript">
">
<!--
<!--
document.write("<hr>");
document.write("Hello World Wide Web");
document.write("<hr>");
-->
-->
</script>
</body>
</html>
INE2720 – Web Application Software Development 10 All copyrights reserved by C.C. Cheung 2003.
5
Embedding JavaScript
<html>
<head>
<title>First JavaScript Program</title>
</head>
<body>
<script language=
language=“JavaScript”
JavaScript” src=
src=“your_source_file.js”
your_source_file.js”></script>
</body>
</html>
<script language=
language=“JavaScript”
JavaScript”
src=
src=“your_source_file.js”
your_source_file.js”></script>
6
Need for a source file
If the JavaScript code is fairly short, you are
recommended to include the code in the
HTML document.
To add clarity to an HTML document.
To share JavaScript code across multiple
HTML documents.
To help you hide your JavaScript code.
– Spent lot of time for JavaScript coding.
– Viewer can only see the location of the source
file but not the contents.
INE2720 – Web Application Software Development 13 All copyrights reserved by C.C. Cheung 2003.
7
JavaScript confusion
JavaScript Java
Interpreted by the client-
client-side Compiled on the server before
computer executed on the client machine
Dynamic binding, object references Static binding, object references
are checked at runtime must exist at compile time
No need to declare data types Data types must be declared
Code is embedded in HTML Code is not integrated in HTML
INE2720 – Web Application Software Development 15 All copyrights reserved by C.C. Cheung 2003.
8
Using the confirm() Method
<head>
<script language=
language=“JavaScript”
JavaScript”>
confirm(“
confirm(“Are you happy with the class?”
class?”);
</script>
</head>
9
Three methods
<script language="JavaScript">
alert("This is an Alert method");
confirm("Are you OK?");
prompt("What is your name?");
prompt("How old are you?","20");
</script>
INE2720 – Web Application Software Development 19 All copyrights reserved by C.C. Cheung 2003.
Variables
JavaScript allows you to declare and use
variables to store values.
How to assign a name to a variable?
– Include uppercase and lowercase letters
– Digits from 0 through 9
– The underscore _ and the dollar sign $
– No space and punctuation characters
– First character must be alphabetic letter or
underscore – 99Total?, 012345number?, …
– Case-
Case-sensitive
– No reserved words or keywords
INE2720 – Web Application Software Development 20 All copyrights reserved by C.C. Cheung 2003.
10
Which one is legal?
My_variable
Legal
$my_variable
1my_example
_my_variable
@my_variable
My_variable_example
++my_variable
%my_variable Illegal
#my_variable
~my_variable
myVariableExample
INE2720 – Web Application Software Development 21 All copyrights reserved by C.C. Cheung 2003.
Variable on-the-fly
<head>
<script language=
language=“JavaScript”
JavaScript”>
Variable declaration
var id;
id = prompt(“
prompt(“What is your student id number?”
number?”);
alert(id);
name = prompt(“
prompt(“What is your name?”
name?”,”No name”
name”);
alert(name);
</script>
</head>
11
Data Types
JavaScript allows the same variable to contain
different types of data values.
Primitive data types
– Number:
Number: integer & floating-
floating-point numbers
– Boolean:
Boolean: logical values “true”
true” or “false”
false”
– String:
String: a sequence of alphanumeric characters
Composite data types (or Complex data types)
– Object:
Object: a named collection of data
– Array:
Array: a sequence of values
Special data types
– Null:
Null: an initial value is assigned
– Undefined:
Undefined: the variable has been created by not yet
assigned a value
INE2720 – Web Application Software Development 23 All copyrights reserved by C.C. Cheung 2003.
12
Integer and Floating-
point number example
<script language=
language=“JavaScript”
JavaScript”>
var integerVar = 100;
var floatingPointVar = 3.0e10;
// floating-
floating-point number 30000000000
document.write(integerVar);
document.write(integerVar);
document.write(floatingPointVar);
document.write(floatingPointVar);
</script>
Boolean Values
13
Boolean value example
<head>
<script language=
language=“JavaScript”
JavaScript”>
var result;
result = (true*10 + false*7);
alert(“
alert(“true*10 + false*7 = “, result);
</script>
</head>
The expression is converted to
– (1*10 + 0*7) = 10
They are automatically converted.
INE2720 – Web Application Software Development 27 All copyrights reserved by C.C. Cheung 2003.
Strings
14
Strings example
<head>
<script language=
language=“JavaScript”
JavaScript”>
document.write(“
document.write(“This is a string.”
string.”);
document.write(“
document.write(“This string contains ‘quote’
quote’.”);
var myString = “My testing string”
string”;
alert(myString);
alert(myString);
</script>
</head>
typeof operator
<head>
<script language=
language=“JavaScript”
JavaScript”>
var x = “hello”
hello”, y;
alert(“
alert(“Variable x value is “ + typeof(x));
typeof(x));
alert(“
alert(“Variable y value is “ + typeof(y));
typeof(y));
alert(“
alert(“Variable x value is “ + typeof(z));
typeof(z));
</script>
</head>
It is an unary operator.
– Return either: Number, string, Boolean,
object, function, undefined, null
INE2720 – Web Application Software Development 30 All copyrights reserved by C.C. Cheung 2003.
15
What is an Object?
An object is a thing, anything, just as things
in the real world.
– E.g. (cars, dogs, money, books, … )
In the web browser, objects are the browser
window itself, forms, buttons, text boxes, …
Methods are things that objects can do.
– Cars can move, dogs can bark.
– Window object can alert the user “alert()”
alert()”.
All objects have properties.
– Cars have wheels, dogs have fur.
– Browser has a name and version number.
INE2720 – Web Application Software Development 31 All copyrights reserved by C.C. Cheung 2003.
Array
16
Array Example
<script language=
language=“JavaScript”
JavaScript”>
Car = new Array(3);
Car[0] = “Ford”
Ford”;
Car[1] = “Toyota”
Toyota”;
Car[2] = “Honda”
Honda”;
document.write(Car[0] + “<br>
br>”);
document.write(Car[1] + “<br>
br>”);
document.write(Car[2] + “<br>
br>”);
</script>
You can also declare arrays with variable length.
– arrayName = new Array();
– Length = 0, allows automatic extension of the length.
– Car[9] = “Ford”
Ford”; Car[99] = “Honda”
Honda”;
INE2720 – Web Application Software Development 33 All copyrights reserved by C.C. Cheung 2003.
An “undefined”
undefined” value is returned when
you attempt to use a variable that has
not been defined or you have declared
but you forgot to provide with a value.
Null refers to “nothing”
nothing”
You can declare and define a variable
as “null”
null” if you want absolutely nothing
in it, but you just don’
don’t want it to be
“undefined”
undefined”.
INE2720 – Web Application Software Development 34 All copyrights reserved by C.C. Cheung 2003.
17
Null & Undefined example
<html>
<head>
<title> Null and Undefined example </title>
<script language=“
language=“JavaScript”
JavaScript”>
var test1, test2 = null;
alert(“
alert(“No value assigned to the variable”
variable” + test1);
alert(“
alert(“A null value was assigned”
assigned” + test2);
</script>
</head>
<body> … </body>
</html>
INE2720 – Web Application Software Development 35 All copyrights reserved by C.C. Cheung 2003.
Character Meaning
\b Backspace
\f Form feed
\t Horizontal tab
\n New line
\r Carriage return
\\ Backslash
\’ Single quote
\” Double quote
INE2720 – Web Application Software Development 36 All copyrights reserved by C.C. Cheung 2003.
18
Break Time – 10 minutes
INE2720 – Web Application Software Development 37 All copyrights reserved by C.C. Cheung 2003.
Expressions
INE2720 – Web Application Software Development 38 All copyrights reserved by C.C. Cheung 2003.
19
Operators
Arithmetic operators
Logical operators
Comparison operators
String operators
Bit-
Bit-wise operators
Assignment operators
Conditional operators
INE2720 – Web Application Software Development 39 All copyrights reserved by C.C. Cheung 2003.
Arithmetic operators
left_operand “operator”
operator” right_operand
20
Unary Arithmetic
Operators
Binary operators take two operands.
Unary type operators take only one
operand.
Which one add value first, and then assign
value to the variable?
Name Example
Post Incrementing operator Counter++
Post Decrementing operator Counter--
Counter--
Pre Incrementing operator ++counter
Pre Decrementing operator --counter
--counter
INE2720 – Web Application Software Development 41 All copyrights reserved by C.C. Cheung 2003.
Logical operators
Used to perform Boolean operations on
Boolean operands
INE2720 – Web Application Software Development 42 All copyrights reserved by C.C. Cheung 2003.
21
Comparison operators
<script language=“
language=“JavaScript”
JavaScript”>
var currentWord=
currentWord=“75”
75”;
var currentValue=75;
currentValue=75;
var outcome1=(currentWord
outcome1=(currentWord == currentValue);
currentValue);
var outcome2=(currentWord
outcome2=(currentWord === currentValue);
currentValue);
alert(“
alert(“outcome1: “ + outcome1 + “ : outcome2: “ + outcome2);
</script>
22
String operator
<script language=“
language=“JavaScript”
JavaScript”>
var myString = “”;
“”;
myString = “Hello”
Hello” + “World”
World”;
alert(myString);
alert(myString);
</script>
INE2720 – Web Application Software Development 45 All copyrights reserved by C.C. Cheung 2003.
INE2720 – Web Application Software Development 46 All copyrights reserved by C.C. Cheung 2003.
23
Assignment operators
INE2720 – Web Application Software Development 47 All copyrights reserved by C.C. Cheung 2003.
<script language=“
language=“JavaScript”
JavaScript”>
if (alpha = beta) { … }
if (alpha == beta) { … }
</script>
Don’
Don’t mix the comparison operator
and the assignment operator.
double equal sign (==) and the equal
operator (=)
INE2720 – Web Application Software Development 48 All copyrights reserved by C.C. Cheung 2003.
24
Order of Precedence
Precedence Operator
1 Parentheses, function calls
2 , ~, -, ++, --,
--, new, void, delete
3 *, /, %
4 +, -
5 <<, >>, >>>
6 <, <=, >, >=
7 ==, !=, ===, !==
8 &
9 ^
10 |
11 &&
12 ||
13 ?:
14 =, +=, -=, *=, …
15 The comma
49
(,) operatorAll copyrights reserved by C.C. Cheung 2003.
INE2720 – Web Application Software Development
Precedence Example
25
Scope of a Variable
26
Output of the code
INE2720 – Web Application Software Development 53 All copyrights reserved by C.C. Cheung 2003.
Conditional Statement
“if” statement
“if … else” statement
“switch” statement
INE2720 – Web Application Software Development 54 All copyrights reserved by C.C. Cheung 2003.
27
“if” statement
if (condition)
condition) { statements; }
It is the main conditional statement in
JavaScript.
The keyword “if”
if” always appears in
lowercase.
The condition yields a logical true or false
value.
The condition is true, statements are
executed.
INE2720 – Web Application Software Development 55 All copyrights reserved by C.C. Cheung 2003.
<script language=“
language=“JavaScript”
JavaScript”>
var chr = “”;
“”;
…
if (chr
(chr == ‘A’ || chr == ‘O’) {
document.write(“
document.write(“Vowel variable”
variable”);
}
</script>
INE2720 – Web Application Software Development 56 All copyrights reserved by C.C. Cheung 2003.
28
“if … else” statement
if (condition)
condition) { statements; }
else { statements; }
INE2720 – Web Application Software Development 57 All copyrights reserved by C.C. Cheung 2003.
(3 > 2) ? alert(“
alert(“True”
True”) : alert(“
alert(“False”
False”);
</script>
29
“else if” statement
if (condition)
condition) { statement; }
else if (condition)
condition) { statement; }
else { statement; }
INE2720 – Web Application Software Development 59 All copyrights reserved by C.C. Cheung 2003.
30
“switch” statement
switch (expression)
expression) {
case label1:
statements; break;
default:
statements;
}
<script language=“
language=“JavaScript”
JavaScript”>
var chr;
chr;
chr = prompt(“
prompt(“Pls enter a character in lowercase:”
lowercase:”,””);
””);
switch(chr){
switch(chr){
case ‘a’ :
alert(“
alert(“Vowel a”
a”); break;
case ‘e’ :
alert(“
alert(“Vowel e”
e”); break;
default :
alert(“
alert(“Not a vowel”
vowel”);
}
</script>
INE2720 – Web Application Software Development 62 All copyrights reserved by C.C. Cheung 2003.
31
Looping Statement
“for”
for” Loops
“for/in”
for/in” Loops
“while”
while” Loops
“do … while”
while” Loops
“break”
break” statement
“continue”
continue” statement
INE2720 – Web Application Software Development 63 All copyrights reserved by C.C. Cheung 2003.
“for” statement
for (initial_expression; test_exp; change_exp)
change_exp)
{ statements; }
32
“for” statement example
<script language=“
language=“JavaScript”
JavaScript”>
var counter;
for (counter = 1;
1; counter <= 10; counter++)
counter++)
{
document.write(counter*counter + “ “);
}
</script>
“for/in” statement
for (counter_variable in object)
{ statements; }
33
“for/in” statement example
<script language=“
language=“JavaScript”
JavaScript”>
var book; (What is the difference if “var book=“”
book=“”;)
;)
var booklist = new Array(“
Array(“Chinese”
Chinese”, “English”
English”, “Jap”
Jap”);
for (var
(var counter in booklist)
booklist) {
book += booklist[counter] + “ “;
}
alert(book);
</script>
INE2720 – Web Application Software Development 67 All copyrights reserved by C.C. Cheung 2003.
“while” statement
initial value declaration;
while (condition)
condition) {
statements;
increment/decrement statement;
}
34
“While” statement example
<html>
<head>
<title>While loop example</title>
<script language=“
language=“JavaScript”
JavaScript”>
var counter = 100;
var numberlist = “”;
“”;
while (counter > 0)
0) {
numberlist += “Number “ + counter + “<br>
br>”;
counter -= 10;
}
document.write(numberlist);
document.write(numberlist);
</script> <body> … </body>
</html>
INE2720 – Web Application Software Development 69 All copyrights reserved by C.C. Cheung 2003.
35
Example of Flow Control
INE2720 – Web Application Software Development 71 All copyrights reserved by C.C. Cheung 2003.
References
Deitel:
Deitel: Chapter 7, 8, 9, 11
CWP: Chapter 24
Making use of JavaScript
SAMS JavaScript
The End.
Thank you for patience!
INE2720 – Web Application Software Development 72 All copyrights reserved by C.C. Cheung 2003.
36