Welcome to the fourth part of an eight-part series on constructing a web page calendar. In the previous part, we discussed the first four code segments for the ShowCurrentMonth function of this application. In this part, we will tackle the last three code segments, move on to the validateDate function, and make modifications that will let you accurately display any month of any year.
Showing Any Month with a Web Page Calendar - Modifying Statements in the showMonth() function (Page 5 of 5 )
The showMonth(date, theMonth) function is a modification of the showCurrentMonth() function. We have not yet modified the function. Let us do it now.
When the showMonth(date, theMonth) function is called, it is possible that it had been called before, meaning that there is already a monthly calendar on the screen. The calendar should be removed before the new one is displayed. So, the very first line of the showMonth() function should be
So theDay is now the date object that will be used to develop the calendar of any month, including the current month. As you may recall, today in the previous showCurrentMonth() function was meant for the current date; theDay is meant for any date.
The attribute of the Show Current Month's Calendar button takes care of the current month creation. So we do not need to worry about that too much in the showMonth() function. So change all the "currentMonth" variables to "month" and all the "currentYear" variables to "year."
Change the last code segment from
//give special background color to today's date number.
Before I explain what is going on here, add the following global variable to your script:
//for removing the special background color of today's month number
var oldIDToday;
Now, let me explain the above code segment. Remember that the second parameter of the showMonth() function is theMonth. When you click the Show Current Month's Calendar button, its value is “current.” When you click the Show any Month’s Calendar button, this value is “”; that is an, empty string.
The first line above tests if the value is “current” (for the current month). If it is “current,” the if-statement gives the table cell of the current day number of the month a background color of "BurlyWood." Under this condition, it goes on to give the oldIDToday global variable the value of the IDToday variable. Remember that the value for IDToday is assigned when the function is filling the table cells with month day numbers. This value can change each time you call the showMonth() function.
Now, if the theMonth parameter does not have the “current” value, meaning it has the “” value, then the special background color for the table cell has to be removed. Remember that this background color is set by giving a CSS style to the table cell.
When you open the web page and display the current month, the special background color would be set. When next you show the calendar of any month, the color remains at the same cell, everything else being equal. So if the value of theMonth is not “current,” we use the ID of the cell that was stored in oldIDToday to give the color that other cells have.
At the end of the series, I will give you the complete code in zip file format. So do not worry about the modifications we make as we go along. Let us take a break here. We continue in the next part of the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.