5.2 DOM History and Levels
5.2 DOM History and Levels
4
• An event in a browser is an occurance of
potential interest.
– Ex: mouseover and mouseout events
6
Intrinsic Event Handling
7
• When using intrinsic event use meta element in the head
of the document
• Intrinsic event attribute value is a script; what language
is it written in?
• HTTP Content-Script-Type header field specifies default
scripting language
• meta element allows document to specify values as if
they were header fields
8
Example HTML document calls window.alert in
response to variety of events
9
5.4 Modifying Element Style
• DOM access to change the style of an HTML document element in
Change
background color
of element
containing cursor
10
Like rollover, style needs to be modified
both when entering and exiting the element.
Child Elements
14
5.5.1 Node Object
• The document tree accessible to Java script programs run
within a DOM2-browser
• There are many types of nodes in the DOM document tree,
representing elements, text, comments, the document type
declaration, etc.
• The DOM is defined by standard properties and methods.
• Properties are often referred to as something that is (i.e. the name of a
node).
• Methods are often referred to as something that is done (i.e. remove a
node).
16
Example HTML fragment
17
• In the HTML DOM, each node is an object.
• Objects have methods and properties that can
be accessed and manipulated by JavaScript.
• Three important node properties are:
– nodeName
– nodeValue
– nodeType
18
Document Tree: Node
19
Document Tree: Node
20
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
21
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Example HTML document
22
Example HTML document
23
• String produced by TreeOutline():
24
Java Script Program for List reordering
Depth in tree
25
5.5.2 Example List Reordering
Example tree modifying methods used
removeChild(Node) refer slide 21
To display a list in the browser that can be
reordered by the user
If the user clicks on the list item, then it will be
swapped with the item below it in the list
Java script function is written switchItems()
Function called from the onclick attribute of each li
element in the list
Two steps
Search through the linked list of nodes to find the
siblings of the node corresponding to the list item
that was clicked
Perform search by using nextSibling property
until find the node for the list item following
the one clicked
Remove the node for the list item following the
one clicked and reinsert before the node which is
clicked
5.5.3 The document Node
The document object itself is considered to be a Dom
tree node
html element is the root of HTML document
In DOM the document object is treated as the root of
useful document properties
title, body, getElementById(string),
getElementByTagName(string),
5.5.4 The Element Node
• Nodes of type ELEMENT_NODE are instances of the
Element host object
• Some methods of Element are
•getAttribute(String)
•setAttribute(String, String)
•hasAttribute(String)
30
5.5.6 HTML Convenience Properties
• DOM’s HTML module defines a number of convenience
properties used to set and retrieve element attribute values
without calling setAttribute() and getAttribute()
• Consider the two statement
element.setAttribute(“id”, “element3”);
element.id =“element3”;
• First statement is recommended
• Clear to say accessing HTML attributes
• Special cases the statement two cannot be used
31
•Element.class class is a reserve word in javascript
5.6 DOM Event Handling
• In this the code is executed in mozilla 1.4
• Much of it will not work in IE6
• Note: IE6 has a different event model
5.6.1 The Event Object and Event Listeners
• When an event occurs , an instance of a host object named
Event is created.
• Event instance created for each event
• Event instance contains information about the event:
– type: name of event (click, mouseover, etc.)
– target: Node corresponding to document element that
generated the event (e.g., button element for click, img
for mouseover). This is the event target.
Event instance properties
32
• Once an event instance is created, it is sent to
certain event listeners.
• Event listener is simply a function that takes a
single argument that is an instance of event
• JavaScript event listener: function that is called with
Event instance when a certain event occurs
• An event listener is associated with a target
element by calling addEventListener() on the
element
33
Example
•Document contains an element with an id of msgbutton
•Java script is executed whenever the mouse is clicked on
the msgButton element and alert box is displayed
•Many of the DOM2 event type same as HTML intrinsic event
•No DOM2 double-click event
Event
target
Event type Event handler
Event instance
Definition of
event
handler
34
OUTPUT
35
5.6 Mouse Events
• Event instances associated with six DOM2
mouse events
– Click, mousedown, mouseup, mousemove,
mouseover, mouseout
– Detail property can be used to detect a
double-click event
– A double click is represented by an Event
instance with a type of click and a detail value
of 2
– All HTML intrinsic events except keypress,
keydown, keyup, and dblclick
36
DOM Event Handling:
Mouse Events
37
DOM Event Handling:
Mouse Events
• Example: mouse “trail”
38
• HTML document:
Create
“blips” String uniquely
identifying this div
Add event
listener
41
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0