Commit e8aea637 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>for loop</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function doFor() {
var strValue = "";
for (var i = 1; i <= 10; i++) {
strValue+=i;
}
alert(strValue);
strValue = "";
for (var i = 10; i > 0; i--) {
strValue+=i;
}
alert(strValue);
}
//]]>
</script>
</head>
<body onload="doFor()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>for...in loop</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function doFor() {
var MyText = {
one : "one",
two : "two",
three : "three"
};
for (var prop in MyText) {
document.writeln(prop + "<br />");
}
}
//]]>
</script>
</head>
<body onload="doFor()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Boolean valueOf</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function newBool() {
var boolFlag = new Boolean("false");
alert(boolFlag.valueOf());
}
//]]>
</script>
</head>
<body onload="newBool()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Number methods</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function numbers() {
// Number methods
var newNumber = new Number(34.8896);
document.writeln(newNumber.toExponential(3) + "<br />");
document.writeln(newNumber.toPrecision(3) + "<br />");
document.writeln(newNumber.toFixed(6) + "<br />");
var newValue = newNumber.valueOf();
document.writeln(newValue.toString(2) + "<br />");
document.writeln(newValue.toString(8) + "<br />");
document.writeln(newValue.toString(10) + "<br />");
document.writeln(newValue.toString(16) + "<br />");
}
//]]>
</script>
</head>
<body onload="numbers()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Boolean valueOf</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function newBool() {
var boolFlag = new Boolean("false");
alert(boolFlag.valueOf());
}
//]]>
</script>
</head>
<body onload="newBool()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>String HTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function stringHTML() {
var someString = new String("This is the test string");
document.writeln(someString.big() + "<br />");
document.writeln(someString.blink() + "<br />");
document.writeln(someString.sup() + "<br />");
document.writeln(someString.strike() + "<br />");
document.writeln(someString.bold() + "<br />");
document.writeln(someString.italics() + "<br />");
document.writeln(someString.small() + "<br />");
document.writeln(someString.link('http://www.oreilly.com'));
}
//]]>
</script>
</head>
<body onload="stringHTML()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Gotta Split</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function splitString() {
var inputString = new String('firstName=Shelley,lastName=Powers,state=Missouri,statement="This is a test, of split"');
var arrayTokens = inputString.split(',',3);
// process split on commas
for (var i = 0; i < arrayTokens.length; i++) {
document.writeln(arrayTokens[i] + "<br />");
// now split on equals and write just value
var newTokens = arrayTokens[i].split('=');
document.writeln(newTokens[1] + "<br /><br />");
}
}
//]]>
</script>
</head>
<body onload="splitString()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>RegExp string matching</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
window.onload=function() {
var re = /(ds)+(j+s)/ig;
var str = "cfdsJS *(&dsjjjsYJSjs 888dsdsJS";
var resultArray = re.exec(str);
while (resultArray) {
document.writeln(resultArray[0]);
document.writeln(" next match starts at " + re.lastIndex + "<br />");
for (var i = 1; i < resultArray.length; i++) {
document.writeln("substring of " + resultArray[i] + "<br />");
}
document.writeln("<br />");
resultArray = re.exec(str);
}
}
//]]>
</script>
</head>
<body>
<p></p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>RegExp</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function matchString() {
var regExp = /\s\*/g;
var str = "This *is *a *test *string";
var resultString = str.replace(regExp,'-');
alert(resultString);
}
//]]>
</script>
</head>
<body onload="matchString()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Find Date</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function findDate() {
var regExp = /:\D*\s\d+\s\d+/;
var str = "This is a date: March 12 2009";
var resultString = str.match(regExp);
alert("Date" + resultString);
}
//]]>
</script>
</head>
<body onload="findDate()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>swap</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function swapWords() {
var rgExp = /(\w*)-*(\w*)/
var str = "Java---Script";
var resultStrng = str.replace(rgExp,"$2-$1");
alert(resultStrng);
}
//]]>
</script>
</head>
<body onload="swapWords()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Date</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function testingDate() {
// new date
var dtNow = new Date();
// set day, month, year
dtNow.setDate(18);
dtNow.setMonth(10);
dtNow.setYear(1954);
dtNow.setHours(7);
dtNow.setMinutes(2);
// output formatted
document.writeln(dtNow.toString() + "<br />");
document.writeln(dtNow.toLocaleString() + "<br />");
document.writeln(dtNow.toLocaleDateString() + "<br />");
document.writeln(dtNow.toLocaleTimeString() + "<br />");
document.writeln(dtNow.toGMTString() + "<br />");
document.writeln(dtNow.toUTCString());
}
//]]>
</script>
</head>
<body onload="testingDate()">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Random Quote</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function getQuote() {
var quoteArray = new Array(5);
quoteArray[0] = "Quote one";
quoteArray[1] = "Quote two";
quoteArray[2] = "Quote three";
quoteArray[3] = "Quote four";
quoteArray[4] = "Quote five";
iValue = Math.random(); // random number between 0 and 1
iValue *= 5; // multiply by 5 to move the decimal
iValue = Math.floor(iValue); // round to nearest integer
alert(quoteArray[iValue]);
}
//]]>
</script>
</head>
<body onload="getQuote();">
<p>some content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>FIFO</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function pushPop() {
// create FIFO queue and add items using push
var fifoArray = new Array();
fifoArray.push("Apple");
fifoArray.push("Banana");
var ln = fifoArray.push("Cherry");
// print out length and array
document.writeln("length is " + ln + " and array is " + fifoArray + "<br />");
// use shift to shift items off the array
for (var i = 0; i < ln; i++) {
document.writeln(fifoArray.shift() + "<br />");
}
// print out length
document.writeln("length now is " + fifoArray.length + "<br /><br />");
// now, same with unshift
var fifoNewArray = new Array();
fifoNewArray.unshift("Learning");
fifoNewArray.unshift("Java");
ln = fifoNewArray.unshift("Script");
document.writeln("length is " + ln + " and array is " + fifoNewArray + "<br />");
// unshift
for (var i = 0; i < ln; i++) {
document.writeln(fifoNewArray.pop() + "<br />");
}
document.writeln("new length is " + fifoNewArray.length );
}
//]]>
</script>
</head>
<body onload="pushPop();">
<p>some content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Pass Me</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function alterArgs(strLiteral, aryObject) {
// overwrite original string
strLiteral = "Override";
aryObject[1] = "2";
aryObject[aryObject.length] = "three";
}
function testParams() {
var str = "Original Literal";
var ary = new Array("one","two");
document.writeln("string literal is " + str + "<br />");
document.writeln("Array object is " + ary + "<br /><br />");
alterArgs(str,ary);
document.writeln("string literal is " + str + "<br />");
document.writeln("Array object is " + ary + "<br /><br />");
}
//]]>
</script>
</head>
<body onload="testParams();">
<p>Some page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Build a Function</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function buildFunction() {
// prompt for function and args
var func = prompt("Enter function body:","");
var x = prompt("Enter value of x:","");
var y = prompt("Enter value of y:","");
// invoke anonymous function
var op = new Function("x", "y", func);
var theAnswer = op(x, y);
// print out results
alert("Function is: " + func );
alert( "x is: " + x +
" y is: " + y);
alert("The answer is: " + theAnswer);
}
//]]>
</script>
</head>
<body onload="buildFunction();">
<p>some content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Build a Function</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function buildFunction() {
// prompt for function and args
var func = prompt("Enter function body:");
var x = 3;
var y = 4;
// invoke anonymous function
var op = new Function("x", "y", "x + y");
var theAnswer = op(x, y);
// print out results
document.writeln("The answer is: " + theAnswer);
}
//]]>
</script>
</head>
<body onload="buildFunction();">
<p>some content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Function Literal</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
// invoking third argument as function
function funcObject(x,y,z) {
alert(z(x,y));
}
function testFunction() {
// third parameter is function
funcObject(3,4,function(x,y) { return x * y})
}
//]]>
</script>
</head>
<body onload="testFunction();">
<p>some content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Recursion</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function runRecursion() {
var addNumbers = function sumNumbers(numArray,indexVal,resultArray) {
// recursion test
if (indexVal == numArray.length)
return resultArray;
// perform numeric addition
resultArray[0] += Number(numArray[indexVal]);
// perform string concatenation
if (resultArray[1].length > 0) {
resultArray[1] += " and ";
}
resultArray[1] += numArray[indexVal].toString();
// increment index
indexVal++;
// call function again, return results
return sumNumbers(numArray,indexVal,resultArray);
}
// create numeric array, and the result array
var numArray = ['1','35.4','-14','44','0.5'];
var resultArray = new Array(0,''); // necessary for the initial case
// call function
var result = addNumbers(numArray,0, resultArray);
// output
document.writeln(result[0] + "<br />");
document.writeln(result[1]);
}
//]]>
</script>
</head>
<body onload="runRecursion();">
<p>some content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Nested</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
// outer function
function outerFunc(base) {
var punc = "!";
// return inner function
return function (ext) {
return base + ext + punc;
}
}