Showing a Full Year with a Web Page Calendar - Explanation of the showYear() Function
(Page 4 of 4 )
This function is called by the Show Current Year's Calendar and Show any Year's Calendar buttons. When it is called by the Show Current Year's Calendar button, the argument passed is "current," meaning the current year. When it is called by the Show any Year's Calendar button, the argument that is passed is the year (e.g. 2005) of your choice.
Another function we saw which uses the word "current" as an argument is showMonth(). With this function, the argument "current" refers to the current month, while with the showYear() function, we are referring to the current year. Do not confuse the roles of the "current" value between these two functions.
The first thing the showYear() function does is declare the date variable. This variable will become a date object. This date can either have the year for the current year or the year of your choice.
The next statement you have is an if-statement. If the argument received by the showYear() function is "current," then the if-statement creates a date object without its own arguments. When a date object is created without arguments, it is the current date object. This current date will be used to create the current yearly calendar. The newly-created date object is assigned to the date variable we have just seen.
If the argument of the showYear() function received was the year of your choice, then the if-statement creates a new date object with the year of your choice. This date object will be used to create the year calendar of the year of your choice. This alternative newly-created date object is assigned to the date variable that we have just seen.
The next thing we have is the declaration of the monthI variable. This variable will have the value A, or B, or C --- L. Remember, each of this letters represents a particular month of the year.
The next statement is a for-loop. It is this for-loop that calls the showMonth() function twelve times to display the twelve months of the yearly calendar. The first statement in the for-loop is an if-else-if statement. For the first iteration, the if-else-if statement assigns the value A to the monthI variable. For the second iteration, the monthI variable is assigned the value B. For the third iteration, it is C, and so on, until the twelfth iteration, which is L.
After the if-else-if statement in the for-loop, the next statement sets the month for the date object. The month is set twelve times for the numbers 0 to 11, each number for each iteration. When the showMonth() function is called, this set of months will be used to create the calendar for the year.
The last statement in the for-loop calls the showMonth() function. The showMonth() function will be modified again, and it will receive three arguments, as follows:
function showMonth(date, theMonth, monthI)
This function is called twelve times by the for-loop of the showYear(year) function. The first parameter, date, has the year of the particular month. For the twelve times the function is called, the value of the year attribute does not change, but the value (0 or 1 --- 11) for the month attribute changes.
The second parameter, theMonth, is important to only the first button, Show Current Month's Calendar. When this button is clicked, the argument is "current." For any other call of the showMonth() function the argument is "".
The last parameter, monthI, is used to set the IDs in the showMonth() function. This argument may be A or B --- L. Each of these letters corresponds to a number for the month in the date argument. So the last statement in the for-loop is:
showMonth(date, "", monthI);
Well, that is it for the showYear() function. We shall take a break here and continue in the next part of the series. As I have been saying, I will give you all the code in a zip file.
| 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. |