Professional Documents
Culture Documents
Learning & Knowledge Education - India: © 2007 IBM Corporation
Learning & Knowledge Education - India: © 2007 IBM Corporation
Day 2
2007 IBM Corporation
JavaScript
2007 IBM Corporation
Objectives
Introduction to JavaScript
Introduction to JavaScript programming language
Using Script tag
Inserting Java Script into tags
Linking Java Script from separate files
Objectives (Contd.)
Ifelse
For loops
while loops
Break and continue statements
Built in functions
User defined functions
Returning values from functions
Events, Event Handlers
2007 IBM Corporation
Objectives (Contd.)
Objectives (Contd.)
Objectives (Contd.)
Objectives (Contd.)
Cookies
Introduction to HTTP Cookies
Formatting Cookies
Persisting Cookies
Cookie Security
JavaScript & Cookies
Reading / Writing Cookies
Modifying and deleting Cookies
Introduction
To
JavaScript
2007 IBM Corporation
What is JavaScript???
JavaScript is a scripting Language created by Netscape
What is a
Scripting
Language???
10
11
JavaScript is a scripting
language which just requires
interpretation. The script is some
set of commands which the
browser interprets.
12
Types of JavaScript
13
Core JavaScript
14
15
16
17
________________ is an extended
version of JavaScript that enables
the enhancement and manipulation
of web pages and client browsers
18
________________ is an extended
version of JavaScript that enables
the enhancement and manipulation
of web pages and client browsers
19
20
21
22
Eg.
<script type="text/javascript">
<!-document.write("Hello World!")
// -->
</script>
23
24
<html>
<head><title>Example</title>
</head>
<body>
<script src="External.js">
</script>>
<p >
This line has been written in the
html page!!!
</p>
</body>
</html>
JavaScript.html
25
<script></script> is embedded
within
<head> </head>
<script></script is embedded
within <body></body>
<script></script> is embedded
within <title></title>
26
<script></script> is embedded
within
<head> </head>
<script></script is embedded
within <body></body>
<script></script> is embedded
within <title></title>
27
JavaScript
Operators &
Expressions
2007 IBM Corporation
JavaScript Variables
Variable:
A variable is a symbolic name that represents
some data in the memory.
A variable value can change during the execution
of the JavaScript.
A variable can be referred by its name to see or
change its value.
Variables are name are case sensitive.
Must begin with a letter or underscore.
29
Rules of a Variable
Variable Declaration
Variables can be declared using the var statement
var <variable name>=some value
Variables can also be created without using var statement
<variable name>=some value
Eg
var firstname=Samuel
OR
firstname=Samuel
30
31
numbers
string
boolean
9, 3.56
true or false
null
A Special keyword which refers to
nothing
32
33
JavaScript Operators
Operators
Arithmetic
Assignment
String
Comparison
Logical
34
Conditional
typeof
35
36
37
38
39
40
41
number
string
null
42
number
string
null
43
Flow Control
&
Functions
2007 IBM Corporation
Flow Control
Conditional Statements
if statement - use this statement if you want to execute some
code only if a specified condition is true.
if...else statement - use this statement if you want to execute
some code if the condition is true and another code if the
condition is false.
if...else if....else statement - use this statement if you want to
select one of many blocks of code to be executed.
switch statement - use this statement if you want to select
one of many blocks of code to be executed.
45
while statement
46
There are two special statements that can be used inside loops:
break.
continue.
Break
The break command will break the loop and continue executing the
code that follows after the loop (if any).
Continue
The continue command will break the current loop and continue with
the next value.
47
48
Result
The number is 0
The number is 1
The number is 2
49
Result
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
For loop
50
Functions
51
DialogBoxes
alert( message)
Displays an alert box with a
message defined by the
string message.
Eg.
alert(An Error Occurred!)
52
confirm(message)
When called, it will display the
message and two boxes. One box
is "OK" and the other is "Cancel". If
OK is selected, a value of true is
returned, otherwise a value of false
is returned.
Eg.
confirm(Do you wish to Continue?)
53
prompt(message)
Displays a box with the message
passed to the function displayed.
The user can then enter text in
the prompt field, and choose OK
or Cancel.
If the user chooses Cancel, a
NULL value is returned. If the
user chooses OK, the string value
entered in the field is returned.
Eg:
prompt(enter your Name)
54
Conversion Functions
eval(string)
Converts a string to an integer or a float value.
Eg
x=20
y=eval(x)+10
y contains the value 30
55
56
57
58
59
60
61
Events
62
Events (contd.)
63
Event Handlers
64
65
66
67
The checkEmail() function will be called whenever the user changes the content of the field:
68
onSubmit
The onSubmit event is used to validate ALL form fields before
submitting it.
The checkForm() function will be called when the user clicks the submit
button in the form.
<form method="post" action="xxx.htm" onsubmit="return checkForm()">
69
70
71
72
Example
73
Example (contd.)
74
Example (contd.)
75
Example (contd.)
76
Example (contd.)
77
Example (contd.)
78
Example (contd.)
function Addition(x,y)
{
var x1=document.form1.text1.value
var y1=document.form1.text2.value
var Ans=document.form1.text3.value
var temp=x1+y1
}
79
Example (contd.)
function Addition (x,y) {
var x1=parseInt(x)
var y1=parseInt(y)
var Ans=document.form1.text3.value
var temp=x1+y1
}
80
Example (contd.)
function Addition (x,y) {
var x1=parseInt(x)
var y1=parseInt(y)
var Ans=document.form1.text3.value
var temp=x1+y1
}
<INPUT TYPE=button value= +op
onClick=Addition(text1.value,text2.value)>
81
Example (contd.)
function Addition (x,y)
var x1=parseInt(x)
var y1=parseInt(y)
var Ans=document.form1.text3.value
var temp=x1+y1
if(Ans==temp){
alert(Your Result agrees with JavaScript!)
document.form1.text1.value=
document.form1.text2.value=
document.form1.text3.value=
}
else{
alert(No, JavaScript evalutes this operation differently)
document.form1.text3.value=
}}
82
83
84
A Complete Program
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
<input type="button"
onClick="myfunction('Good Morning!')"
value="In the Morning">
<input type="button"
onClick="myfunction('Good Evening!')"
alert(txt)
</form>
</script>
<p>
</head>
<body>
<form>
</p>
</body>
</html>
85
Output
86
87
88
Objects
&
Arrays
2007 IBM Corporation
JavaScript Objects
JavaScript is not a true Object Oriented language as C++ or Java
but rather an Object Based language.
Objects in JavaScript are software entities such as the browser
window or an HTML document.
90
91
92
93
94
95
96
97
98
99
100
101
102
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
103
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
104
105
106
107
108
109
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
110
111
112
113
114
115
116
117
118
119
120
121
122
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
123
124
125
126
127
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
128
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
129
130
131
132
133
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
134
135
136
137
1.
Boolean
2.
Date
3.
Function
4.
Math
5.
Number
6.
String
Boolean
Math
Date
Number
Function
String
138
s1 = "2 + 2
139
eval(s1)
eval(s2)
eval(s2.valueOf());
140
Defining Arrays
An Array object is used to store a set of values in a single
variable name.
An Array object is created with the new keyword.
An array can be created as:
var MyArray=new Array()
An array can also be created by specifying the array size.
var MyArray=new Array(3)
141
Arrays (contd.)
Data can be entered into an array as:
var MyArray=new Array()
MyArray[0]=Paul
MyArray[1]=Sam
MyArray[2]=Niel
Data can also be entered into an array as:
var MyArray=new Array(Paul,Sam, Niel)
142
Arrays (contd.)
Accessing Arrays
You can refer to a particular element in an array by referring to
the name of the array and the index number.
The index number starts at 0 .
var MyArray=new Array()
Myarray [0]
143
Document
Object
Model
2007 IBM Corporation
145
146
147
Document
Document Tree
Tree
148
149
Navigator Object
Navigator object is the object representation of the client internet
browser or web navigator program that is being used.
This is a top level object to all others object in DOM hierarchy.
150
151
152
Window Object
The Window object is the top level object in the JavaScript
hierarchy.
The Window object represents a browser window.
A Window object is created automatically with every instance of a
<body> or <frameset> tag.
153
154
Frame Object
Frame object represents an HTML frame.
For each instance of a <frame> tag in an HTML document, a
Frame object is created.
157
In
Document Object
Model
Frames are
instances of Window
object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
Document Object
The document object represents the entire HTML document and
can be used to access all elements in a page.
The document object is part of the window object and is
accessed through the window.document property or simply
document.
177
178
179
180
Location Object
Location object is an JavaScript object it is not an DOM object.
The Location object is automatically created by the JavaScript
runtime engine and contains information about the current URL.
The Location object is part of the Window object and is accessed
through the window.location property.
181
182
183
184
185
186
187
188
Output:
If Accessed from the Local File System
190
Output:
If the page is delivered by a Web Server.
191
History Object
History object is a JavaScript object.
The History object is automatically created by the JavaScript
runtime engine and consists of and array of URLs.
It is a part of window object & accessed through window.history
property.
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Cookies
2007 IBM Corporation
Cookies
224
Cookies (contd.)
225
Cookies (contd.)
226
Cookies (contd.)
The domain
The "domain" attribute allows you to set a domain name for
the cookie. Again, this is optional.
227
Question
The name-value pair is the
first
second
third
228
Answer
The name-value pair is the
first
second
third
229
230
Format of Cookie
231
document.cookie=<name of cookie>=<value of
cookie>;<blank space>expires=<date in UTC
format>;<blank space>path=<path location>
Example
232
</script>
</head>
<body>
</body>
</html>
233
escape() function
There's no escape!
Strictly speaking, we should be escaping our cookie values -encoding non-alphanumeric characters such as spaces and
semicolons.
This is to ensure that our browser can interpret the values
properly.
Fortunately this is easy to do with JavaScript's escape() function.
For example
document.cookie = "username=" + escape(Paul Smith") + ";
expires=15/02/2003 00:00:00";
234
</script>
</head>
<body>
<form>
<input type="button" value="Set a
Cookie"
onClick="setCookie(user',Paul
Smith')">
</form>
</body>
</html>
235
Reading a Cookie
<html>
</script>
<head>
</head>
<script language="JavaScript">
<body>
function readCookie() {
<form><input type="button"
value="read"
onClick="readCookie()">
var ca = document.cookie
document.write(ca)
}
</form>
</body>
Value retrieved is
</html>
user=Paul Smith
236
document.write( document.cookie.substri
ng(c_start,c_end))
<script language="JavaScript">
function readCookie(c_name)
if (document.cookie.length>0)
</script>
{c_start=document.cookie.indexOf(c_name +
"=")
</head>
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
237
<body>
<form> <input type="button" value="read"
onClick="readCookie('user')">
</form>
</body>
</html>
Delete Cookies
<html>
<head>
<body>
<script language="JavaScript">
<form>
<input type="button"
value=delete"
onClick="deleteCookie(user')
">
{
var cookie_date = new Date ( )
cookie_date.setTime
( cookie_date.getTime() - 1 )
document.cookie = cookie_name += "=;
expires=" + cookie_date.toGMTString()
}
</script>
</head>
238
</form>
</body>
</html>
Modifying a cookie
239
Summary
Cookies are small text files that sit on your hard disk.
Cookies are created when you visit websites that use cookies to store
information that they need (or prefer).
240
Thank You
For Your
Participation
2007 IBM Corporation