8

If there is no internet connection ,it will show some error message using dialog box like " No internet connection" without using java .I need to display using jquery or ajax script alert...

Raj
  • 193
  • 1
  • 3
  • 13
  • 1
    Possible duplicate http://stackoverflow.com/questions/189430/javascript-how-to-detect-that-the-internet-connection-is-offline – Akhil Thayyil Apr 05 '12 at 09:43
  • i need to show some dialog box in my mobile app if there is no internet connection.dont bother about button...if i am in offline – Raj Apr 05 '12 at 09:45

2 Answers2

13

In your JQuery ajax call, you could use the following and then query the status code of the error. Note that the status code will be 0 if they are offline, but you can also query other status codes (see below for a list):

$.ajax({
    //your ajax options
    error: function(statusCode, errorThrown) {
        if (statusCode.status == 0) {
            alert("you're offline");
        }
    }
});

Here's a list of status codes you could also catch for reference: http://support.google.com/webmasters/bin/answer.py?hl=en&answer=40132

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • what is x ,e where should i apply – Raj Apr 05 '12 at 09:49
  • x is the HTTP Status Code of the error and e is the error object, sorry I'll update the names in my code. You should put the error inside your existing jQuery ajax call. If you post your actual code I can show you exactly where it needs to go. Take a look at the error function here: http://api.jquery.com/jQuery.ajax/ – Mathew Thompson Apr 05 '12 at 09:53
  • 2
    Status can be 0 for multiple other reasons than a network failure. – Mirko Adari Aug 22 '13 at 20:42
  • 0 could also mean that the firewall was blocked ( I know this from personal experience) – Stardust Dec 21 '15 at 22:04
4
function isOnline() {
    var online = navigator.onLine;    // Detecting the internet connection
    if(online) {
       // do your stuff
    } else {
       alert('You\'re Offline now...');
    }
}
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164