Académique Documents
Professionnel Documents
Culture Documents
lifecycle,
HTML & DOM
PZI 2016/2017
Server
Internet browser
Enter URL
or click on a link
Event
handling phase
HTML
HyperText Markup Language (HTML)
A markup language combines (XML,
XAML, LaTeX)
First-version Tim Berners Lee, 1993 CERN
Standard by W3C - current version 5.1
(2016)
https://www.w3.org/TR/2016/REC-html51< !D O CTYPE htm l>
20161101/
< htm l>
< head> < /head>
< body> < /body>
< /htm l>
HTML
Start tag
End tag
Self-closing element
Attribute
< !D O C TY P Eh tm l>
HTML 4 strict:
Page-building phase
User
1
Enter URL
or click on a link
Internet browser
Server
Execute
JavaScript
code
<!DOCTYPE html>
<html>
<head>
<title>Web app lifecycle</title>
<style> #first { color: green; } #second { color: red; } </style>
</head>
<body>
<ul id="first"></ul>
<script>
Creates a new
function addMessage(element, message){
element,
const messageElement = document.createElement("li");
messageElement.textContent = message;
adds a text message
element.appendChild(messageElement);
to it,
}
and appends it to
const first = document.getElementById("first");
Adds a message
the passed in
addMessage(first, "Page loading");
that a page
is
element.
</script>
loading
<ul id="second"></ul>
<script>
When a mouse
document.body.addEventListener("mousemove", function() {
moves,
addMessage(document.getElementById("second"), "Event: mousemove");
});
add a mouse
move message
document.body.addEventListener("click", function(){
When the page is
addMessage(document.getElementById("second"), "Event: click");
});
clicked,
</script>
add the click
</body>
message
</html>
1
<!DOCTYPE html>
1 <html>
html
2 <head>
4
3 <title>Web app lifecycle</title> 2
7
5 <style>
head
body
#first
{
color:
green;
}
6
5 8
9
#second { color: red; } 3
</style>
style
title
ul script
</head>
7 <body>
Web app
#first
function
8 <ul id="first"></ul>
4 lifecylce
6 {}
addMessage
9 <script>
#second{
}
function addMessage(element, message){
const messageElement =
document.createElement("li");
messageElement.textContent = message;
element.appendChild(messageElement);
}
html
html
head
p
body
head
body
p
DOM relationships
A
B
E
C
txt
A - root node/element
B, C, D - children of A
B firstChild of A, D lastChild of A
A - parentNode of B, C, D
B, C, D are siblings
C nextSibling of B, C previousSibling of D
B, C, D, E, txt - descendants of A
A - ancestor of B, C, D, E, txt
E - children of B; E, txt - childNodes of B
A, B, C, D, E - HTML elements
A, B, C, D, E, txt - HTML nodes
children (only elements); childNodes (elements and text)
A
B
E
C
txt2
D
F
Global objects
window - global object through which all other global objects,
global variables (even user-defined oness), and browser API-s
are accessible.
https://developer.mozilla.org/en-US/docs/Web/API/Window
<script>
window.parseInt(); //global function
parseInt();
(https://developer.mozilla.org/en-US/docs/Web/API/document)
Globals: location
Interact with the information about the
current URL of the page (reload, go to
new URL from JavaScript, access hash
data, protocol, search query)
https://developer.mozilla.org/enUS/docs/Web/API/Window/location
Globals: console
Object for logging information to the
browser's console
(https://developer.mozilla.org/enUS/docs/Web/API/Window/console)
console.log('M essage'); //Logs 'M essage'to the console
console.assert(truthy | falsy, 'M essage'); //Breaks if falsy
console.tim e('Som e-string-ID '); //Starts a tim er
console.tim eEnd('Som e-string-ID '); //Stops the tim er
//Prints out the result
add a new
child to an element
elem ent.rem oveChild(child) to
remove a child of an
element
elem ent.textContent = "N ew Text" to
of an element;
elem ent.innerH TM L = "< p> Som e < b> H TM L< /b> < /p> "
HTML attributes
HTML attributes are additional values that
configure HTML elements or adjust their behavior.
< input type= "passw ord" < !-- Adjusts the behavior/look -->
id= "m yId" < !-- Allow s us to reference the elem ent -->
class= "m yClass" < !-- sets the class -->
data-custom -attribute= "m yAttribute"/> < !-- Custom -->
<!DOCTYPE html>
<html>
<head>
<title>Web app lifecycle</title>
<style> #first { color: green; } #second { color: red; }
</style>
</head>
<body>
The DOM tree can be modified to any degre
<ul id="first"></ul>
create new nodes, delete or modify existing
<script>
function addMessage(element, message){
const messageElement = document.createElement("li");
html
messageElement.textContent = message;
element.appendChild(messageElement);
}
head
Web app
style
#first
{}
#second{
}
li
Page
loading
body
ul
script
function
addMessage
<!DOCTYPE html>
<html>
<head>
<title>Web app lifecycle</title>
<style> #first { color: green; } #second { color: red; } </style>
</head>
<body>
<ul id="first"></ul>
<script>
function addMessage(element, message){
const messageElement = document.createElement("li");
messageElement.textContent = message;
element.appendChild(messageElement);
}
const first = document.getElementById("first");
addMessage(first, "Page loading");
</script>
<ul id="second"></ul>
<script>
document.body.addEventListener("mousemove", function() {
addMessage(document.getElementById("second"), "Event: mousemove");
});
document.body.addEventListener("click", function(){
addMessage(document.getElementById("second"), "Event: click");
});
</script>
Execute JavaScript code
</body>
</html>
Careful!
Modifying the page is expensive (for a
large number of changes)
Changing a single node can have an
influence on how a lot of other nodes are
rendered.
(
https://www.youtube.com/watch?v=ZTnI
xIA5KGw
)
Also, beware of premature optimizations!
Use console.tim e to measure!
Example
Which is faster?
A: function alignBookm arksFirst(){
const readableElem ents = docum ent.querySelectorAll(".readable-text");
const bookm arks = docum ent.querySelectorAll(".fa-bookm ark-o");
for(let i= 0; i< readableElem ents.length; i+ + )
bookm arks[i].style.top = $(readableElem ents[i]).position().top + "px";
}
Layout trashing
The browser tries to be lazy and batch
updates
Occurs when we don't allow the browser
to batch
updates:
const
h1 = element1.clientHeight;
//Read
element1.style.height = (h1 * 2) + 'px'; // Write (invalidates
layout)
const h2 = element2.clientHeight; // Read (triggers layout)
element2.style.height = (h2 * 2) + 'px'; // Write (invalidates
layout)
const h3 = element3.clientHeight; // Read (triggers layout)
element3.style.height = (h3 * 2) + 'px'; // Write (invalidates
Layout invalidation
properties
Interface
Property name
Element
MouseEvent
Window
Frame,
Document,
Image
React (http://facebook.github.io/react/)
Set of virtual objects that mimic the DOM
React code deals with the virtual DOM (plain