Web Programming Step by Step, 2nd Edition

Lecture XX: More JavaScript

Reading: none

Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. 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.

Valid HTML5 Valid CSS

JavaScript in HTML body (example)

<script type="text/javascript">
	JavaScript code
</script>

Injecting Dynamic Text: document.write

document.write("message");

The typeof function

typeof(value)

The arguments array

function example() {
	for (var i = 0; i < arguments.length; i++) {
		alert(arguments[i]);
	}
}
example("how", "are", "you");   // alerts 3 times

The "for each" loop

for (var name in arrayOrObject) {
	do something with arrayOrObject[name];
}

Arrays as maps

var map = [];
map[42] = "the answer";
map[3.14] = "pi";
map["champ"] = "suns";

Date object

var today = new Date();               // today

var midterm = new Date(2007, 4, 4);   // May 4, 2007

The eval (evil?) function

eval("JavaScript code");
eval("var x = 7; x++; alert(x / 2);");  // alerts 4

Dr. Evil