Dialog box if there is no internet connection using jquery or ajax
Asked Answered
P

2

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...

Propylene answered 5/4, 2012 at 9:38 Comment(2)
Possible duplicate #189930Crenate
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 offlinePropylene
H
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

Haller answered 5/4, 2012 at 9:45 Comment(4)
what is x ,e where should i applyPropylene
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: api.jquery.com/jQuery.ajaxHaller
Status can be 0 for multiple other reasons than a network failure.Cowbind
0 could also mean that the firewall was blocked ( I know this from personal experience)Skewness
T
4
function isOnline() {
    var online = navigator.onLine;    // Detecting the internet connection
    if(online) {
       // do your stuff
    } else {
       alert('You\'re Offline now...');
    }
}
Twoup answered 5/4, 2012 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.