Lecture-8 (2) DOM
Lecture-8 (2) DOM
Java Script Window object represents the window
in which the browser displays documents.
The Window object provides largest enclosing
referencing environment for scripts.
All javaScript variables are properties of some object
The Window properties are visible to all scripts in the
document --they are the globals.
Variables created by client-side script become (new)
property of Window
Window properties
Property document is a reference to the
JavaScript Document object that the window
displays
Property frames is an array of references to the
(may be multiple) frames of the document
Property forms is an array of references to the
forms in the document.
Each Form object has an elements array of references
to form's elements.
DOM Document Object Model
Provides specifications for portability for users to
write code in programming languages to create
documents, work on their structures, change, add,
or delete elements and their context
W3c development since early 90's
DOM 0 supported by JavaScript capableborwsers
DOM 1, Oct 98; focused on HTML and XML
DOM 2, Nov 00; support CSS, event, tree
manipulation events NS6 but not IE6
DOM
DOM documents have a treelike structure
DOM is abstract model defines interface between
HTML documents and application programs
DOM is an OO model -document elements are
objects with both data (properties) and operations
(methods)
Language supporting DOM must have binding to
the constructs
DOM
In JavaScript binding,
HTML elements -->objects
HTML element attributes --> properties
<input type = "text" name = " address>
would be represented as one object with two properties,
type and name, with the values "text" and
"address"
For a description of tree traversal, adding and
deleting nodes go to in http://www.w3c.org
Element Access in JavaScript
DOM 0 uses forms and element arrays
<form action ="">
<input type = "button" name = "pushMe">
</form>
DOM 0 address: documents.forms[0].element[0]
Element names -- element and all ancestors to have name
attributes (but body)
<form name ="myForm" action= "">
<input type = "button" name = "pushMe">
</form>
Element name address: documents.myForm.pushMe
Element Access in JavaScript
DOM 1 adrressing via JavaScript method:
getElementId
<form action ="">
<input type = "button" name = "pushMe">
</form>
DOM address:
document.getElementId("pushme")
Events/Event Handling
HTML 4.0 provided standard --DOM 0
Supported by all browsers that include JavaScript
DOM 2 comprehensive event model
Supported by Mozilla and NS6 browsers
Not supported by IE6!!
Events/Event Handling
Event-driven: code executed resulting to user or
browser action
Event: a notification that soemthing specific
occurred -- by browser or user
Event handler: a script implicitly executed in
response to event occurrence
Registration: the process of connecting event
handler to event
Events/Event Handling JavaScript
Events are JavaScript objects --> names are case
sensitive, all use lowercase only.
(Method write should never be used in event handler.
May cause document to be written over.)
JavaScript events associated with HTML tag
attributes which can be used to connect to event-
handlers
Table 5.1 (pg 182)
Events/Event Handling JavaScript
One attribute can appear in several different tags:
e.g. onClick can be in <a> and <input>
HTML element get focus:
When user puts mouse cursor over it and presses the
left button
When user tabs to the element
By executing the focus method
Element get blurred when another element gets focus
Table 5.2 pg. 184-184
Event Handling JavaScript
Event handlers can be specified two ways
Assigning the event handler script to an event tag
attribute
onClick = "alert('Mouse click!');"
onClick = "myHandler();
Assigning them to properties of JavaScript object
associated with HTML elements
Event Handling JavaScript
The load event: the completion of loading of a
document by browser
The onload attribute of <body> used to specify
event handler:
http://www.ens.utulsa.edu/~class_diaz/cs2043/load.htmlT
he unload event: used to clean up things before a
document is unloaded.
Radio Buttons
<input type ="radio" name ="button_group"
value = "blue" onClick ="handler">
The checked property of radio button object is
true if the button is pressed
All button in the group have the same name, must
use DOM address element
var radioElement =
document.myForm.elements;
Radio Buttons