Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.
Every Javascript program can refer to the following global objects:
name | description |
---|---|
document
|
current HTML page and its content |
history
|
list of pages the user has visited |
location
|
URL of the current HTML page |
navigator
|
info about the web browser you are using |
screen
|
info about the screen area occupied by the browser |
window
|
the browser window |
window
objectthe entire browser window; the top-level object in DOM hierarchy
window
objectalert
,
confirm
,
prompt
(popup boxes)
setInterval
,
setTimeout
clearInterval
,
clearTimeout
(timers)
open
,
close
(popping up new browser windows)
blur
,
focus
,
moveBy
,
moveTo
,
print
,
resizeBy
,
resizeTo
,
scrollBy
,
scrollTo
document
objectthe current web page and the elements inside it
location
objectthe URL of the current web page
navigator
objectinformation about the web browser application
navigator
object to see what browser is being used, and write browser-specific scripts and hacks:
if (navigator.appName === "Microsoft Internet Explorer") { ...
history
objectthe list of sites the browser has visited in this window
history
properties, for security<button id="ok" onclick="okayClick();">OK</button>
// called when OK button is clicked
function okayClick() {
alert("booyah");
}
// where element is a DOM element object
element.event = function;
var okButton = document.getElementById("ok"); okButton.onclick = okayClick;
<head> <script src="myfile.js" type="text/javascript"></script> </head> <body> <div><button id="ok">OK</button></div>
// global code var okButton = document.getElementById("ok"); okButton.onclick = okayClick; // error: okButton is undefined
head
is processed before page's body
has loaded
window.onload
event
(8.1.1)
window.onload = functionName; // global code
// this will run once the page has finished loading
function functionName() {
element.event = functionName;
element.event = functionName;
...
}
window.onload
event that occurs at that momentwindow.onload
handler we attach all the other handlers to run when events occur
<!-- look Ma, no JavaScript! -->
<button id="ok">OK</button>
window.onload = pageLoad; // global code // called when page loads; sets up event handlers function pageLoad() { var okButton = document.getElementById("ok"); okButton.onclick = okayClick; } function okayClick() { alert("booyah"); }
()
when attaching the handler
window.onload = pageLoad();window.onload = pageLoad;okButton.onclick = okayClick();okButton.onclick = okayClick;
window.onLoad = pageLoad;window.onload = pageLoad;
function(parameters) { statements; }
window.onload = function() { var okButton = document.getElementById("ok"); okButton.onclick = okayClick; }; function okayClick() { alert("booyah"); }
or the following is also legal (though harder to read and bad style):
window.onload = function() { var okButton = document.getElementById("ok"); okButton.onclick = function() { alert("booyah"); }; };
this
(8.1.3)
window.onload = pageLoad; function pageLoad() { var okButton = document.getElementById("ok"); okButton.onclick = okayClick; // bound to okButton here } function okayClick() { // okayClick knows what DOM object this.innerHTML = "booyah"; // it was called on }
this
this
<fieldset> <label><input id="Huey" type="radio" name="ducks" /> Huey</label> <label><input id="Dewey" type="radio" name="ducks" /> Dewey</label> <label><input id="Louie" type="radio" name="ducks" /> Louie</label> </fieldset>
function processDucks() {if (document.getElementById("huey").checked) { alert("Huey is checked!"); } else if (document.getElementById("dewey").checked) { alert("Dewey is checked!"); } else { alert("Louie is checked!"); }alert(this.id + " is checked!"); }
var paragraph = document.getElementById("welcome");
paragraph.innerHTML = "Welcome to our site!"; // change text on page
DOM element objects have the following properties:
innerHTML
: text and/or HTML tags inside a nodetextContent
: text (no HTML tags) inside a node
innerHTML
, but not supported in IE6
value
: the value inside a form controlinnerHTML
// bad style!
var paragraph = document.getElementById("welcome");
paragraph.innerHTML = "<p>text and <a href="page.html">link</a>";
innerHTML
can inject arbitrary HTML content into the pageinnerHTML
to inject HTML tags; inject plain text only
<button id="clickme">Color Me</button>
window.onload = function() { document.getElementById("clickme").onclick = changeColor; }; function changeColor() { var clickMe = document.getElementById("clickme"); clickMe.style.color = "red"; }
style
property lets you set any CSS style for an elementcamelCasedNames
backgroundColor
, borderLeftWidth
, fontFamily
.style
when setting styles
var clickMe = document.getElementById("clickme");clickMe.color = "red";clickMe.style.color = "red";
likeThis
, not like-this
clickMe.style.font-size = "14pt";clickMe.style.fontSize = "14pt";
clickMe.style.width = 200;clickMe.style.width = "200px"; clickMe.style.padding = "0.5em";
function okayClick() {this.style.color = "red";this.className = "highlighted"; }
.highlighted { color: red; }
How would we do each of the following in JavaScript code? Each involves modifying each one of a group of elements ...
div
s of class puzzle
to random x/y locations.ul
list with id
of TAs
to have a gray background.<p> This is a paragraph of text with a <a href="/path/to/another/page.html">link</a>. </p>
every node's DOM object has the following properties:
name(s) | description |
---|---|
firstChild , lastChild
|
start/end of this node's list of children |
childNodes
|
array of all this node's children |
nextSibling , previousSibling
|
neighboring nodes with the same parent |
parentNode
|
the element that contains this node |
<p id="foo">This is a paragraph of text with a <a href="/path/to/another/page.html">link</a>.</p>
<div> <p> This is a paragraph of text with a <a href="page.html">link</a>. </p> </div>
div
above have?"\n\t"
(before/after the paragraph)a
tag?
document
and other DOM objects for accessing descendents:
name | description |
---|---|
getElementsByTagName
|
returns array of descendents that have the given HTML tag, such as "div"
|
getElementsByName
|
returns array of descendents that have the given name attribute (mostly useful for accessing form controls)
|
highlight all paragraphs in the document:
var allParas = document.getElementsByTagName("p"); for (var i = 0; i < allParas.length; i++) { allParas[i].style.backgroundColor = "yellow"; }
<body> <p>This is the first paragraph</p> <p>This is the second paragraph</p> <p>You get the idea...</p> </body>
getElementById
highlight all paragraphs inside of the section with ID "address"
:
var addr = document.getElementById("address"); var addrParas = addr.getElementsByTagName("p"); for (var i = 0; i < addrParas.length; i++) { addrParas[i].style.backgroundColor = "yellow"; }
<p>This won't be returned!</p>
<div id="address">
<p>1234 Street</p>
<p>Atlanta, GA</p>
</div>
// create a new <h2> node
var newHeading = document.createElement("h2");
newHeading.innerHTML = "This is a heading";
newHeading.style.color = "green";
document.createElement("tag")
: creates and returns a new empty DOM node representing an element of that type
document.createTextNode("text")
: creates and returns a new text node containing the given text
Every DOM element object has these methods:
name | description |
---|---|
appendChild(node)
|
places given node at end of this node's child list |
insertBefore(new, old)
|
places the given new node in this node's child list just before old child
|
removeChild(node)
|
removes given node from this node's child list |
replaceChild(new, old)
|
replaces given child with new node |
window.onload = function() { var thisSlide = document.getElementById("slide38"); thisSlide.onclick = slideClick; } function slideClick() { var p = document.createElement("p"); p.innerHTML = "A paragraph!"; this.appendChild(p); }
style
property to see all styles