Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help >  WWW

DHTML

DHTML stands for Dynamic HyperText Markup Language, a term describing the combined use of HTML, style sheets and scripts to change documents while they're being displayed. In this document none of these components will be covered in any depth, though some annotated examples will be provided. First, a few reminders about some key concepts.

More about HTML

Before using javascript you need to be conversant with HTML. If you're going to change something, you need to be able to specify what to change and when it will change. Objects created by HTML can be given names, and can be of various types. Objects also have properties (like color, etc). These properties can be used on static web pages and stylesheets to control appearance. Each type of object has a set of valid properties. See our HTML4 page for more details. 2 properties not often used in static HTML files are id and display, both of which we'll use later

There are ways to group objects together so that they can in some way be treated as a single entity

Objects also have properties to control position. For example,
<p id='MYDIV' style="position:relative; left:100px; bottom:30px; color:red; ">HELLO</p>
produces

HELLO

which should be 100 pixels further right and 30 pixels further up than usual.

There's another type of property that's more to do with interaction. Buttons, for example, have an onclick event handler property so that you can call a particular function when the button is pressed.

Combining Javascript and HTML

No attempt will be made here to describe Javascript. If you know C++ or Java you know enough about programming to understand the fragments here. The javascript code goes inside a SCRIPT tag. If you look at the source of this page using the browser menu's "Page Source" or "Show code" option you'll see the code for this page. Javascript offers a way to access the components of the page and change their properties (their color, position, etc). You can ask for a list of all the components, or pick them out by type or by name. Note however, that the HTML names for the properties aren't quite the same as those used by Javascript. For example, HTML's margin-left would cause Javascript trouble because the '-' would be treated as a minus sign. The Javascript equivalent is marginLeft

Counting objects and writing HTML code

The document has a structure that javascript routines can discover. It's easy to list elements of a particular type. The line

var nodes = document.getElementsByTagName('h2')

finds out all the h2 objects and puts them into an array called nodes. You can loop through these nodes to find out more about each of them. The number of nodes is provided by nodes.length.

A routine called document.write writes a string to the current page. If you call it while a page is being created, it generates text "on-the-fly". For example, the following code (which is in the head of this page)

function headcount() { var nodes = document.getElementsByTagName('h2') document.write('There are ' + nodes.length + ' h2 headers in this document so far'); }
is called at this point of the document by using this code
<script> headcount() </script>
The output is -

Controlling the appearance and visibility of objects

Try clicking on these buttons to change some C++ text and some Java text followed by even more C++ text

The code for the start of the affected text has "<span class=Cplusplus> some C++ text</span>" which makes the spanned text belong to the Cplusplus class. The Emphasise C++ button calls a Javascript routine that makes all Cplusplus objects bold, and all Java objects grey. The code to create the button is
<button onclick="changeClassStyle('Cplusplus','bold','normal','black',''); changeClassStyle('Java','normal','normal','grey','')" >Emphasise C++</button>
The changeClassStyle code that's called is rather long, but it can be adapted without having to be fully understood.
function changeClassStyle(el,fw,fs,clr,dis){ if(document.getElementsByTagName)//check for obj { var nodes = document.getElementsByTagName('span') var max = nodes.length for(var i = 0;i < max;i++) { var nodeObj = nodes.item(i); if(nodeObj.className== el) { nodeObj.style.fontWeight = fw; nodeObj.style.fontStyle = fs; nodeObj.style.color = clr; nodeObj.style.display =dis; } } } }
For those familiar with computing languages, this shouldn't be too bewildering. The first line names the function, which has 5 arguments. Whereas in the previous example we used getElementById to find the element that had a particular name, here we use getElementsByTagName to get all the document's span objects (you can use the same idea to pick out all the img objects etc). For each span object it then checks the className. If that matches the requested class, then the properties are changed to what the user requested.

Text-wrapping

floating right floating left An object's float property determines how it fits into the layout. By default it's set to none. Alternatives are right or left (used for the panels in this paragraph). Text by a float element will start beside the element then continue underneath. Note that the panels here have been given margins and some visible borders.

Animation

The position and background image of objects can be changed. Objects also have a clipping property which controls how much of the image is visible. You can control the animation with a for loop or set it running using javascript's setInterval routine, which repeatedly runs a routine of your choice at an interval you choose. Over-use of this facility may irritate users, but since no javascript introduction would be complete without some animation, here's a button that moves when clicked on -

It uses a routine called moveme in the HEAD section of this page to change the button's x coordinate. The button's position property has been set to relative. If it's left as the default static then the object won't move.

You can also obscure one object with another, then reveal the underlying object. Try clicking on the temple below

Non-document Properties available using Javascript

The javascript language offers access to other information that might be of use. The following button when clicked on will show the page with the link that led to this page

And here some information about your browser is printed using document.write to print the values of navigator.appName and navigator.appVersion

Debugging

If you write pages with javascript, mistakes aren't always easy to find because error messages aren't displayed. Some browsers offer help. Firefox for example has a Toolsmenu with helpful options including a DOM Inspector

If you have Firefox 3 you can add Firebug to help you diagnose CSS and JavaScript problems.

Don't forget that the internal representation of the document won't be complete until the page is fully processed. We'll call headcount again, to see how page production has progressed since we last called it. The output is now -

© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: April 2011