250
Creating Cool Web Sites with HTML, XHTML, and CSS
Here is the result: Page last modified Wednesday, December 17, 2003 8:12:44 PM
This is amusing because the page always reports that it was last modified at exactly the moment the visitor is viewing the page!
A built-in clock One additional neat thing you can do with the time methods is to output a clock container that stays up-to-the-second while someone is viewing the page. It’s a bit more complex, because it uses a lot of JavaScript. Here’s the code: <html>
<head><title>Does anybody really know what time it is?</title>
<script language=”JavaScript”>
function clock() {
var now = new Date(); var hours = now.getHours();
var amPm = (hours > 11) ? “pm” : “am”;
hours = (hours > 12) ? hours - 12 : hours;
var minutes = now.getMinutes();
minutes = (minutes < 10) ? “0” + minutes : minutes;
var seconds = now.getSeconds();
seconds = (seconds < 10) ? “0” + seconds : seconds;
dispTime = hours + “:” + minutes + “:” + seconds + “ “ + amPm; if (document.getElementById) { document.getElementById(“clockspace”).innerHTML = dispTime;
}
setTimeout(“clock()”,1000);
}
</script>
</head>
<body onload=”clock()”>
The actual time right now is:
<span id=”clockspace”></span>
</body>
</html>
Figure 11-3 shows a screenshot of the preceding code with all the additional snippets explored in this section thrown in for good measure. Notice that the page loaded at 16:23 (4:23 p.m.), but because the built-in clock keeps track of time, the actual time indicates 4:42 p.m. The difference between the two times can be a bit subtle: The first time indicates when the page