Is there a way to tell the user that there is no internet connection when they click submit?

Assuming an internet connection, of course, is missing. How is the jQuery method?

+4
source share
6 answers

I would try to make HEAD requests (without downloading content) to several servers, which, as you know, are online. They automatically crash 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 (disconnect the network from testing)

+10
source

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

+2
source

Sort, yes. Before the actual presentation, you can have an ajax call to try connecting to a simple lightweight service on your site to check if it is available.

If the call fails, you can assume that there is no connection.

+1
source

With failed XHR requests, you can determine the connection. Try again a few times if the request fails, warns and fails.

I can use this function that 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]); } }); }; }; 
0
source

One way to do this is to submit the form using AJAX. Then you will have a handler that tells you that he was unable to connect to the server, and you can save the data and tell the user that the server is unavailable. It doesn’t really matter if the problem is your internet connection or the server may not be available.

0
source
 if (navigator.onLine) { alert('online'); } else { alert('offline'); } 
-2
source

Source: https://habr.com/ru/post/1436433/


All Articles