Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    876
    Thanks
    53
    Thanked 6 Times in 5 Posts

    How to check for Internet connection

    Hi. I am trying to think of an elegant way to have a js script check for an Internet connection, and only proceed if one exists. If not, then it displays "Hello World" or whatever I wish to say.

    Any ideas? I can only think of trying to visit a site, but I don't know how to write onfail etc.

    Thanks,

    Terry

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use an image object to load an image off the internet.
    Like so:

    Code:
    function doConnectFunction() {
      //I'm fired if I'm connected to the internet
    }
    function doNoConnectFunction() {
      //I'm fired if I'm not connected to the internet
    }
    
    
    var i = new Image();
    i.onload = doConnectFunction;
    i.onerror = doNoConnectFunction;
    i.src = 'http://www.internet.com/image.jpg';

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    876
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I can follow the logic here - thanks, but I can't get it to work!

    (Assume the graphic exists - it does.)

    I have this page:

    *********************

    <html>

    <head>
    <title>INTERNET CONNECT</title>
    <script>
    function doConnectFunction() {
    //I'm fired if I'm connected to the internet
    alert("hello");
    }
    function doNoConnectFunction() {
    //I'm fired if I'm not connected to the internet
    alert("goodbye");
    }
    var i = new Image();
    i.onload = doConnectFunction;
    i.onerror = doNoConnectFunction;
    i.src = 'http://www.aapress.com.au/img/71f.gif';
    </script>
    </head>

    <body>
    </body>

    </html>

    ***********************

    If I am connected to the net, I get the "hello" alert and nothing - which is fine.
    But if I am not connected to the net, I get the "hello" alert (fine) but no "goodbye".

    What is going on?

    Thanks

  • #4
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If it's always saying hello, then I think the browser my be caching the image, so that it's always loading it.

    Try puting the date in the url as a querystring like this:

    i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());

    That way the request is always unique.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    It could be the browser you are using. Not all browsers trigger an error if the image doesn't download. Some trigger the onload event handler when they give up trying to load the page even if the image didn't download and some don't support these events on images. The code can only be expected to work as required on some versions of IE.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    876
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I can live with the ie only possibility, but I stillcan't get the page to redirect properly if the connection fails. Instead I get the 404 page error. I now have:

    <html>

    <head>
    <title>INTERNET CONNECT</title>
    <script>
    function doConnectFunction() {
    //I'm fired if I'm connected to the internet
    redirURL = "http://www.yahoo.com.au";
    self.setTimeout("self.location.href = redirURL;",0000);}
    function doNoConnectFunction() {
    //I'm fired if I'm not connected to the internet
    redirURL = "ifnoconnect.html";
    self.setTimeout("self.location.href = redirURL;",0000);}
    </script>
    <script>
    var i = new Image();
    i.onload = doConnectFunction;
    i.onerror = doNoConnectFunction;
    i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());
    //escape(Date()) necessary to override possibility of image coming from cache
    </script>
    </head>

    <body>
    </body>

    </html>

    Just can't get that "ifnoconnect.html" page to display.

    Thanks for your help.

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    876
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I am stupid. It didn't display because it was being attempted within FrontPage preview - not within the browser.

    So it WORKS!

    Thanks once again.

    Who would have thought such a simple thing as trying to determine if you are connected to the net would be so difficult! Who wrote the javascript rules? Also, I'd much prefer it if the thing didn't have to waste time timing out. Isn't there some registry entry that js can check to see if ie is breezing about online? I guess not. Oh well...

  • #8
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd like to know this as well.


  •  

    Posting Permissions

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