4

Assuming there is no internet connection, of course. Like a jQuery method?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Doug Smith
  • 29,668
  • 57
  • 204
  • 388

6 Answers6

10

I would try to make HEAD requests (no content downloaded) to a few servers you know are online. They will automatically fail if there is no network (no need to set a timeout).

$.ajax({
    type: "HEAD",
    url: 'http://www.google.com',
    error: function() {
        alert('world is gone !');
    }
});

DEMONSTRATION (unplug your network to test)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

If you are dealing with ajax requests, you can catch timeout error - see error(jqXHR, textStatus, errorThrown) in $.ajax reference.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
1

Sort of, yes. Before the actual submission, you can have an ajax call try to connect to a simple lightweight service on your site just to check if it is reachable.

If the call fails, you can assume there's no connection.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
0

With help of failed XHR requests you can determine the connection. Retry few times , if the request doesnot go through, alert and fail gracefully.

I can use this function which I got from http://jamiethompson.co.uk/web/2008/06/17/publish-subscribe-with-jquery/

$.networkDetection = function(url,interval){
    var url = url;
    var interval = interval;
    online = false;
    this.StartPolling = function(){
        this.StopPolling();
        this.timer = setInterval(poll, interval);
    };
    this.StopPolling = function(){
        clearInterval(this.timer);
    };
    this.setPollInterval= function(i) {
        interval = i;
    };
    this.getOnlineStatus = function(){
        return online;
    };
    function poll() {
        $.ajax({
            type: "POST",
            url: url,
            dataType: "text",
            error: function(){
                online = false;
                $(document).trigger('status.networkDetection',[false]);
            },
            success: function(){
                online = true;
                $(document).trigger('status.networkDetection',[true]);
            }
        });
    };
};
Rahul
  • 1,549
  • 3
  • 17
  • 35
0

One way to go about it is to send your form using AJAX. Then you'll have a handler that will tell you that it wasn't able to connect to the server and you can save the data and inform the user that the server is not available. It doesn't really matter whether the problem is your internet connection or the server may be down.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
-2
if (navigator.onLine) {
  alert('online');
} else {
  alert('offline');
}
teewuane
  • 5,524
  • 5
  • 37
  • 43
  • That property is not reliable across browsers http://ejohn.org/blog/offline-events/ – Ruan Mendes Sep 26 '12 at 18:01
  • @JuanMendes ah, So others can learn of this, I will not delete my down voted answer :) Also, I should have read this part that talks about browser compatibility. https://developer.mozilla.org/en-US/docs/DOM/window.navigator.onLine#Browser_compatibility – teewuane Sep 26 '12 at 18:03
  • Regardless of the accuracy of the answer, you should always explain your code and why we should use it (often a sentence or two is enough.) – Sam Sep 26 '12 at 18:06