lbutlr
lbutlr

Reputation: 424

Automatically reload a webpage when it expires

I have an HTML page with headers like this:

<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="Wed, 07 Oct 2020 09:50:03 -0600" />
<meta http-equiv="pragma" content="no-cache" />

It is currently 04:45:17 on that date and I want that page to automatically reload at 09:50:03, at which point the page will have a new expiration date in the met tag, and I want the page to reload based on the new date.

I can reload on an interval, I can use JavaScript to reload if any the files on disk change, but I can’t figure out how to reload based on the expires header. PHP or JavaScript solutions preferred.

Upvotes: 1

Views: 1100

Answers (3)

const expiresHeader = document.querySelector("meta[http-equiv='expires']").getAttribute('content');

let myinterval =new Date(expiresHeader).getTime() - (new Date()).getTime();
console.log("MyInterval:",myinterval);
 setInterval(function(){ refreshpage(); }, myinterval );
 
 
 refreshpage=()=>{
  window.location.reload(true);
 };
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="Wed, 07 Oct 2020 09:50:03 -0600" />
<meta http-equiv="pragma" content="no-cache" />

Upvotes: 0

Pramod Kharade
Pramod Kharade

Reputation: 2085

Answer already been given to extending existing answer with further:

const expiresHeader = document.querySelector("meta[http-equiv='expires']").getAttribute('content');

let myinterval =new Date(expiresHeader).getTime() - (new Date()).getTime();
console.log("MyInterval:",myinterval);
 setInterval(function(){ refreshpage(); }, myinterval );
 
 
 refreshpage=()=>{
  window.location.reload(true);
 };
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="Wed, 07 Oct 2020 09:50:03 -0600" />
<meta http-equiv="pragma" content="no-cache" />

Upvotes: 1

Adrian Solarczyk
Adrian Solarczyk

Reputation: 247

Is this what are you looking for?

const expiresHeader = document.querySelector("meta[http-equiv='expires']").getAttribute('content');

console.log(expiresHeader);
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="Wed, 07 Oct 2020 09:50:03 -0600" />
<meta http-equiv="pragma" content="no-cache" />

Then you should use library like https://day.js.org/ or JS Date API to parse expires and calculate the difference between current and expires date. Then maybe setTimeout based on that difference when DOM is loaded.

Upvotes: 2

Related Questions