Click to See Complete Forum and Search --> : page last updated script trouble
rasyekj
09-02-2003, 09:55 PM
I am using the following script on two different websites to automatically display the time and date of when the page was last updated. On the first site, it works properly. On the second site, the date and time change when the page is refreshed, every time the page is refreshed, not when the page is updated. I am using the same exact script. Can someone please help? Maybe I am doing something wrong, or maybe you have a script that works better?
Thanks in advance. Script follows...
<script language="JavaScript">
<!--//Date Stamp
var dayArray = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var lastUpdate = new Date(document.lastModified);
var thisDay=dayArray[lastUpdate.getDay()];
var thisDate=lastUpdate.getDate() < 10 ? '0'+lastUpdate.getDate() : lastUpdate.getDate();
var thisMonth=monthArray[lastUpdate.getMonth()];
var thisMonthNum=lastUpdate.getMonth()+1 < 10 ? '0'+ (lastUpdate.getMonth()+1) : lastUpdate.getMonth()+1;
var thisFullYear=String(lastUpdate.getFullYear());
var thisYear= thisFullYear.charAt(2) + thisFullYear.charAt(3);
document.write("Site Last Updated: ");
document.write(thisMonth + ' ' + thisDate + ', ' + thisFullYear+ ' '+ lastUpdate.getHours()+':'+lastUpdate.getMinutes()+':'+lastUpdate.getSeconds());
//-->
</script>
This just gives the date,month and year, but that's easily changed.
Note the year check. It is necessary.
function Revision() {
var Updated=new Date(document.lastModified);
var Year=Updated.getYear();
if(Year<1900) { // some browsers return 103 for 2003 !!
Year +=1900;
}
var Month=Updated.getMonth();
var Day=Updated.getDate();
return Day+' '+Months[Month]+' '+Year;
}
Charles
09-03-2003, 05:28 AM
Originally posted by Fang
Note the year check. It is necessary.Date.getYear() was depricated in advance of the whole Y2K thing. Use instead Date.getFullYeaar().
All you reall need for a date stamp is
<script type="text/javascript">
<!--
document.write('<p>last modified: ', new Date(document.lastModified).toDateString(), '</p>');
// -->
</script>
That will format the date according to the user's settings. If you want to include the time or control format we make some simple changes:
<script type="text/javascript">
<!--
Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear(), [this.getHours(), this.getMinutes() < 9 ? '0' + this.getMinutes() : this.getMinutes(), this.getSeconds() < 9 ? '0' + this.getSeconds() : this.getSeconds()].join(':')].join(' ')}
document.write('<p>last modified:', new Date(document.lastModified), '</p>')
// -->
</script>
I don't think Date.getFullYear() is supported on some older browsers i.e. navigator 4
Charles
09-03-2003, 06:27 AM
It works just fine in Netscape 4. Though it is true that some really old browsers do not understand Date.getFullYear() a good 13% of users do not use JavaScript at all. More to the point Date.getYear() has been depricated which means that the people who know better than you or me have requested that we stop using it. It also means that they have given browser manufacturers permission to stop supporting it.
It didn't work in my navigator 4.0 (js1.2) (http://developer.irt.org/script/387.htm), getFullYear() was first implemented in 1.3
The getFullYear() is rarely used. Maybe it's time to throw those old browsers away and stop supporting them. ;)
jalarie
09-03-2003, 12:27 PM
rasyekj, are your two different websites on two different servers? The following could explain why this would matter.
Some ISP's block document.lastModified as part of a group of possibly-problem items rather than blocking only those individual items that really are problems. This causes the value sent to the browser to be null.
Internet Explorer gets the null value, uses it in the "Date()" function as-is, gets the CURRENT date and time as a result, and displays it for the user. The date and time will change with each refresh of the page.
Netscape gets the null value, forces it to be numeric, has a zero at this point, uses the zero in the "Date()" function, and gets "First midnight of January 1st, 1970" for the base value. It then corrects for timezone offset before displaying the result. For me, in the eastern US, the result is always "December 31st, 1970, 7:00pm."
WebTV works similarly to Netscape except that it corrects for timezone offset earlier, may get a negative value, and may generate an error.
Other JavaScript-enabled browsers all, as far as I know, follow one of the above examples. Non-JS browsers, of course, do nothing.
Nothing is blocked. On one site the document.lastModified is updated everytime the page is refreshed.
If you are using different browsers it is possible to set the option to update the page from the sever - a new copy of the page.
Or: could one of the servers be adding a meta tag to force a new update.
jalarie
09-04-2003, 08:11 AM
rasyekj, how about giving us links to the two pages so that we can see what we're talking about?