HTML and CSS Reference
In-Depth Information
◗
4.
Save your changes to the file.
◗
5.
Reload the
ccc.htm
file in your Web browser. The table cell corresponding to
July 6, 2015 now should be highlighted with an ivory background and a solid
black border as shown earlier in Figure 12-1.
Displaying the Current Calendar Month
Maria is pleased with the calendar application, but she wants it to display months other
than July 2015. You can display different dates by changing the value of the
calDate
variable in the calendar() function. Another option is to create a parameter for the func-
tion that supplies the calendar date as a text string. Users then could specify the date
when the function is called. It would be ideal if the calendar() function worked like the
JavaScript
new
Date()
object constructor, creating a monthly calendar for a specified
date with the command
calendar(“March 18, 2015”)
but producing the calendar for the current month if no date is specified as follows:
calendar()
To test for the presence or absence of a parameter value, you'll insert an
if
condition
that tests whether the supplied date string is
null
. If the parameter value is
null
, the
calDate
variable will be set to the current date; otherwise, the
calDate
variable will be
set based on the date string specified by the user. The revised calendar() function will
appear as follows:
function calendar(dateString) {
if (dateString == null) calDate = new Date()
else calDate = new Date(dateString);
...
You'll edit the calendar() function to add this feature, and then you'll retest the function
using both the current date and a date that you specify.
To create and test the
dateString
parameter:
◗
1.
Return to the
calendar.js
file in your text editor, go to the calendar() function, and
then add the parameter
dateString
to the function line.
◗
2.
Replace the statement that creates the
calDate
variable with the following (see
Figure 12-30):
if (dateString == null) calDate = new Date()
else calDate = new Date(dateString);