Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Mobile Detected if there's internet Connection

What is the best way to detect if there's internet connection or not on my mobile via my web app?

like image 205
Satch3000 Avatar asked Apr 11 '11 20:04

Satch3000


2 Answers

There's no code necessary for this -- it's part of the HTML5 API. Check the value of window.navigator.onLine -- it will be false if the user is offline.

http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#browser-state

like image 155
Liza Daly Avatar answered Oct 22 '22 15:10

Liza Daly


An option might be changing the Ajax settings to add a specific timeout, then add an error handler that looks for a textStatus (second argument) of 'timeout'.

When a timeout occurs, either internet connectivity is spotty or your site is down.


Using ajaxSetup to set option defaults for all requests:

$.ajaxSetup({
    timeout: 1, // Microseconds, for the laughs.  Guaranteed timeout.
    error: function(request, status, maybe_an_exception_object) {
        if(status != 'timeout')
            alert("YOU BROKE IT");
        else
            alert("OH NOES TEH INTARWEBS ARE DOWN!!!!!1one");
    }
});
like image 37
Charles Avatar answered Oct 22 '22 16:10

Charles