强大的自动刷新网页 [英] Robust auto-refresh web page

查看:205
本文介绍了强大的自动刷新网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多网页需要每分钟自动刷新一次。使用META REFRESH或一些javascript轻松完成。 (是的,整个页面需要刷新 - 很多内容都在变化)。

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).

但是,它需要尽可能健壮。如果Web服务器暂时关闭或网络打嗝,它将无法刷新,然后会出现404错误等,并永久停留在错误页面上。

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.

我能想出的唯一选择是将整个页面托管在IFRAME中,并在父页面上放置一些脚本以刷新框架页面。框架应该是不可见的,因此任何调整窗口的大小也需要调整IFRAME的大小。

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.

是否有更简单,更优雅的解决方案? (由于时间限制,转到Flash / AIR / Silverlight也不是一个选项。)

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

推荐答案

你可以加载新的使用Ajax的页面内容。如果您的页面是在服务器端生成的,那么您可以省略身体周围的HTML并仅输出它的内容。然后,您可以使用Ajax接收新主体,并使用 body.innerHTML = request.responseText 替换页面的现有主体。在Ajax回调中,您可以执行所有类似的错误处理,甚至可以忽略任何错误并重试Ajax请求。

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>

这篇关于强大的自动刷新网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆