Robust auto-refresh web page

前端 未结 3 1383
半阙折子戏
半阙折子戏 2020-12-17 05:57

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 c

相关标签:
3条回答
  • 2020-12-17 06:16

    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>
    
    0 讨论(0)
  • 2020-12-17 06:23

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

    0 讨论(0)
  • 2020-12-17 06:24

    You can also Use JQUERY load method.

    0 讨论(0)
提交回复
热议问题