Document Object PDF
Document Object PDF
Object Model
(DOM)
Mendel Rosenblum
Browser JavaScript interface to
HTML document
● HTML document exposed as a
collection of JavaScript objects and
methods
window this
'use strict';)
DOM hierarchy
● Rooted at window.document (html tag)
● Follows HTML document structure
window.document.head window.document.body
firstChild, lastChild.
parentNode
previousSibing previousSibing
nodeName: #text nodeName: B nodeN
nodeType: 3 (Text) nodeType: 1 nodeT
nodeValue: "Sample " nodeV
nextSibing nextSibing
firstChild
lastChild parentNode
nodeName: #text
nodeType: 3 (Text)
nodeValue: "bold"
element.className = "active";
element.style.color =
document.querySelector() and
document.querySelectorAll()
window.location.href =
"newPage.html";
console.log("Reached
confirm("OK?"); //
Popup dialog
DOM Coordinates
<div class="div1"><div class="div2"><div class="div3">
X
Y
div1
offsetTop
div2
offsetLeft offsetTop offsetParent
offsetLeft div3
offsetHeight offsetParent
offsetWidth
Positioning elements
● Normally elements are positioned automatically by
the browser as part of the document
Positioning context
● Each element has an offsetParent (some
ancestor element).
Parent
top/offsetTop Parent
left/offsetLeft
Child margin
Child border
Element dimensions
● Reading dimensions: element.offsetWidth and
element.offsetHeight
<body>
<div id="div1">
<p>div1</p>
</div>
#div1 { width:
50px; height:
200px; background:
#ffe0e0;
}
Positioning
…
<div id="div2">
<p>div2</p>
<div id="div2-1">
<p>div2-1</p>
</div>
</div>
#div2 {width: 300px; height:
200px; position: relative;
background: #d0e0ff;}
Positioning
...
<div id="div3">
<p>div3</p>
<div id="div3-
1">
<p>div3-1</p>
</div>
</div>
#div3 {width: 300px;
height:
200px; background:
#ffffe0;}