Week 2
Week 2
Lecture 1
Objects, Properties, Methods
(Introduction to JavaScript)
1
During Today’s Lecture …
• We will have a formal introduction to JavaScript
and client-side scripting
4
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function checkForm() {
if ( document.sendEmail.sender.value.length < 1) {
window.alert( “Empty From field! Please correct” );
}
}
8
Disadvantages
• Client-side scripts do not work with all browsers
• JavaScript:
– Is an interpreted language
– Supports event-driven programming
– Is object-based 10
Some of things that JavaScript cannot do!
• The following file ops. on the client computer:
– Read -- Modify
– Rename -- Delete
– Create
13
Case Sensitivity
• HTML is not case sensitive. The following
mean the same to the browser:
– <HTML> -- <html>
– <Html> -- <htMl>
• JavaScript:
– Is an interpreted language
– Supports event-driven programming
– Is object-based 15
JavaScript is Object-Based
• Everything that JavaScript manipulates, it
treats as an object – e.g. a window or a button
A collection
of properties
All objects have the
& methods “name” property: it
holds the name of
the object (collection)
name method 2
prop 1
prop 3 prop 5
prop 2
method 1 prop 4 method 3
18
Example: A Bicycle
name accelerate()
color
pressure direction
height
inflate() speed turn() park()
19
Example: JavaScript’s “window” Object
name open()
width
document status
height
close() location alert()
20
Properties
• Objects may have a single or several properties
objectName.propertyName
Examples:
window.width
button.value
22
23
24
<HTML>
<HEAD>
<TITLE>Change Property Demo # 1</TITLE>
<SCRIPT>
function changeStatus() {
window.status = “Mouse has touched the button”;
}
</SCRIPT>
</HEAD>
<BODY>
<H1>Change Property Demo # 1</H1>
<FORM name=“dummy” method=“” action=“”>
<INPUT type=“submit” name=“” value=“Change Status“
onMouseOver=“changeStatus()”>
</FORM>
</BODY> 25
</HTML>
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function changeStatus() {
window.status=“Mouse has touched the button”;
}
property new value
30
A Suggestion
• Please try the pieces of code that I just
demonstrated to you to change the
status and location properties of the
“window” object yourself
31
Types of Objects
• JavaScript objects
– Objects that are part of JavaScript
– Examples: window, document
• Browser objects
– Objects that contain info not about the
contents of the display, but the browser itself
– Examples: history, navigator
• User-defined object
32
One More Example:
33
34
35
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function changeBgcolor() {
window.document.bgColor = “pink”;
}
property new value
• fgColor • cookie
• linkColor • forms[ ]
• title • images[ ]
• • links[ ]
URL
• …
• referrer
• …
• lastModified
• …
37
Read-Only Properties
38
• We have learnt how to modify the properties of
objects
39
40
41
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function changeBgcolor() {
oldColor = window.document.bgColor;
window.document.bgColor = “pink”;
window.alert("The old color was " + oldColor);
}
43
Methods: Functions (code, instructions,
behavior) associated with objects
• Methods are functions associated with
an object that can be used to
manipulate that object
• Example:
– window.close()
– Here “close()” is a method that has been
defined for the “window” object. Its
function is to close the “window”
44
Referring to a Method
dot
objectName.methodName( argumnets )
Examples: Info is
window.close() passed on to
the method
button.click()
through one
or more
arguments45
A few more methods associated
with the “window” object
• alert()
• confirm()
• prompt()
• close()
• open()
• focus()
• blur()
• setTimeOut()
46
47
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function vuWindow() {
window.open(“http://www.vu.edu.pk/”);
}
method argument
51