Professional Documents
Culture Documents
Lecture 18
Objects, Properties, Methods
(Web Development Lecture 6)
1
During the last lecture we continued
our discussion on Interactive Forms
• We got our first taste of JavaScript – the
object-based language that we will be
employing throughout the rest of the Web
development part of this course
2
During Today’s Lecture …
• We will have a more formal introduction to
JavaScript and client-side scripting
5
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” );
}
}
9
Disadvantages
• Client-side scripts do not work with all browsers
• JavaScript:
– Is an interpreted language
– Supports event-driven programming
– Is object-based 11
Some of things that JavaScript cannot do!
• The following file ops. on the client computer:
– Read -- Modify
– Rename -- Delete
– Create
14
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 16
JavaScript is Object-Based
• Everything that JavaScript manipulates, it
treats as an object – e.g. a window or a button
A collection
All objects have the
of properties
“name” property: it
& methods
holds the name of
the object (collection)
name method 2
prop 1
prop 3 prop 5
prop 2
method 1 prop 4 method 3
19
Example: A Bicycle
name accelerate()
color
pressure direction
height
inflate() speed turn() park()
20
Example: JavaScript’s “window” Object
name open()
width
document status
height
close() location alert()
21
Properties
• Objects may have a single or several properties
objectName.propertyName
Examples:
window.width
button.value
23
24
25
<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> 26
</HTML>
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function changeStatus() {
window.status=“Mouse has touched the button”;
}
propert new
y value
The JavaScript code included as an attribute of the
“Submit” button:
onMouseOver=“changeStatus()”
27
28
29
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function gotoURL() {
window.location=“http://www.vu.edu.pk/”;
}
propert new
y value
The JavaScript code included as an attribute of the
“Go to VU” button:
onMouseOver=“gotoURL()”
30
You should be connected to the
Internet for the successful execution
of the example that we just
discussed
31
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
32
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
33
One More Example:
34
35
36
The main code segment that goes between the
<SCRIPT>, </SCRIPT> tags in the HEAD:
function changeBgcolor() {
window.document.bgColor = “pink”;
}
propert new
y value
The JavaScript code included as an attribute of the
“Change Color” button:
onMouseOver=“changeBgcolor()”
37
In addition to “bgColor”, there are many other
properties of the “document” object, e.g.
• fgColor • cookie
• linkColor • forms[ ]
• title • images[ ]
• URL • links[ ]
• …
• referrer
• …
• lastModified
• …
38
Read-Only Properties
39
• We have learnt how to modify the properties of
objects
40
41
42
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);
}
44
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”
45
Referring to a Method
dot
objectName.methodName( argumnets )
Examples: Info is
window.close() passed on to
the method
button.click()
through one
or more
arguments
46
A few more methods associated
with the “window” object
• alert()
• confirm()
• prompt()
• close()
• open()
• focus()
• blur()
• setTimeOut()
47
48
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
52