3. How Browsers Render
• Each browser has a Rendering Engine
• Rendering Engine takes an HTML text
and produces a DOM tree
Friday, April 12, 13
4. Rendering Engines
Engine Browser Developer
Blink Google Chrome Google
Gecko Mozilla Firefox Mozilla
Trident IE Microsoft
Apple, KDE, Nokia,
Webkit Apple Safari
Others
Friday, April 12, 13
5. How Browsers Work
Parse HTML to make DOM Tree
Build Render Tree from CSS
Layout Elements
Paint
Friday, April 12, 13
6. What’s a DOM Tree
<html>
<body>
<p>
Hello World
</p>
<div>
<img src="example.png"/>
</div>
</body>
</html>
Friday, April 12, 13
8. Rendering DOM Tree
• A Render tree is derived from DOM
tree
• It’s not a 1-1 relation
Friday, April 12, 13
9. Browser Flow
• Read page as text
• Create DOM tree
• Create Render tree
• Paint
Friday, April 12, 13
10. Enter JavaScript
• Read page as text
• Create DOM tree JavaScript
Manipulates the
• Create Render tree
data
• Paint
Friday, April 12, 13
11. Enter JavaScript
• JavaScript alters page load
• JavaScript alters DOM Tree
• JavaScript creates interactivity through
events handling
Friday, April 12, 13
12. JavaScript and DOM
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body> A <script> element is
<script>
var x = 5; executed in place
var y = 3;
console.log('Hello From JS');
</script>
<p>This is just some text</p>
</body>
</html>
Friday, April 12, 13
13. Q&A
Browser Page Rendering
Friday, April 12, 13
14. Interactive Apps
• Browser is a programming platform
• A web application is interactive
Friday, April 12, 13
15. Interactivity
• Browser itself is interactive
• In addition: A web page is interactive
• Demo
Friday, April 12, 13
17. Event Loop
Wait for Events
Handle Events
Friday, April 12, 13
18. Event Loop
Wait for Events
Handle Events
Friday, April 12, 13
19. Event Loop
Wait for Events
Handle Events
Friday, April 12, 13
20. Event Handling
DOM Node
+ Event Handler (code)
Event
Friday, April 12, 13
21. Code Outline
• From HTML:
• <a on...=”handleEvent()”>
Friday, April 12, 13
22. Code Outline
• But this can get messy
<a href="#" onclick="doclick"
onblur="doblur"
onchange="dochange"
ondblclick="dodblclick"
onmousemove="domove"
onmouseover="doover">
Too many events</a>
Friday, April 12, 13
23. Code Outline
• From JS
• Get a DOM node
• Bind event to code
Friday, April 12, 13
24. Getting DOM Nodes
• getElementById(...)
• getElementsByTagName(...)
• querySelector(...) - IE8 and up
Friday, April 12, 13
25. Browser Events
• All browsers use:
node.onevent = ...
• IE uses:
node.attachEvent(...)
• Other browsers use
node.addEventListener(...)
Friday, April 12, 13
26. Demo: Events
• Write a simple page that shows alert as
a response to click event
• Modify to change text of element
Friday, April 12, 13
27. Using the Event Object
• Event object includes info on the event
• Print it to console for inspection
<button>Click Me</button>
<script>
var btn = document.getElementsByTagName('button')[0];
btn.onclick = function(e) {
if ( ! e ) e = window.event;
console.dir( e );
};
</script>
Friday, April 12, 13
31. Capturing vs. Bubbling
• node.addEventListener takes a third
parameter
• true means capturing
• false means bubbling
• defaults to false
Friday, April 12, 13
32. Demo
• Capture all click events using
document.onclick = ...
Friday, April 12, 13
35. Double Handlers
function doSomething(e) {
if ( ! e ) e = window.event;
Element2
// this refers to
Element1 // the current element
// for inner event:
// this = element2
// for outer event:
// this = element1
}
Friday, April 12, 13
40. Events Lab
• Implement 5 duplicated input boxes
• Each input box should have the same
text
• Change one -> all change automatically
Friday, April 12, 13
51. DOM Traversal
<body> body
<div>
<h1>Header</h1>
<p>
<img src="..." /> #text #div #text
Paragraph text
<a
href="#">google</a> #text #text #h1 #text #p
</p>
</div>
</body>
Friday, April 12, 13
52. DOM Traversal
• n.childNodes[]
• n.firstChild
• n.lastChild
• n.nextSibling
• n.parentNode
• n.previousSibling
Friday, April 12, 13
53. The (Past) Future
• document.querySelector takes any CSS
selector and fetches DOM element
• Supported in IE8 and later
Friday, April 12, 13
54. DOM API
• Allows
• Getting info on elements
• Changing element attributes
• Creating new elements
• Setting elements style
Friday, April 12, 13
55. DOM API
• Use x.nodeName to get the tag name
if ( this.nodeName === 'INPUT' ){
// handle input element
}
Friday, April 12, 13
56. DOM API
• Use x.nodeValue to get/set the node
value
a.firstChild.nodeValue = 'Go to google';
Friday, April 12, 13
57. DOM API
• Use getAttribute / setAttribute to
change element attributes
a.setAttribute('href',
'http://www.google.com');
Friday, April 12, 13
58. Creating New Elements
• Create elements and text nodes using
document
• Later you can add them to the DOM
document.createElement('P');
document.createTextNode('Hello
World');
Friday, April 12, 13
59. Creating New Elements
• Insert new nodes by manipulating
existing
// append y to x
x.appendChild(y)
// insert y to x before z
x.insertBefore(y,z)
// remove y from x
x.removeChild(y)
// replace y with z
x.replaceChild(y,z)
Friday, April 12, 13
60. Change Style
• Use .style property to set an element
style
• Note style keys are almost like CSS
property names
p.style.backgroundColor = 'blue';
Friday, April 12, 13
62. DOM Lab
• Given the HTML at:
http://jsbin.com/ecitag/1/edit
• Use JavaScript to:
• write your name in the first <li> item
of the second list
• Change the H3 to H4
• Set all links to point to google.com
Friday, April 12, 13
63. DOM Lab
• Create a Money Converter calculator
• Support 3 currencies
Friday, April 12, 13