Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Plugin for Detecting Internet Connection

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)

like image 699
Piyush Poddar Avatar asked Jun 08 '12 08:06

Piyush Poddar


2 Answers

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:

  • Check if Internet Connection Exists with Javascript?
  • JQuery Mobile Detected if there's internet Connection
  • checkNet: My jQuery plugin for detecting Internet connection
like image 190
Sarfraz Avatar answered Sep 28 '22 04:09

Sarfraz


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.

like image 24
tripRev Avatar answered Sep 28 '22 06:09

tripRev