06DOM
06DOM
Model
CITS3403: Agile Web Development
• The DOM tree also includes nodes for the execution environment in a browser
• Window object represents the window displaying a document
– All properties are visible to all scripts
– Global variables are properties of the Window object
• Document object represents the HTML document displayed
– Accessed through document property of Window
– Property arrays for forms, links, images, anchors, …
• The Browser Object Model is sometimes used to refer to bindings to the browser, not
specific to the current page (document) being rendered. This includes:
– Type of browser
– User’s history
– Cookies
– Screen size
– Location (url)
– Geolocation
– Local (browser) storage
DOM Tree in More Detail
Source: tech.irt.org
JavaScript and the DOM
document.forms[0].element[0]
• Example
<form name = "myForm" action = "">
<input type = "button" name = "pushMe">
</form>
document.myForm.pushMe
• In order to work, all elements from the reference
element up to, but not including, the body must
have a name attribute
• Names are required on form elements by server-side
scripts
• You can also select all elements by tag name.
Method 3: Using ID
document.getElementById( on")
Other Access Methods
• Checkboxes and radio buttons have an implicit array, which has their name as the array name
<form id = "topGroup">
<input type = "checkbox" name = "toppings"
value = "olives" />
...
<input type = "checkbox" name = "toppings"
value = "tomatoes" />
</form>
...
var numChecked = 0;
var dom = document.getElementById("topGroup");
for index = 0; index < dom.toppings.length;index++)
if (dom.toppings[index].checked]
numChecked++;
DOM Tree Traversal and Modification
• As we’ve seen each element in an HTML document has a corresponding Element object
in the DOM representation
• The Element object has methods to support
– Traversing the document
• that is, visiting each of the document nodes
– Modifying the document
• for example, removing and inserting child nodes
<!-- Here's an example of how the countTags( ) function might be used -->
• There are also methods that allow you to modify or construct a DOM tree. eg:
– The insertBefore method inserts a new child of the target node
– replaceChild will replace a child node with a new node
– removeChild removes a child node
– appendChild adds a node as a child node at the end of the children
you can construct part or whole document dynamically!
l This is what front-end frameworks like Angular or React do: they dynamically build the
entire document on the client side.
l Document writing methods include:
– open()
– close()
– write()
– writeln()
Example
<script type="text/javascript">
function createNewDoc() {
var newDoc=document.open("text/html","replace");
var txt="<html><body>Learning about the DOM is FUN!</body></html>";
newDoc.write(txt);
newDoc.close();
}
</script>
• Batch program
2. Assign the event handler to the appropriate property of the element’s object
<input type = button id = myButton />
.
.
document.getElementById( myButton ).onclick =
myButtonHandler;
– statement must follow both handler function and form element so (JavaScript) interpreter has seen both
– note: just function name, not function call (or string)
Events and their Tag Attributes
Tag Attributes and their Tags
function load_greeting () {
alert( You are visiting the home page of\n +
Pete s Pickled Peppers \n + Welcome!!! );
}
Mouseover events
• Any HTML element can be have a mouseover event associated with it.
Handling Events from Text Box and Password
Elements
• An important use of events is to validate the content of forms, without using bandwidth and
time to access a remote server.
• By manipulating the focus event the user can be prevented from changing the amount in a
text input field
• DOM 2 defines a process for determining which handlers to execute for a particular event
• The event object representing the event is created at a particular node called the target
node
• The process has three phases…
• In the capturing phase each node from the document root to the target node, in order, is
examined.
– If the node is not the target node and there is a handler for that event at the node and the
handler is enabled for capture for the node, the handler is executed
• Then all handlers registered for the target node, if any, are executed
• In the bubbling phase each node from the parent of the target node to the root node, in
order, is examined
– if there is a handler for that event at the node and the handler is not enabled for capture for the
node, the handler is executed
Some event types are not allowed to bubble: load, unload, blur and focus among the HTML
event types
Event Propagation