Popup Calendar 1.1 - DHTML Lab | 2
Pop-up Calendar 1.1
The next-month navigation problem
Note
This page uses DHTML Lab Popup Calendar 1.0 (the previous version) in order to demonstrate the problem discussed.
The Problem
Click the image below to display a calendar. The root_date attribute has been set to 2004,08,30 so you'll see August 30 selected.
Click the next-month arrow to navigate to September.
Try it:
The behavior is as expected. Now, pop up a calendar from the next image, below. The root_date attribute has been set to 2004,08,31 so August 31 is selected.
Click the next-month arrow to navigate to September.
Try it:
Oops, we've ended up in October!
The Reason
Technically, the reason for this unwanted behavior is simple. In the script, we keep track of the displayed date, as well as the selected date. When we navigate using the next-month arrow, the script simply increments the displayed date month by 1 (one).
In our first example, the displayed date changes from August 30 to September 30, and the calendar displays the full month of the latter date: September.
In the second example, the displayed date changes from August 31 to September 31. There is no such date, so JavaScript automatically returns the date that is one day later than the last valid date. That is,
September 30 + 1 = October 1.
The calendar correctly displays the full month of the new displayed date: October.
The Fix
The calendar script keeps track of the displayed date, for the sole purpose of identifying what month is currently visible, and what month to navigate to. The date (day-of-the-month) part of the displayed date is irrelevant.
Therefore, the displayed date object could always have a date of 1 (one) without affecting functionality and ensuring that we never navigate past the end of a month.
For those who have perused the calendar script itself and are familar with its logic, the single statement to correct the problem has been added to the dlcalendar_navigate() function.
Send a comment or suggestion to Peter Belesis
Created: September 07, 2004Revised: September 07, 2004
URL: https://webreference.com/dhtml/column69/2.html