10

I am Currently using Tom Riley's Jquery Plugin for Detecting Internet Connection in my Application, It works fine in Internet Explorer, but it does not respond when implementing it in Google Chrome.

Can anybody suggest a better Plugin for Detecting Internet Connection which works perfect in Google Chrome(all Browsers)

Piyush Poddar
  • 103
  • 1
  • 8

3 Answers3

11

You dont need a plugin for that, simply do:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
  if (! window.jQuery) {
    alert('No internet Connection !!');
  }
  else {
     // internet connected
  }
</script>

The reason why above works is because jQuery lib is read from google CDN for which internet connection is required, if page can not read it, it means internet connection isn't there.

Update

You can do something like:

function checkConnection() {
  var connected = true;
  var img = document.createElement('img');
  img.src = "path to remoate image on your server";
  img.onerror = function() { connected = false; }
  return connected;
}

You can use it like this at any time:

if (checkConnection()) {
  // connected
}

Update 2

You can periodically/automatically check for it too like this:

setInterval(function(){
  var isConnected = checkConnection(); // checkConnection() comes from above code
  if (isConnected) {
    alert('Connected');
  }
  else {
    alert('Not Connected');
  }
}, 10000); // 10000 = 10 seconds, check for connection every 10 seconds

Other useful links:

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Thanks for your Reply Sarfraz, but i am using google Maps and once loaded if i disconnectthe internet, it shows the Message in IE, but not in Chrome. – Piyush Poddar Jun 08 '12 at 09:01
  • @Sarfraz I am already using the checkNet plugin, it is working Perfectly fine in IE, but its not implementing in Google Chrome. – Piyush Poddar Jun 08 '12 at 09:18
  • @PiyushPoddar: I have also updated my answer with `checkConnection` function and other links – Sarfraz Jun 08 '12 at 09:19
  • @Sarfraz this is not working, my situation is my page is loaded in HTML, if i suddenly switch off my WIFI router then it should display a message and on switching on , it should again prompt a message.I have source code working fine in IE, i need something which works in Google Chrome. – Piyush Poddar Jun 08 '12 at 09:36
  • @PiyushPoddar: see Update 2 plz, it should work in ***your*** situation. – Sarfraz Jun 08 '12 at 09:43
  • @Sarfraz it works perfect for the functionality when Internet Connection is active, but I am getting Connected Alert only when i have disconnected my Internet. – Piyush Poddar Jun 08 '12 at 16:07
  • Is `error` event fired synchronously? If it isn't wont your `isConnected` always be `true`. – alex Jun 12 '12 at 01:02
  • @Sarfraz, I'd say use setTimeout instead (and recall the setTimeout when it returns true or false), especially when dealing with things like networking. – CJT3 Aug 09 '13 at 04:05
2

The checkNet plugin ( http://tomriley.net/blog/archives/111 ) works in all browsers, including Chrome. It does not rely on querying google.com (because google is blocked in come countries, and the connection between continents can be unreliable!)

If the error message doesn't show, it's usually because you are still on your local server - it will work when it's on the Internet.

Also, it would be very cool to see what you're using it for. Drop me a link if you feel like sharing.

tripRev
  • 830
  • 3
  • 12
  • 27
  • I am not working on local server, because i tested my HTML page on IE as well as Chrome, as i disconnect my Internet, the error message prompts on top in IE, but nothing happens on Chrome. please suggest if you got any other way around.... – Piyush Poddar Jun 19 '12 at 10:28
  • Can you give me details of your operating system and Chrome version? Easiest way is to send to tdriley att gmail from http://supportdetails.com/ also a link to your live site makes it easier to solve the problem. – tripRev Jun 27 '12 at 13:47
  • I've released a new version of my checkNet plugin - works much more efficiently now, and thoroughly tested in all browsers: http://tomriley.net/blog/archives/111 – tripRev Jul 01 '12 at 23:58
0

There is no need to download any file or image you can check that by a simple piece of code.

click bellow "Run code snippet" button and see it in action.

function checkInternetConnection(){
        var status = navigator.onLine;
        if (status) {
            console.log('Internet Available !!');
        } else {
            console.log('No internet Available !!');
        }  
        setTimeout(function() {
            checkInternetConnection();
        }, 1000);
      }
      checkInternetConnection();

Edit - More simple way without function

setInterval(function(){
    var status = navigator.onLine;
    if (status) {
        console.log('Internet Available !!');
    } else {
        console.log('No internet Available !!');
    }  
}, 1000); // 1000 = 1 seconds, check for connection after every 1 seconds
Muhammad Tahir
  • 2,351
  • 29
  • 25