BIT 4303 DL Chapter Seven & Eight
BIT 4303 DL Chapter Seven & Eight
Chapter seven
What is the DOM?
The HTML Document Object Model (HTML DOM) defines a standard way for accessing and
manipulating HTML documents. It presents an HTML document as a tree-structure (a node tree),
with elements, attributes, and text.
With JavaScript you can restructure an entire HTML document. You can add, remove, change,
or reorder items on a page.
This access, along with methods and properties to add, move, change, or remove HTML
elements, is given through the Document Object Model (DOM).
All browsers have implemented this recommendation, and therefore, incompatibility problems in
the DOM have almost disappeared. It can be used by JavaScript to read and change HTML,
XHTML, and XML documents.
<html>
<head>
Advanced Web @ Fred 2022
<script type="text/javascript">
function ChangeColor() {
document.body.bgColor="yellow" }
</script>
</head>
<body onclick="ChangeColor()">
Click on this document!
</body>
</html>
JavaScript Objects
Window: The top level object in the JavaScript hierarchy. The Window object represents a
browser window. A Window object is created automatically with every instance of a <body> or
<frameset> tag
Navigator: Contains information about the client's browser
Screen: Contains information about the client's display screen
History: Contains the visited URLs in the browser window
Location: Contains information about the current URL
DOM Objects
DOM Anchor DOM Area
DOM Base DOM Body
DOM Button DOM Event
DOM Form DOM Frame
DOM Frameset DOM IFrame
DOM Image DOM Input Button
DOM Input Checkbox DOM Input File
DOM Input Hidden DOM Input Password
DOM Input Radio DOM Input Reset
DOM Input Submit DOM Input Text
DOM Link DOM Meta
DOM Object DOM Option
DOM Select DOM Style
Advanced Web @ Fred 2022
Window Example
<html>
<body>
<script type="text/javascript">
window.status="Some text in the
status bar!!"
</script>
</body>
</html>
<html>
<body>
<script type="text/javascript">
myWindow=window.open('','','width=200, height=100')
myWindow.document.write("This is 'myWindow'")
myWindow.focus()
</script>
</body>
</html>
Browser Example
<html>
<body>
<script type="text/javascript">
document.write("<p>Name: ")
document.write(navigator.appName + "</p>")
</script>
</body>
</html>
Screen Example
<html>
<body>
<script type="text/javascript">
document.write("<p>Width: ")
document.write(screen.width + "</p>")
</script>
</body>
</html>
Anchor Example
<html>
<head>
Advanced Web @ Fred 2022
<script type="text/javascript">
function changeLink() {
document.getElementById('myAnchor').innerHTML="W3Schools";
document.getElementById('myAnchor').href="http://www.w3schools.com";
document.getElementById('myAnchor').target="_blank"; }
</script>
</head>
<body>
<a id="myAnchor" href="http://www.microsoft.com">Microsoft</a>
<input type="button" onclick="changeLink()" value="Change link">
</body>
</html>
Table Example
<head>
<script type="text/javascript">
function changevalue() {
var x=document.getElementById('mytable').cells
x[0].innerHTML=“Ali"
x[1].innerHTML=“Veli"
x[2].innerHTML=“Selami" }
</script>
</head>
<body>
<table id="mytable" border=1>
<tr>
<td id="col1"> John </td>
<td id="col2"> Doe </td>
<td id="col3"> Alaska </td>
</tr>
</table>
<input type="button" value="Change value" onclick="changevalue()">
Advanced Web @ Fred 2022
Chapter eight
What is DHTML?
DHTML is a combination of technologies used to create dynamic and interactive Web sites.
(Combination of HTML 4.0, Style Sheets, DOM and JavaScript)
DHTML is not a standard defined by W3C. DHTML is a "marketing term" - used by Netscape
and Microsoft to describe the new technologies the 4.x generation browsers would support.
DHTML Technologies
HTML 4.0
Cascading Style Sheets (CSS)
The Document Object Model (DOM)
JavaScript
First, the element must specify the position attribute (relative or absolute).
Layer 2
<div style="position:relative;
<html>
<body>
<h1 id="header">My header </h1> <script type="text/javascript">
document.getElementById('header').style.
color="red"
</script>
</body>
</html>
With an event handler you can do something with an element when an event occurs.
<html>
<head>
<script type="text/javascript">
function changecolor() { document.getElementById('header').style.color="red" }
</script>
</head>
<body>
<h1 id="header" onclick="changecolor()"> Click on this text</h1>
</body>
</html>
a page is finished loading. Note: In Netscape this event occurs during the
onload
loading of a page!
What is AJAX?
AJAX is not a new programming language, but a technique for creating better, faster, and more
interactive web applications.
• JavaScript
• XML
• HTML
• CSS
XHTML and CSS standards based presentation. Interaction with the page through the DOM.
Data interchange with XML and XSLT. Asynchronous data retrieval with XMLHttpRequest.
With AJAX, your JavaScript can communicate directly with the server, using the JavaScript
XMLHttpRequest object. With this object, your JavaScript can trade data with a web server,
without reloading the page.
AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server,
allowing web pages to request small bits of information from the server instead of whole pages.
The AJAX technique makes Internet applications smaller, faster and more user-friendly.
In traditional web applications, the user will have to click the "Submit" button to send/get the
information, wait for the server to respond, then a new page will load with the results.
With AJAX, your JavaScript communicates directly with the server, through the JavaScript
XMLHttpRequest object. With an HTTP request, a web page can make a request to, and get a
response from a web server - without reloading the page.
The user will stay on the same page, and he or she will not notice that scripts request pages, or
send data to a server in the background.
By using the XMLHttpRequest object, a web developer can update a page with data from the
server after the page has loaded.
Internet Explorer uses an ActiveXObject, while other browsers uses the built-in JavaScript
object called XMLHttpRequest.
The onreadystatechange Property: stores the function that will process the response from a
server.
The readyState Property: The readyState property holds the status of the server's response.
The responseText Property: The data sent back from the server can be retrieved with the
responseText property.
AJAX Example
<html>
<body>
<script type="text/javascript">
function ajaxFunction() {
var xmlHttp;
try {
// Internet Explorer
try {
xmlHttp=new activeXObject("Msxml2.XMLHTTP");}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) {
document.myForm.time.value=xmlHttp.responseText; } }
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}
</script>
<form name="myForm">
Advanced Web @ Fred 2022
Name:
<input type="text"
onkeyup="ajaxFunction();“
name="username" />
Time:
</form>
</body>
</html>
What is E4X?
E4X is a new extension to JavaScript. E4X adds direct support for XML to JavaScript.
JavaScript = ECMAScript.
ECMAScript is the official name for JavaScript. ECMAScript is the same as JavaScript.
With E4X, you can declare an XML object variable the same way as you declare a Date or an
Array object variable:
E4X Example
<note>
<date>2002-08-01</date> <to>Tove</to>
<from>Jani</from> <heading>Reminder</heading> <body>
Don't forget me this weekend!
</body>
</note>
Advanced Web @ Fred 2022
document.write(x.from)
Jani
var xmlDoc
//code for Internet Explorer
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async=false;
xmlDoc.load("note.xml")
displaymessage()
}// code for Mozilla, Firefox, etc.
else (document.implementation && document.implementation.createDocument)
{xmlDoc= document.implementation.createDocument("","",null)
xmlDoc.load("note.xml");
xmlDoc.onload=displaymessage
}
function displaymessage()
{
document.write(xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue)
}
A beta version of the Mozilla engine (1.8) has limited support for E4X. The first browser to
support E4X is expected to be Firefox 1.1.
What is WML?
WML is a content format for devices that implement the Wireless Application Protocol (WAP)
specification, such as mobile phones.
WML documents are XML documents that validate against the WML (currently version 1.3)
DTD (Document Type Definition).
Pages in WML are called DECKS. Decks are constructed as a set of CARDS.
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN"
"http://www.phone.com/dtd/wml11.dtd" >
<wml>
<card id="main" title="First Card">
<p mode="wrap">This is a sample
WML page.</p>
</card>
</wml>
What is WMLScript?
WML scripts are not embedded in the WML pages. WML pages only contains references to
script URLs.
WMLScript is compiled into byte code on the server before it is sent to the WAP browser.
WMLScript is used to generate message boxes and dialog boxes locally, to view error messages
and confirmations faster.