Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, Victoria Kirst and Roy McElmurry IV. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
"use strict"; your code...
(function() { "use strict"; your code... })();
"use strict";
at the very top of your JS file turns on strict syntax checking:
method | description |
---|---|
setTimeout(function, delayMS);
|
arranges to call given function after given delay in ms |
setInterval(function, delayMS);
|
arranges to call function repeatedly every delayMS ms |
clearTimeout(timerID); clearInterval(timerID);
|
stops the given timer so it will not call its function |
setTimeout
and setInterval
return an ID representing the timer
clearTimeout
/Interval
later to stop the timer
setTimeout
example
<button onclick="delayMsg();">Click me!</button> <span id="output"></span>
function delayMsg() {
setTimeout(booyah, 5000);
document.getElementById("output").innerHTML = "Wait for it...";
}
function booyah() { // called when the timer goes off
document.getElementById("output").innerHTML = "BOOYAH!";
}
setInterval
example
var timer = null; // stores ID of interval timer function delayMsg2() { if (timer == null) { timer = setInterval(rudy, 1000); } else { clearInterval(timer); timer = null; } } function rudy() { // called each time the timer goes off document.getElementById("output").innerHTML += " Rudy!"; }
We want to add javascript to this alarm page.
function delayedMultiply() {
// 6 and 7 are passed to multiply when timer goes off
setTimeout(multiply, 2000, 6, 7);
}
function multiply(a, b) {
alert(a * b);
}
setTimeout(multiply(6 * 7), 2000);
()
when passing the function
setTimeout(booyah(), 2000);setTimeout(booyah, 2000);setTimeout(multiply(num1 * num2), 2000);setTimeout(multiply, 2000, num1, num2);
()
?objectName.attributeName
<div id="main" class="foo bar"> <p>Hello, <em>very</em> happy to see you!</p> <img id="icon" src="images/borat.jpg" alt="Borat" /> </div>
var mainDiv = document.getElementById("main"); var icon = document.getElementById("icon");
Property | Description | Example |
---|---|---|
tagName
|
element's HTML tag |
mainDiv.tagName is "DIV"
|
className
|
CSS classes of element |
mainDiv.className is "foo bar"
|
innerHTML
|
content in element |
mainDiv.innerHTML is "\n <p>Hello, <em>ve...
|
src
|
URL target of an image |
icon.src is "images/borat.jpg"
|
<input id="sid" type="text" size="7" maxlength="7" /> <input id="frosh" type="checkbox" checked="checked" /> Freshman?
var sid = document.getElementById("sid"); var frosh = document.getElementById("frosh");
Property | Description | Example |
---|---|---|
value
|
the text/value chosen by the user |
sid.value could be "1234567"
|
checked
|
whether a box is checked |
frosh.checked is true
|
disabled
|
whether a control is disabled (boolean) |
frosh.disabled is false
|
readOnly
|
whether a text box is read-only |
sid.readOnly is false
|
<select id="captain"> <option value="kirk">James T. Kirk</option> <option value="picard">Jean-Luc Picard</option> <option value="cisco">Benjamin Cisco</option> </select> <label> <input id="trekkie" type="checkbox" /> I'm a Trekkie </label>
select
, you usually want its value
checked
(true/false)innerHTML
// 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"; }
Property | Description |
---|---|
style
|
lets you set any CSS style property for an element |
camelCasedNames
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; }
function highlightField() {
// turn text yellow and make it bigger
if (!document.getElementById("text").className) {
document.getElementById("text").className = "highlight";
} else if (document.getElementById("text").className.indexOf("invalid") < 0) {
document.getElementById("text").className += " highlight";
}
}
className
property corresponds to HTML class
attribute<button id="clickme">Click Me</button>
window.onload = function() { document.getElementById("clickme").onclick = biggerFont; }; function biggerFont() { var size = parseInt(document.getElementById("clickme").style.fontSize); size += 4; document.getElementById("clickMe").style.fontSize = size + "pt"; }
style
property lets you set any CSS style for an elementdocument.getElementById("main").style.top = document.getElementById("main").style.top + 100 + "px";// bad!
"200px" + 100 + "px"
, "200px100px"
document.getElementById("main").style.top =
parseInt(document.getElementById("main").style.top) + 100 + "px"; // correct
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
window.open
window.open("http://foo.com/bar.html", "My Foo Window", "width=900,height=600,scrollbars=1");
window.open
pops up a new browser windowdocument
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") { ...