Java Script
Java Script
Introduction to JavaScript
Lesson Objectives
• To understand the following topics:
Basic Concepts of JavaScript
2
1.1: Basic Concepts of JavaScript
3
1.1: Basic Concepts of JavaScript > 1.1.1: What is JavaScript?
What is JavaScript ?
4
1.2: JavaScript and Java
JavaScript Java
5
1.3: Embedding JavaScript in HTML
<SCRIPT>
JavaScript statements …
</SCRIPT>
<SCRIPT LANGUAGE=“JavaScript1.2”>
6
1.3: Embedding JavaScript in HTML
<SCRIPT>
JavaScript statements …
</SCRIPT>
<SCRIPT LANGUAGE=“JavaScript1.2”>
7
Embedding JavaScript in HTML (Contd.)
<script type=“text/javascript”>
<!--
some statements …
// -->
</script>
<SCRIPT SRC=“common.js”></SCRIPT>
8
Embedding JavaScript in HTML (Contd.)
9
Embedding JavaScript in HTML (Contd.)
<NOSCRIPT>
Your browser has JavaScript turned off.
</NOSCRIPT>
10
1.3: Embedding JavaScript in HTML > 1.3.1: Where to Write JavaScript?
<head></head>
• Body Section <script
language=
• External File “JavaScript”> <body></body>
</script>
External file
//script statement
11
1.3.1: Where to Write JavaScript? > 1.3.1.1: JavaScript in Head Section
<HTML>
<HEAD>
<TITLE>Script tag in Head Section</TITLE>
<SCRIPT language="Javascript">
<!--
document.write("<h1>message displayed due to script in head</h1>")
- ->
</ SCRIPT >
</HEAD>
<BODY>
</BODY>
</HTML>
12
1.3.1: Where to Write JavaScript? > 1.3.1.2: JavaScript in Body Section
<HTML>
<HEAD>
<TITLE>Script tag in Body</TITLE>
</HEAD>
<BODY >
<SCRIPT language=“Javascript">
document.write("<h1>message displayed due to script in body</h1>")
</SCRIPT>
</BODY>
</HTML>
13
1.3.1: Where to Write JavaScript? > 1.3.1.3: JavaScript in External File
<HTML>
<HEAD>
<TITLE>script tag in external file</TITLE>
<SCRIPT src="common.js">
<!– No javascript statements can be written here
</ SCRIPT>
</HEAD>
<BODY>
< SCRIPT>
document.write("Display value of a variable”+msg)
</ SCRIPT >
</BODY>
</HTML>
14
External js File
var msg
Contents of Common.js
15
Demo
• Hello.html
• Head_section.html
• Extern_file.html
• Comm.js
• Var_ex.html
• Confirm_ex.html
16
JavaScript
The JavaScript Language
Lesson Objectives
• To understand the following topics:
Data Types and Variables
JavaScript Operators
Control Structures and Loops
JavaScript Functions
18
Overview
• JavaScript Language:
Data Types and Variables
Functions
Using the arguments Array
Predefined Functions
Using Global and Local Variables
Summary
19
2.1: Data Types and Variables
20
Data Types in JavaScript (Contd..)
• Scope of variables
<script language=“Javascript”>
var Global Variable
companyName=“MyCompany”
function f(){ Local Variable
22
2.2: JavaScript Operators > 2.2.1: Arithmetic Operator
+ Addition 2+2 4
- Subtraction 5–2 3
* Multiplication 4*5 20
/ Division 5/2 2.5
% Modulus 10 % 8 2
++ Increment x = 5; x++ x=6
-- Decrement x = 5; x-- x=4
23
2.2: JavaScript Operators > 2.2.2: Comparison Operator
== is equal to 5 == 8 false
24
2.2: JavaScript Operators > 2.2.3: Assignment Operator
+= x += y x=x+y
-= x -= y x=x–y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
25
2.2: JavaScript Operators > 2.2.4: Logical Operator
&& and x = 6; y = 3
x < 10 && y > 1 returns true
|| or x = 6; y = 3
x < 10 || y > 5 returns true
! not x = false
!x returns true
26
2.2: JavaScript Operators > 2.2.5: String Operator
27
2.2: JavaScript Operators > 2.2.6: typeof Operator
typeof Operator
typeof 33 “number”
28
Demo
• Typeof_ex.html
29
2.3: Control Structures and Loops
30
2.3: Control Structures and Loops > 2.3.1: „if‟ Statement
The if Statement
if(condition) {
if(a>10) {
statement 1
document.write(“Greater than
} else {
10”)
statement 2
} else {
}
document.write(“Less than 10”)
}
Shorthand
document.write( (a>10) ? “Greater than 10” : “Less than 10”
);
31
2.3: Control Structures and Loops > 2.3.2: Switch Statement
32
2.3: Control Structures and Loops > 2.3.3: „for‟ and „while‟ Statements
while(condition) { while(i<10) {
statements document.write(“Hello”);
} i++;}
33
The for and while Statements (contd..)
while(condition) {
statements
}
34
2.3: Control Structures and Loops > 2.3.4: „Break‟ and „Continue‟ Statements
35
Demo -for loop
• For_ex.html
36
2.4: JavaScript Functions
37
2.4: JavaScript Functions > 2.4.1: Argument Arrays and How to call a Function
arguments[i]
functionName.arguments[i]
38
The Function Statement (Contd..)
function myConcat(separator) {
result = “”
for(var i=1; i<arguments.length;i++) {
result += arguments[i] + separator
}
return result
}
myConcat( “,” , “red” , “orange” , “blue”)
// returns “red, orange, blue”
39
2.4: JavaScript Functions > 2.4.3: Predefined Functions
Predefined Functions
• eval:
Evaluates a string of JavaScript code without reference
to a particular object.
eval (expr)
where expr is a string to be evaluated
• isFinite:
Evaluates an argument to determine whether it is a
finite number.
40
Predefined Functions (Contd..)
isFinite (number)
where number is the number to evaluate
• isNaN :
isNaN (testValue)
where testValue is the value you want to evaluate
41
Predefined Functions (Contd..)
parseInt(str, radix)
returns an integer of the specified radix of the string
argument
42
Predefined Functions (Contd..)
43
2.4: JavaScript Functions > 2.4.5: Global and Local Variables
44
Demo
• If_ex.html
• Switch_ex.html
• Break_con_ex.html
• Fun_ex.html
• Num_string_fun.html
46
JavaScript
Arrays
Lesson Objectives
48
5.1: Array Objects
49
5.2: Populating an Array
• Populating an array:
50
Concept of Populating an Array
52
5.3: Deleting an Array Entry
myArray.length// result: 5
delete myArray[2]
myArray.length// result: 5
myArray[2] // result: undefined
53
5.4: Array Object Methods
arrayObject.slice(startIndex, [endIndex])
arrayObject.join(separatorString)
• The code snippet here shows the usage of join method.
In this, myArray contents will be joined and placed into
arrayText by using the comma separator"
54
Concept of Array Object Methods
arrayObject.sort([compareFunction])
55
JavaScript
Working with Objects
Lesson Objectives
• To understand the following topics:
Object and Properties
61
Overview
Summary
62
3.1: Objects and Properties
63
3.2: Creating New Objects > 3.2.1: Using Initializers
64
3.2: Creating New Objects > 3.2.2: Using Constructors
65
Creating New Objects (Contd..)
67
3.2: Creating New Objects > 3.2.3: Defining Properties for an Object Type
• Accessing properties
car1.year=2000
document.write(car1.model)
document.write(car1.owner.name)
car1.color = “black”
68
3.2: Creating New Objects > 3.2.3: Defining Methods for an Object Type
• Defining methods:
obj.methodName = function_name
obj.methodName(params)
69
3.2: Creating New Objects > 3.2.4: Defining Methods for an Object Type
car1.displayCar()
car2.displayCar()
70
Object Properties: An Example
myobj=new
for … in
car("Mazda","Miata",1990)
for(variable in object)
for (var i in myobj) {
{ statements }
prop = i + ":" +
myobj[i]+"<BR>"
document.write(prop)
}
71
Creating Objects: Using „with‟ Keyword
• with object:
with (objectName)
{ statement }
with Math
{
x = PI * x
y = x * sin(PI)
}
72
3.4: Deleting Objects
Deleting Objects
• You can remove an object by using the delete operator.
mobs=new Car()
delete myobj // removes the object and returns true
73
Demo
• Complete_ex.html
• Instance_obj.html
• Temp_obj.html
74
JavaScript
76
Module Coverage
• Topics covered in this module are:
JavaScript Document Object Model
Object Properties and Event Handlers
77
6.1: JavaScript Document Object Model
78
6.1: JavaScript Document Object Model
79
6.1: JavaScript Document Object Model> 6.1.1: Object Properties
Object Properties
• Define a particular, current setting of an object.
• Property names are case-sensitive.
• Each property determines it‟s own read-write status.
• Any property you set survives as long as the document
remains loaded in the window.
• For example:
document.forms[0].phone.value = “555-1212”
document.forms[0].phone.delimiter = “-”
80
6.1: JavaScript Document Object Model> 6.1.2: Object Methods
Object Methods
• Command the script gives to that object.
• Some methods return values, but that is not a
prerequisite.
• Predefined by the object model
Assign additional methods to an existing object.
81
6.2: Event Handlers
Event Handlers
• Specify how an object reacts to an event.
Event can be triggered by a user action or a browser action.
82
6.2: Event Handlers
83
6.3: Window Object
84
6.3: Window Object > 6.3.1: Properties
window.defaultStatus=“Javascript Examples”
• parent
• frames
parent.frames.length parent.frames[0]
• onerror
window.onerror=null
• opener
85
6.3: Window Object > 6.3.2: Methods
window.alert(“Display Message”)
• confirm(message)
• prompt(message,[defaultReply])
var input=
86
6.3: Window Object > 6.3.2: Methods
newwin=window.open(“new/URL”,”NewWindow”,
“toolbar,status,resizable”)
• close()
• moveBy(deltaX,deltaY), moveTo(x,y)
• resizeBy(deltaX,deltaY),
resizeTo(outerwidth,outerheight)
• scrollBy(deltaX,deltaY), scrollTo(x,y)
87
6.4: Frame Object
Frame Object
• Properties, methods and event handlers are same as the
window object.
• Behaves exactly like a window object, except that it is
created as part of a frameset by another document.
• Event Handlers:
OnBlur onDragDrop onMove onUnload
88
Demo
• Window_object.html
• setTimeOut_method.html
• Window_ex.html
• setInterval_method.html
89