DOM: Document Object Model
DOM: Document Object Model
DocumentFragment HTMLHeadElement
Entity HTMLParagraphElement
Notation HTMLInputElement
HTMLTableElement
<html>
<head><title>XHTML Test</title></head>
<body>
<h1>Heading Content</h1>
<p>First Paragraph Content</p>
<p id="p2">Second Paragraph Content</p>
</body>
</html>
HTMLDocument
HTMLHtmlElement Attr
<html> xmlns=”http://www.w3.org/1999/xhtml”
HTMLHeadElement HTMLBodyElement
<head> <body> Attr
id=”p2”
Element interface
DOMString getAttribute(DOMString name): Returns the
value for the attribute with the given name.
NodeList getElementsByTagName(DOMString name): Similar
to the method with the same name in the Document
interface, except only searches below the Element.
Node interface
DOMString nodeName
DOMString nodeValue: Returns the name of the node and the
value of the node. The name of the node is the tag name for
nodes representing HTML tags, such as “HEAD” for the
<head> tag. For other nodes, it’s a value representative of
the node as defined by the DOM (e.g., “#text” for Text
nodes). The value of the node is really only useful for nodes
that contain text, like the Text node, because for most other
nodes the value is null.
NodeList childNodes: Returns a NodeList containing all of
the nodes immediate children. The childNodes property
does not return grandchildren, i.e., a child of a child. Only
Element nodes have children.
Node parentNode
Node firstChild
Node lastChild
Node previousSibling
Node nextSibling: parentNode returns the parent node
(only Element nodes are capable of being parents, but all
nodes, except for Document, have a parent). firstChild
returns the first node in the NodeList returned by
childNodes, while lastChild returns the last node in
the list. When two nodes have the same parent they are
called siblings, which means both the nodes appear in the
NodeList returned by the parent’s childNodes property.
previousSibling returns the sibling node that comes
before it in the childNodes list, while nextSibling
returns the node that comes after it.
DOM Modifiers
Document interface
Element createElement(DOMString tagName): Creates an
Element of the given type. The given tagName is the name
of the element you wish to create. The new node must be
added to an document using the Node methods. Example:
var hrElement = document.createElement("hr");
Text createTextNode(DOMString data): Creates a new Text
node containing the given text. The new node must be added
to an document using the Node methods. Example:
var paragraphText =
document.createTextNode("Third Paragraph Content");
Node importNode(Node importedNode, boolean deep):
Imports a node from another document to this document
without modifying the other document.
importNode() example:
var otherParagraph =
window.parent.frames[1].getElementsByTagName("p")[0];
var newParagraph =
document.importNode(otherParagraph, true);
Element interface
void setAttribute(DOMString name, DOMString value):
Adds an attribute to the Element or, if the attribute already
exists, sets the attribute to the given value. Example:
document.getElementsByTagName("p")[0].setAttribute("id",
"p1");
void removeAttribute(DOMString name): Removes the
attribute with the given name. If the attribute doesn’t exist,
then the method has no effect.
Node interface
Node insertBefore(Node newChild, Node refChild):
Inserts the new child in the childNodes list before the
given existing child and returns the inserted node. Example:
var newParagraph = document.createElement("p");
var firstParagraph = document.getElementsByTagName("p")[0];
document.body.insertBefore(newParagraph, firstParagraph);
Property Description
appCodeName Returns the code name of the browser
appName Returns the name of the browser
appVersion Returns the version information of the browser
cookieEnabled Determines whether cookies are enabled in the
browser
language Returns the language of the browser
onLine Determines whether the browser is online
platform Returns for which platform the browser is
compiled
product Returns the engine name of the browser
userAgent Returns the user-agent header sent by the
browser to the server
Navigator Object Methods
Method Description