Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q832 Can I detect when the page was last loaded, and when the page is reloaded - say the user presses the back button - then reload the page?

You are here: irt.org | FAQ | JavaScript | Redirect | Q832 [ previous next ]

If you include the time since year dot in miliseconds as part of the locations search property - then when the page is loaded, if the time isn't there, then reload, if the time is there, then check to see how long ago that time was - if its more than say a 10 seconds you can reload it again with another time:

<SCRIPT LANGUAGE="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

var now = new Date();
var nowsecs = Date.UTC(y2k(now.getYear()),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds());

if (location.search) {
    var thensecs = location.search.substring(1);
    var difference = nowsecs - thensecs;
    if ((nowsecs - thensecs) > 10000) {
        location.href = location.href.substring(0,location.href.length - location.search.length) + '?' + nowsecs
    }
}
else {
    location.href = location.href + '?' + nowsecs;
}
//--></SCRIPT>

To avoid the double load of the document - then you could alter links to the document to provide the time in the first instance:

<SCRIPT LANGUAGE="JavaScript"><!--
function getTime() {
    var now = new Date();
    return Date.UTC(y2k(now.getYear()),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds());

}
//--></SCRIPT>

<A HREF="mydocument.htm" onClick="this.href = 'mydocument.htm?' + getTime()">text link</A>

Feedback on 'Q832 Can I detect when the page was last loaded, and when the page is reloaded - say the user presses the back button - then reload the page?'

©2018 Martin Webb