SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Periodical Page Refresh... that can handle a network outage.

    I need to refresh a page every N seconds. This must work even after a network outage.

    By default, if a browser reloads a page during network outage, the page is lost and the browser will display an error page. The page can't keep refreshing itself since there is no page anymore.

    Is there a way to reload a page "safely", kind of like google does it in gmail where you get a message saying that there are connection problems but you can still see your data and when the connection is back everything is normal again.

    thanks!

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ajax is the best thing to use here.

    Ajax can only access files on the same domain. The code below was tested in IE which allowed me to access Google's website via ajax. Firefox won't!

    Code javascript:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                var silentMode = false;
                var checker;
                function checkConnection() {
                    // Do a synchronous ajax call to the url in question
                    var req = createXMLHttpRequest();
                        req.open('GET', 'http://www.google.co.uk', false);
                        req.send(null);
     
                        // We can't find the page we want!
                        if(req.status != 200) {
                            // Should we ignore this error?
                            if(!silentMode) {
                                if(confirm('An error has occurred, do you want to hide any future errors until we connect again?'))
                                    silentMode = true;
                            }
                        } else {
                            // We've found the page.
                            if(silentMode) {
                                // If we have found the page after an error, let the user know its back up and running.
                                alert('We have connected again!');
                                silentMode = false;
                            }
                        }
                }
                function createXMLHttpRequest() {
                    if (typeof XMLHttpRequest != "undefined") {
                        return new XMLHttpRequest();
                    } else if (typeof ActiveXObject != "undefined") {
                        return new ActiveXObject("Microsoft.XMLHTTP");
                    } else {
                        throw new Error("XMLHttpRequest not supported");
                    }
                }
                window.onload = function() {
                    // Check ever 2 seconds for a connection.
                    checker = setInterval(checkConnection, 2000);
                    // To stop checking, use clearInterval(checker);
                }
            </script>
        </head>
        <body>
        </body>
    </html>



Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •