Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

google chrome - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...