HTML and CSS Reference
In-Depth Information
EXAMPLE
10.11
<html>
<!-- This script is a modification of a free script found at
the JavaScript source.
Author: Asif Nasir (Asifnasir@yahoo.com)
-->
<head>
<script type="text/javascript">
1
var today = new Date();
2
var year=today.getFullYear();
3
var future = new Date("December 25, " + year);
4
var diff = future.getTime() - today.getTime();
//
Number of milliseconds
5
var days =Math.floor(diff / (1000 * 60 * 60 * 24 ));
//
Convert to days
6
var str=
"Only " + days + "
shopping days left until Christmas!";
7
function scroller()
{
8
str = str.substring(1, str.length) + str.substring(0,1);
9
document.title=str;
10
window.status=str;
11
setTimeout(scroller, 300);
//
Set the timer
}
</script>
</head>
12
<body onload = "scroller()">
<b>
<font color="green" size="4">
Get Dizzy. Watch the title bar and the status bar!!
<br />
<image src="christmasscene.bmp">
</font>
</body>
</html>
EXPLANATION
1
The
Date()
constructor creates an instance of a new
Date
object, called
today
.
2
The
getFullYear()
method returns the current year in four digits.
3
Another Date object is created with the date “
December 25, 2010
” assigned to the
variable, called
future
, assuming the current year is 2010.
4
The difference in milliseconds between the future time and the current time is as-
signed to the variable called
diff
.
5
The milliseconds of time are converted into days, and the result is rounded down
by the Math object's
floor()
method.
6
The string variable, “
Only (number of days goes here) shopping days left until Christ-
mas!
”, is assigned to
str
.
Continues