Computer 8 Module 10
Computer 8 Module 10
8
Fundamentals of Web
Application Development
Module 12 (Week 3 and 4)
Other Objects
The HTML Document Object Model
This lesson explains some objects that are used in javascript program (i.e. Window Object,
Document Object, History Object, and Location Object).
Page | 2
Q. OTHER OBJECTS
Aside from the objects that you encountered earlier in this chapter, there are additional
objects that you can use to manipulate or retrieve information about the page. There is no need
to memorize the properties and methods of the objects. Just go over and find out their possible
uses.
a. Window Object
The Window object is the main or top-level object in Javascript. It has the following
properties and methods that you can use
Page | 3
URL Returns the URL of var a The URL of the
the current page a = document.URL current page
document.write(a)
write(value) Inserts text or HTML document.write( “Hello Hello World
elements into the World”)
Value can be any value. page
You may place two or
more values as
arguments. Use commas
to separate the values.
</script>
var x
x =
document.getElementsByNa
me (“T1”)
alert (“There are” +
x.length + “textboxes
that use the T1 name”)
</script>
getElementsByTagName Returns a collection <body> There are 3 input
(Tagname) of objects with the <input name=”T1” /> elements
<input name=”T2” />
specified tagname
<input name=”T3” />
Tagname refers to the
HTML tagname <script type=
“text/javascript”>
var x
x =
document.getElementsByTa
gName (“input”)
alert (“There are” +
x.length + “input
elements”)
</script>
Page | 4
c. History Object
The History object is an object created by Javascript that contains an array of URLs that
have been visited by the user.
Property/ Method Description Example Result/Output
length Returns the number document.write(“You have displays the
of URLs in the visited”, history.length, number of visited
history list “pages”) pages
back() Loads the previous <a href = “” loads the
URL. Simulates the onclick=”history.back(); previous URL
clicking of the Back return false”> once the link is
Click to go back </a>
Button. clicked
forward() Loads the next URL. <a href = “” loads the next
Simulates the onclick=”history.forward() URL once the
clicking of the Next ; return false”> link is clicked
Click to go forward </a>
button
go(index) Loads a URL in the <a href = “” onclick = loads the next
history list based on “history.go(-1); return URL once the
Index is a numeric value the given number. false”> link is clicked
Click to go back</a>
that refers to a URL in <a href= “”
the history list. onclick=”history.go(1); loads the next
A negative number return false” URL once the
loads the previous URL Click to go forward </a> link is clicked
d. Location Object
The Location object is an object created by Javascript that contains information about
the page is currently loaded.
Page | 5
R. THE HTML DOCUMENT OBJECT MODEL
The HTML Document Object Model (HTML DOM) provides a standardized way to
access objects in HTML. All HTML elements have their own set of properties and methods that
you can use. The History and Location objects are created by Javascript and are not part of the
HTML DOM.
Create an HTML file and copy the script beow to see how the contents of a multi-line textbox
can be accessed and changed using Javascript.
<html>
<head>
<script type = “text/javascript”>
function addtext(t)
{
var d
d = document.getElementById(“displaybox”)
d.value = d.value + t
}
</script>
</head>
<body>
<p align=”center” onclick=”addtext(‘click ‘)”
ondblclick = “addtext(‘double-click ‘)”
onmouseover = “addtext(‘mouseover ’)”
onmouseout = “addtext(‘mouseout ‘)”>
Click me
</p>
The script above will display a page similar to the image below.
The textarea (multi-line textbox) has an Id value of
displaybox while the paragraph element uses events to
trigger the addtext() function. The textarea in the
sample script is created by using the <textarea> tag:
<textarea cols = “30” rows = “10” id =
“displaybox”></textarea>
You will learn more aout the textarea and other similar
objects in the next chapter. In the meantime, let us
focus on how to access and modify the content of the
textarea using Javascript.
The addtext() function modifies values of the textbox by
performing the following:
You should see the text in the textarea change as you interact with the paragraph element.
Page | 6
COMPUTER SCIENCE 8 (MODULE 12)
Identify the following questions. Specify the name of the object and write a period (.) before the
property/method. (Example: document.referrer – returns the URL of the page that loaded the
current page.)
Define the setInterval(“statement”, milliseconds) from Window Object and give 1 example with
its result/output.
Page | 7
Enumerate the Property/Method of the following objects
Direction: Explain what the code does. (Practice the code and do not submit your answer).
<html>
<head>
<title>DOM</title>
<script type = "text/javascript">
function changeBackground(eventDone){
if (eventDone == 1){
document.body.style.backgroundColor = "red"
document.getElementById("context").innerHTML = "CLICKED"
}else if(eventDone == 2){
document.body.style.backgroundColor = "blue"
document.getElementById("context").innerHTML = "DOUBLE-CLICKED"
}else if(eventDone == 3){
document.body.style.backgroundColor = "green"
document.getElementById("context").innerHTML = "MOUSEOVER"
}else{
document.body.style.backgroundColor = "brown"
document.getElementById("context").innerHTML = "MOUSEOUT"
}
}
</script>
</head>
<body>
<div style="text-align: center">
<button onclick="changeBackground(1)"
ondblclick = "changeBackground(2)"
onmouseover = "changeBackground(3)"
onmouseout = "changeBackground(4)">
Click me
</button>
</div>
Page | 8