admin管理员组文章数量:1315339
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 e 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).
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 e 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).
Share Improve this question asked Sep 16, 2010 at 19:16 DougNDougN 4,62712 gold badges60 silver badges83 bronze badges3 Answers
Reset to default 5You 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>
Google uses the iframe method for gmail. Can't go wrong with google's solution.
You can also Use JQUERY load method.
本文标签: javascriptRobust autorefresh web pageStack Overflow
版权声明:本文标题:javascript - Robust auto-refresh web page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975325a2408087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论