0

I'm making an app with XDK, so I need to show a connection failure message. I want to check that with a button that when I click on it check the connection and if it's online goes to a webpage, otherwise goes to another with the failure connection message.

Is there any solution, method or function with javaScript or jQuery for do this status check?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

0

Any xhr request to your server should do the trick. If it comes back with a timeout, then you know you don't have a connection (or that your server isn't working).

Paul
  • 35,689
  • 11
  • 93
  • 122
0

Use an XMLHttpRequest to query any file, if it succeeds you are connected else you ain't.

function checkConnection(){
 var xhr = new XMLHttpRequest();
 var file = "http://yoursite.com/somefile.png";
 var r = Math.round(Math.random() * 10000);
 xhr.open('HEAD', file + "?subins=" + r, false);
 try {
  xhr.send();
  if (xhr.status >= 200 && xhr.status < 304) {
   return true;
  } else {
   return false;
  }
 } catch (e) {
  return false;
 }
}
ahjashish
  • 573
  • 1
  • 3
  • 13