JSHandbook | JSBible 2nd Ed. | JSBible 3rd Ed. |
Listing 16-1 Listing 16-2 |
Listing 18-1 Listing 18-2 |
Listing 47-1 Listing 47-2 Listing 47-3 |
All three editions of my JavaScript book contain examples of what I call 'static' and 'dynamic' tables. These pages use a monthly calendar view to demonstrate scripting techniques for these kinds of tables. Due to changes in JavaScript's getYear() method between Navigator 2 and 3, these calendars do not display months correctly starting with January 2000 when viewed in recent browsers.
The problem centers on the fact that getYear() started its life returning the year value as the year minus 1900. But starting with Navigator 3, the method returned a four-digit year for years starting with 2000. While Navigator 4 and IE 4 include the comparatively new getFullYear() method to extract four-digit years from a date object, this is not available in older browsers. For the sake of compatibility with all browsers, I have modified the calendar scripts to simulate getFullYear(), and thus fix the problem.
At the core of the repair is a new function:
// new function (16SEP1998) to take care of getYear() anomalies for Y2K
function get4DigitYear(yrValue) {
yrValue += (yrValue < 1900) ? 1900 : 0
return yrValue
}
Any script statement that invokes the normal getYear() method should wrap itself inside a call to get4DigitYear(). For example, the following statement:
var firstDay = theMonths[monthOffset].getFirstDay(today.getYear())
becomes:
var firstDay = theMonths[monthOffset].getFirstDay(get4DigitYear(today.getYear()))
In addition, each of the original scripts includes a statement with a new Date() constructor whose first parameter value has 1900 subtracted from it. Because all scripts are now designed to work in the four-digit format, remove all -1900 parts from such statements.
You can try out each of the examples (depending on your browser). Right-click (or click-and-hold for the Mac) on each link below to choose to save the link to your local hard disk as HTML source code.
|