views:

132

answers:

3

I have many web pages that needs to auto-refresh once a minute. Easily done with META REFRESH or a some javascript. (And yes, the whole pages needs to refresh -- LOTS of content changing).

However, it needs to be as robust as possible. If the web server is momentarily down or there is a network hiccup, it can't refresh and will then get a 404 error, etc and be permanently stuck on the error page.

The only option I can come up with is host the whole page in an IFRAME, and have some script on the parent page fresh the framed page. The frame should be invisible so any resizing of the window would also need to resize the IFRAME.

Is there an easier, more elegant solution? (Going to Flash/AIR/Silverlight also isn't an option because of time constraints).

+1  A: 

Google uses the iframe method for gmail. Can't go wrong with google's solution.

Galen
+1  A: 

You could load the new content of the page using Ajax. If your page is generated on the server side you can just omit the HTML around the body and only output it's content. You can then receive the new body with Ajax and replace the existing body of the page with body.innerHTML = request.responseText. In the Ajax callback you can do all kind of error handling you like, even ignore any error and retry the Ajax request.

<html>
<head>
<script type="text/javascript">
function doRequest() {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200)
                 body.innerHTML = request.responseText;
            doRequest(); // restart the request
        }
    }
    request.open("get", "", true);
    request.send(null);
}
</script>
<body onload="doRequest()">
Page content...
</body>
</html>
Reboot
That's a cool idea. I'm doing it now, except replacing document.documentElement.innerHTML instead. That way the page can continue to return normally with head and body tags. Seems like it works fine in all the major browsers as far as I can tell.
DougN
I avoided that because I'm not sure how the browser handles it if you add a new document with script and css tags in the header. It might reload the files every time you replace the page, which may not be what you want. Another idea could be to use response.responseXML and get the body tag from it and clone it into your document using DOM methods, but I guess for that your page has to be XHTML, otherwise the XML parsing could fail.
Reboot
Hmmm, yeah, after more testing it looks like documentElement.innerHTML doesn't work -- it isn't loading CSS with Chrome, and scripts don't run with Chrome or Firefox. So I'm back to looking for a solid solution that doesn't require re-writing every page. Uggg, I hope iframes isn't the only option.
DougN
Do the headers of your page change when they reload, or is it enough if the content (body) of the page is updated? Does the body of your page contain JavaScript?
Reboot
The headers don't change. The body does contain javascript. With Chrome, CSS isn't used after refreshing documentElement.innerHTML.
DougN
I finally decided to do something like this where I load a small graphic. If a 200 status comes back, then just do a simple window.location = page. Not 100% guaranteed, but a lot closer than what I had.
DougN
A: 

I'm a bit new to JS. How do i set this up with a 10 second timeout between refreshes?

Bill
window.setInterval(function(){}, 10000); // args: callback, milliseconds
Josiah Ruddell
thanks. sorry. really new to JS. how do I use that?
Bill