|
|
And now...The Weekly Update Script
You are here: irt.org | Articles | JavaScript | Date and Time | And now...The Weekly Update Script Published on: Monday 3rd February 1999 By: Martin Webb IntroductionOne of the more common features shared by most sites is the "What's New" page. This page can contain anything from news and press releases, to listings of new features that have been added to the site that week. But implementing a weekly "What's New" page can be a daunting task. Depending on who and where you are, the week can start on Sunday or Monday. The first week of the year can be the first week which contains the 1st of January or the first Monday in January. And then there's the whole issue of reliability. Since this page will need to be updated weekly, you have to be able to rely on the human factor, i.e., can you trust someone to change the page and the links to it every week? This article describes how you can manipulate the date object to figure out which week of the year a date falls in, and where the first week of the year is. (In this case, week 1 is defined as the week with the first Sunday in January.) It is important to note that, on occasion, the first few days of a particular year may fall into the last week of the previous year. Becase of this, we will need 53 weekly pages instead of 52 (366 [365 days + 1 leap day] divided by 7 days [1 week] does not exactly equal 52 weeks). A Self-modifying linkThis script can then be used to provide an automated link to the current page for the current week. For this article, let's assume that the pages week1.htm through to week53.htm will be used. The exact format of the page names can be adapted to suit the design of your own site. If a page contains a link to the current week's page, it might look something like this:
Since the link is hard-coded, we would need to amend this link each week, which would soon become a chore. This simple task could occasionally get overlooked, and instead of being a link to this week, it would become a link to last week, or even worse, a week several months ago. By using JavaScript, we can alter the HREF property of a link using the onClick event handler:
This previous link provides a default page (weekxx.htm) for browsers that don't support JavaScript, or where JavaScript has been disabled, and a link to the current week's page by replacing the current (this) links href property with week3.htm. We can now take this a step further and automate the weekly page, by using a JavaScript function (getWeek()) that returns the page name to be used:
Get the weekA date object can be created to hold the current date by using a simple line of JavaScript code:
This creates a new date object (today) that contains the current date. If we had specified parameters within the Date() method, for example Date(98,6,13), we can create a date object for any date we specified. We use the function, showWeek(), to figure out the current day, month, and year:
The getYear() date method returns a two digit date up to 1999, and a four digit date from 2000 onward. To ensure that we actually get a four digit date, we use the y2k() function, which when given a two or four digit year, will always return a four digit year:
The last part of the showWeek() function calls the getWeek() function. The getWeek() function is used to calculate the week number of the page to be loaded, which is returned to the selectWeek() function, which in turn returns a page name to the onClick event handler:
All we need to do now is to define the getWeek() function, which figures out which week the current date falls within:
Finally we need to return the week number:
Working exampleThe working example uses the following code:
Selecting Another WeekUntil now, the script has only permitted the user to select the current week's "What's New" page. Next we'll show you how to make it so the user can select any day of the year and display the "What's New" page for that particular week. We can keep the current getWeek() function as it stands. All we need add is a way for the user to select a date. Rather than let the user type in the required date, which then requires a large amount of validation to confirm that it is indeed a valid date, the following form restricts the users input to one of the 12 months and up to 31 days for a particular month:
Rather than using the existing showWeek() function, we are now using a new selectWeek() function. The selectWeek() function passes a reference to the current (this) form, using the buttons' onClick event handler. The selectWeek() function retrieves the values of the month and day from the passed reference to the form object. This invokes the isDate() function to check whether the month and day selected are indeed a valid date for the current year, before going on to invoke the getWeek() function for validated dates. For example, the date validation eliminates the user from getting the wrong week or an error if they select a date such as February 30th. Instead, if an incorrect date is entered, the user will get an alert message asking them to recheck the date.
The isDate() function simply attempts to create a date object with the year, month, and day the user selects, and compares that date object with the year, month, and day. If they match then the date is valid, and the user gets forwarded to the weekly page for the date they've selected:
You can test this advanced version by selecting a date from the following form: ConclusionIf you want to use Monday as the first day of the week then use the following getWeek() definition:
As you can see, the scripting is fairly simple, and it helps to reduce the chance of errors or problems that come along with having to remember to go back and change one simple link each week. By implementing this script, your users will be seemlessly transferred to the applicable "What's New" page, and also have the ability to select past weeks to see what they may have missed. Feedback on 'And now...The Weekly Update Script'
View the profile on Martin Webb and the list of other Articles by Martin Webb. |
-- div -->
|