Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: validation for connectivity

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default validation for connectivity

    Hi,

    This will sound silly but I need a script that tells if you are connected to the internet, and if the user is not, then it will show an error message. It is a long story why this script (I know it doesn't make sense) but I am in DIRE need for this script, and I don't know what to do

    Can anyone help?

    Thanks,
    -JShor

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    You could do this with PHP quite easily.
    PHP Code:
    <?php
    if (file_exists('http://www.google.com.au')) {
    echo 
    'It works!';
    }
    else {
    echo 
    'Error! You cannot view Google!';
    }
    ?>
    But I'm not sure if you can do this with JavaScript.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This works but, you cannot simply navigate to the page unless your cache is disabled or cleared (arrow2.gif or whatever you use there must exist locally):

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <img src="arrow2.gif" onerror="alert('not connected!');">
    <script type="text/javascript">
    document.images[0].src='http://www.google.com/intl/en_ALL/images/logo.gif';
    </script>
    </body>
    </html>
    Or, what you can do though is, once you have navigated to it, hit refresh.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default THe otherway is

    Ajax.
    Send a request for conformation that you are online.

    Cookies.
    I'm sure there is a way to test. Maybe use a domain URL check.
    IE domain must be "http://www.example.com"


    Easily hacked though, but so is the php, unless it writes some encrypted code.


  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This is very useful if you have, for example, a slideshow, so you don't keep running it if you're disconnected... it should know, then stop.
    Sure, it could be hacked, but this isn't security.... it's functionality.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
      function checkConnection(yesfunc, nofunc) {
        var e = document.createElement("img");
        e.onerror = function() {
          nofunc();
          e = null;
        };
        e.onload = function() {
          yesfunc();
          e = null;
        };
        e.src = "http://www.google.com/images/google_sm.gif?c=" + (new Date().getTime());
      }
    
      checkConnection(
        function() {
          alert("Internet connection succeeded.");
        },
        function() {
          alert("Internet connection failed.");
        }
      );
    </script>
    This is more reliable than AJAX: there are less reasons it could give a false negative (although there are still some).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Twey, I had tried something like that at first, using new Date().valueOf(). However, either due to something at Google or a broader problem with the concept, it didn't work. Your code doesn't either. With a remote hosted image on a different server (preferably one that you control), it could work.

    In any case, the code I eventually came up with works as I described in my post. It may be able to be improved so as to still use the ubiquitous Google logo while not requiring user intervention to refresh the page.

    I wonder though, how the folks at Google feel about something like this?
    Last edited by jscheuer1; 04-22-2007 at 01:55 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey, I had tried something like that at first, using new Date().valueOf(). However, either due to something at Google or a broader problem with the concept, it didn't work. Your code doesn't either. With a remote hosted image on a different server (preferably one that you control), it could work.
    "You" as in me, or "you" as in the OP? I was trying to avoid depending upon a remote service that might change or be discontinued in the future. This might work if the OP has a server with the appropriate facilities.
    I wonder though, how the folks at Google feel about something like this?
    I doubt they'd mind terribly, if they even noticed it. It would probably account for something like one in a billion of their requests, even if it were to be checked regularly. In any case, the Google logo was just an example of an image I thought was reasonably likely to stay there for, if not the life of the Internet, a darned long time.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I meant you as in anybody trying to do this. I subsequently tested this concept on an image on a site I master and it worked fine, as I suspected that it would.

    Apparently Google has something going on that prevents it from working with their images. Most likely, whatever that is isn't intended to break our scripts, it just does so as a side effect. Other servers over which one has inadequate control could also break a script like these, intentionally or otherwise.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default surely.....

    Doesn't

    Code:
    if(document.location.toString().lastIndexOf(/http/) > 0)
     {online}
    else
     {offline}

    Quicker

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
  •