Dynamic HTML XHTML CSS
Dynamic HTML XHTML CSS
manipulate manipulate
Scripting
Language
DHTML
1
Accessing HTML Elements Accessing Elements by Paths
function execute() {
• All HTML elements (objects) are accessed var img = document.images[0]; img.src="lighton.gif"
var inx = document.forms[0].elements[0]; inx.value="xx"
through the document object
var iny = document.forms["form1"].elements["y"]; iny.value="yy"
• document itself is automatically created } head
• Several ways to access a specific element <p><img src="lightoff.gif" alt="light off" id="img1" /></p>
<form id="form1" method="get" action="nosuch"><p>
- paths in the DOM tree
<input type="text" name="x"/>
- retrieval by ID <input type="text" name="y"/>
- retrieval by tag <input type="reset"/></p>
</form> body
function execute() {
var theDiv = document.getElementById("div1");
if (theDiv.style.visibility=="hidden")
{theDiv.style.visibility="visible" }
else {theDiv.style.visibility="hidden" }
} head
2
Event Example
<html>
<head>
<title>Simple Events</title>
• Elements can have registered event listeners • The simplest (and most common) way to register a
associated with certain types of events listener is by adding this attribute to the element:
ontype = "JavaScript code"
• When an event takes place, the listeners that are
• For example:
registered for this event are invoked
<img src="img.gif" onmouseover="alert('!')" />
• Typically, a listener is described by a scripting
• The JavaScript code has access to the following objects:
code (e.g., JavaScript)
- this - the element (e.g., the image defined above)
- This code is executed upon listener invocation
- event - the event object
3
Some Event Types Another Example
<html>
load click reset <head><title>Event Object Example</title>
unload dblclick select <script type="text/javascript">
abort mousedown submit function execute(e) {
mousemove alert(" x: " + e.clientX + ", y: " + e.clientY +
mouseup " mouse button: " + e.button); }
keydown change
mouseover </script></head>
keypress blur
<body onmousedown="execute(event)"
keyup focus
style="cursor: pointer;
position:absolute; width:100%; height:100%"> </body>
</html>
4
Form Validation - Another
Form Submission
Example (cont)
• A form can be submitted without the special
<body>
<form id="email-form" action="myurl" method="get" submission button
onsubmit="return validateEmail()">
<p>
• Use the function form.submit() to submit a
Name: <input type="text" name="Name:" /> <br/> specific form from JavaScript code
Email: <input type="text" name="email" />
<input type="submit" />
</p>
</form>
</body>
• When we click on the image, which of the functions • Event Bubbling: events propagate through the
should be executed? elements in bottom-up order
- Answer: all of them! - i.e., from the most specific to the most general
• In what order?
• Whenever an element is visited, its listeners are
• Two different models:
triggered
- Microsoft (impl. in IE)
• In our example: img → p → div
- W3C (impl. in Mozilla, Opera 7, Konqueror)
5
W3C Model Event Flow (cont)
• In the W3C model, there are two traversals: • A listener can be registered in either the capturing or the
• What will happen if we replace f2 with the • A listener can be dynamically registered by using
following? JavaScript code
• Microsoft:
function f2(e) {
element.ontype = functionName
alert("2");
if(e.stopPropagation) e.stopPropagation(); element.attachEvent("ontype", functionName)
if(e.cancelBubble!= undefined) e.cancelBubble=true;
• Note that the function is given as an object
}
• The function is called without arguments
• The event can be accessed using window.event
6
Structure Manipulation DOM Tree Manipulation
- change the text under elements - add created nodes to the DOM tree
- DOM tree manipulation (W3C specification) • To create new nodes, use these methods of document:
7
Previous Example with
innerHTML
function replace(button) {
d = document.getElementById("d1");
The window Object
d.innerHTML = "<h1>This is a header<\/h1>";
button.disabled=true;
}
8
Opening New Windows Accessing Window Frames