Javascript DOM - E-Textmodule18
Javascript DOM - E-Textmodule18
Learning Objectives
The last module discusses about how to create Arrays, Functions and Objects. Moreover it also discusses
about the use of built-in Objects in JavaScript.
In this module we try to understand about HTML DOM object hierarchy and to learn about how to
access HTML elements using DOM. Moreover we will learn to work with DOM objects with examples.
DOM - Objects
Here is a simple hierarchy of a few important objects shown in Figure 18.1.
HTML DOM
The HTML DOM is a standard object model and programming interface for HTML. DOM defines the
HTML elements as objects, the properties of all HTML elements, the methods to access all HTML
elements, the events for all HTML elements. The HTML DOM is a standard for how to get, change, add,
or delete HTML elements.
The programming interface defines the properties and methods of each object.
Methods are actions you can perform on HTML Elements.
Properties are values of HTML Elements that you can set or change.
Referencing Objects
Objects can be referenced using their id or name. It should be unique in the hierarchy. It can be
referenced using their numerical position in the hierarchy, by the array index. They can also be
referenced using their relation to parent, child, or sibling like parentNode, previousSibling, nextSibling,
firstChild, lastChild or the childNodes array.
Accessing Elements by ID
Example
function execute() {
var theDiv = document.getElementById("div1");
if (theDiv.style.visibility=="hidden" )
{ theDiv.style.visibility="visible" }
else { theDiv.style.visibility="hidden" }
}
<div id = "div1"> This text can be hidden! </div>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
Property Description
Method Description
Attribute Description
scrollbars Creates scrollbars when the document exceeds the window size
Method Description
This example demonstrates how document object can be used. The write() method can be used with the
document object to display output text "Hello World!". On clicking the button, the number of scripts
found in the document can be displayed using the property "length" of the object as shown in Figure
18.3.
<html>
<body>
<p>Click the button to display the URL of the document.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x = document.URL;
document.getElementById("demo").innerHTML = x;
}
</script></body>
</html>
Figure 18.4 displays the URL of the document. The example shows the use of the property "URL"
used with the document object.
Figure 18.4 Output for Window Object Example
Property Description
length An integer value representing the number of links in the history object
previous Contains the URL of the previous entry in the history list
Method Description
back() Sends the user to the previous page in the history list
forward() Sends the user to the next page in the history list
go(x) Sends back or forward by “x” number of pages in the history list
Figure 18.5 shows the property length of the history object. It displays the number of URL's in the
history list.
Figure 18.5 Output for History Object Example
<html>
<head>
<script>
function goBack()
{
win dow.history.go(-2)
}
</script>
</head>
<body>
<button onclick="goBack()">Go 2 pages back</button>
</body>
</html>
This example uses the method go() of the "history" object. Figure 18.6 displays the button "Go 2 pages
back". On clicking the button, it calls the function goBack() which navigates two pages back and hence
the output shown as the Google home page shown in Figure 18.7.
Figure 18.6 Output for History Object Example
<html>
<body>
<p> <a id="myAnchor"
href="http://www.example.com:4097/test.htm#part2">Exam ple link </a></p>
<p>Click the button to display the port num ber of the link above.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myAnchor").port;
document.getElementById("demo").innerHTML = x;
}
</script>
</ body>
</ html>
This example shows the property "port" used with the Anchor object. The property retrieves the port
number of the anchor given as link and displays it as shown in Figure 18.8.
<html>
<body>
<img border="0" src="grapes.jpg" width="48" height="48">< br>
<img border="0" src="flower.jpg" width="107" height="98"><br>
<img border="0" src="fish.jpg" width="107" height="98"><br>
<p>
<script type="text/javascript">
document.write("<hr><b>This document contains: "
+ document.images.length + " images.</b>")
</script>
</p>
</body>
</html>
This example demonstrate the property length used with the image object. Figure 18.9 displays the
number of images found in the document object.
Summary
This module is discussed about HTML DOM object hierarchy and explains about how to access HTML
elements using DOM. This module also explores about working with DOM objects using JavaScript.