HTML and CSS Reference
In-Depth Information
Figure 12-24
setting the initial day of the month
date object
pointing to the first
day of the month
variable containing
the weekday
number (0-6)
2. Save your changes to the file.
Placing the First Day of the Month
Before the first day of the month, the calendar table should show only empty table cells
that would represent the days from the previous month. The value of the weekDay vari-
able indicates how many empty table cells you need to create. For example, if the value
of the weekDay variable is 4, indicating that the month starts on a Thursday, you know
that there are four blank table cells—corresponding to Sunday, Monday, Tuesday, and
Wednesday—that need to be written at the start of the first table row. The following loop
writes the correct number of blank table cells in the first table row:
document.write(“<tr>”);
for (var i = 0; i < weekDay; i++) {
document.write(“<td></td>”);
}
Note that if weekDay equals 0—indicating that the month starts on a Sunday—then no
blank table cells will be written because the value of the counter variable is never less
than the value of the weekDay variable, and thus the document.write() command is
completely skipped. You'll add these commands to the writeCalDays() function now.
To write the initial blank cells of the first table row:
1. Below the second comment line, insert the following for loop, as shown in
Figure 12-25:
document.write(“<tr>”);
for (var i = 0; i < weekDay; i++) {
document.write(“<td></td>”);
}
Search WWH ::




Custom Search