Today's agenda:
tree shaped structure built out of all of the HTML elements in a page, accessible via JavaScript
<html>
<head>
<title> ... </title>
</head>
<body>
<h1> ... </h1>
<div>
<p> ... </p>
</div>
</body>
</html>
HTML
a set of JavaScript objects that represent each element on the page
div
document.getElementyById(...)
document.querySelector(...)
document.querySelectorAll(...)
document.createElement(...)
HTML
var pTag = document.getElementById("october");
JavaScript
For starters, the HTML attributes. This HTML:
<img src="images/puppy.png" alt="A fantastic puppy photo"/>
HTML
Has a DOM object (let's call it puppyImg
) with these two properties:
puppyImg.src
-- set by the browser to images/puppy.png
puppyImg.alt
-- set by the browser to "A fantastic puppy photo"
<p>See our <a href="sale.html" id="saleslink">Sales</a> today!</p>
<img id="icon" src="images/borat.jpg" alt="Borat" />
<caption class="photo user-upload">Beauty.</caption>
HTML
var icon = document.getElementById("icon");
var theLink = document.getElementById("saleslink");
var caption = document.querySelector("caption");
JavaScript
Property | Description | Example |
---|---|---|
tagName
|
element's HTML tag |
icon.tagName is "IMG"
|
className
|
CSS classes of element |
caption.className is "photo user-upload"
|
src
|
URL target of an image |
icon.src is "images/borat.jpg"
|
href | URL target of a link |
theLink.href is "sale.html"
|
innerHTML
property
All DOM elements have a property called innerHTML
that has the contents of the HTML tag as a string:
<ul id="dr-seuss">
<li>Thing 1</li>
<li>Thing 2</li>
</ul>
HTML
var elm = document.getElementById("dr-seuss");
// elm.innerHTML : "\n Thing 1 \n Thing 2 \n"
JavaScript
Set their properties, and the page changes in response
This is how give behavior to web pages: use JavaScript to manipulate the DOM by changing the properties of DOM elements
<a id="fb-link" href="http://www.facebook.com">Facebook</a>
HTML
Before the JavaScript runs, we'd see:
And after we run this Javascript:
var link = document.getElementById("fb-link");
link.innerHTML = "MySpace is back in a really big way."
JavaScript
We'd see:
<button onclick="okayClick();">OK</button>
HTML
// called when OK button is clicked
function okayClick() {
alert("booyah");
}
JavaScript
objectName.onevent = function;
<button id="ok">OK</button>
HTML
var okButton = document.getElementById("ok");
okButton.onclick = okayClick;
JavaScript
<html>
<head>
<script src="myfile.js" type="text/javascript"></script>
</head>
<body> ... </body>
</html>
HTML
var x = 3;
function f(n) { return n + 1; }
function g(n) { return n - 1; }
x = f(x);
myfile.js
-- JavaScript
script
tag
body
<html>
<head>
<script src="myfile.js" type="text/javascript"></script>
</head>
<body>
<div><button id="ok">OK</button></div>
(... more html ...)
HTML
var btn = document.getElementById("ok");
btn.onclick = okayClick; // this is bad: btn is null at this point
myfile.js
-- JavaScript
head
is processed before page's body
has loaded
window.onload
event
function functionName() {
// put code to initialize the page here
}
// instruct window to run the function when the page has loaded:
window.onload = functionName;
JavaScript
window.onload
event that happens once everything in the page has been loadedwindow.onload
, it will run at that time<button id="ok">OK</button> <!-- (1) -->
HTML
// called when page loads; sets up event handlers
function pageLoad() {
var ok = document.getElementById("ok"); // (3)
ok.onclick = okayClick;
}
function okayClick() {
alert("booyah"); // (4)
}
window.onload = pageLoad; // (2)
JavaScript
window.onLoad= pageLoad; window.onload = pageLoad;
()
when attaching the handler ok.onclick = okayClick(); ok.onclick = okayClick;
alert
; must enclose in your own function
ok.onclick =alert("booyah");ok.onclick = okayClick; function okayClick() { alert("booyah"); }
function(parameters) { ... statements ...; }
window.onload = function() {
var ok = document.getElementById("ok");
ok.onclick = okayClick;
};
function okayClick() {
alert("booyah");
}
JavaScript
window.onload = function() {
document.getElementById("ok").onclick = function() {
alert("booyah");
};
};
function okayClick() {
this.style.color = "red"; // <-- bad style
this.className = "highlighted"; // <-- better style
}
HTML
.highlighted { color: red; }
CSS
var count = 0;
function incr(n) {
count += n;
}
function reset() {
count = 0;
}
incr(4);
incr(2);
console.log(count);
count
, incr
, and reset
function everything() {
var count = 0;
function incr(n) {
count += n;
}
function reset() {
count = 0;
}
incr(4);
incr(2);
console.log(count);
}
everything(); // call the function to run the code
JavaScript
everything
(can we get it down to 0?)(function() {
statements;
})();
(function() {
var count = 0;
function incr(n) {
count += n;
}
function reset() {
count = 0;
}
incr(4);
incr(2);
console.log(count);
})();
"use strict";
...your code...
JavaScript
"use strict";
at the very top of your JS file turns on strict syntax checking:
HTML
(function() {
window.onload = function() {
// phew! your code goes here
};
})();
JavaScript