Forum Moderators: open
I am trying to create a webpage that displays events, and from time to time the webpage is updated. When it is updated I want it to automatically update all the clients viewing the webpage with a refresh, but only when it is updated.
I know I can add this into the header of the webpage "<META HTTP-EQUIV=Refresh CONTENT=30>", and the clients will update every 30 seconds, but this is not efficient in my opinion.
All suggestions are welcome.
I think you're going to have a hard time doing what you suggest. If you want a page downloaded by the client to check for updates on the server it came from, something on that page is going to have to make a request every 30 seconds (or other period).
I tried to find a way to query files in JavaScript, but came up blank. document.location does have a property called hash, but that's not a hash of the page, which would tell you whether its content has changed. Maybe someone else knows a method?
I'd recommend putting whatever content needs to update in an iframe or something - even an invisible frame that causes a reload of the main page - since it seems like HTTP requests are going to be the easiest way to go. You can then use a META refresh just on that page, and check whether your content has been updated using querystrings or something.
hth,
g.
Other option
Harder part:
In your server-side script, you are going to have to have access to how long it is before you next forced refresh. Depending on what this event is, this could be hard.
Once you have that value, it should be rather easy:... when outputting your page with your script, in the header, in your script tag, do something like.
function blah()
{
[server script outputs here:]
setTimeout(window.refresh, HOW_LONG_TO_WAIT);
}
<body onload="blah();">
window.refresh is pseudo javascript: im not that familiar with js off the top of my head.