Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
JavaScript is a powerful language, but it has many flaws:
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js" type="text/javascript"></script>
$ function
					(9.1.3)
				
$("id")
				id
					document.getElementById("id")
					
$("footer").innerHTML = $("username").value.toUpperCase();
					<button onclick="okayClick();">OK</button>
// called when OK button is clicked
function okayClick() {
	alert("booyah");
}
					
				
// where element is a DOM element object
element.event = function;
				<button id="ok">OK</button>
$("ok").onclick = okayClick;
					
				<head> <script src="myfile.js" type="text/javascript"></script> </head> <body> ... </body>
// global code
var x = 3;
function f(n) { return n + 1; }
function g(n) { return n - 1; }
x = f(x);
				script tag
						body
						<head> <script src="myfile.js" type="text/javascript"></script> </head> <body> <div><button id="ok">OK</button></div>
// global code $("ok").onclick = okayClick; // error: $("ok") is null
head is processed before page's body has loaded
						window.onload event
					(8.1.1)
				// this will run once the page has finished loading function functionName() { element.event = functionName; element.event = functionName; ... } window.onload = functionName; // global code
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>
					// called when page loads; sets up event handlers function pageLoad() { $("ok").onclick = okayClick; } function okayClick() { alert("booyah"); } window.onload = pageLoad; // global code
() 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");
}
					
window.onload = function() {
	var okButton = document.getElementById("ok");
	okButton.onclick = function() {
		alert("booyah");
	};
};
			
function okayClick() {
	this.style.color = "red";
	this.className = "highlighted";
}
.highlighted { color: red; }
				
				
				| 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);
	$("output").innerHTML = "Wait for it...";
}
function booyah() {   // called when the timer goes off
	$("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 $("output").innerHTML += " Rudy!"; }
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);
() ?