HTML and CSS Reference
In-Depth Information
EXAMPLE 14.29
<html>
<head><title>The onload() method</title>
<script type="text/javascript">
1
window.onload=setBorder;
2
function setBorder(){
3
document.getElementById("bdy").style.border=
"2px solid blue";
}
4
/* window.onload=function(){ // Anonymous function
document.getElementById("bdy").style.backgroundColor=
"lightgreen";
}
*/
5
function getToday() {
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
return (weekday[d.getDay()]);
}
</script>
</head>
6
<body id="bdy">
<div align="center">
<script type="text/javascript">
7
document.write("<h1>Today it is " + getToday() +"!</h1>");
</script>
</div>
</body>
</html>
EXPLANATION
1
When the page has completely loaded, JavaScript will execute the user-defined
function called setBorder . Notice that there are no parentheses or quotes when set-
Border is assigned to the event handler. That is because setBorder is being assigned
as a reference to the function defined on line 2.
2
The function setBorder() will be called when the onload event is fired on line 1.
3
The purpose of this function is to use the getElementById() method to retrieve a
reference to the body of the document and with the reference create a solid blue
border in the body of the document.
Continues
Search WWH ::




Custom Search