How to warn the user when there is no Internet connection

I need to warn the user with the following conditions:

  • Request timed out
  • No internet connection
  • Unable to contact server

Here is the code; How to record the following events and warn the user?

failure: function (response) { var text = response.responseText; console.log("FAILED"); },success: function (response) { var text = response.responseText; console.log("SUCCESS"); } 

I tried the following code to check if internet access is available, but it does not work.

 var networkState = navigator.network.connection.type alert(states[networkState]); if (networkState == Connection.NONE){ alert('No internet '); }; 

UPDATE **

I added the following to my index.html, but when I turned off WIFI, I do not see a popup warning.

 <script> function onDeviceReady() { document.addEventListener("offline", function() { alert("No internet connection"); }, false); } </script> 
+6
source share
5 answers

It’s best to listen to offline . When you receive an offline event, you can alert your user and take any steps necessary to save the data, etc.

For example, your callback is "deviceready":

 document.addEventListener("offline", function() { alert("No internet connection"); }, false); 

This code should work for most versions of PhoneGap. It has been included since at least version 1.0.

+19
source

Just like Simon said you can use

 document.addEventListener("offline", youCallbackFn, false); 

or you can request a boolean property

 navigator.onLine 

(Must return true or false)

However, this method will tell you if the device is connected. The caveat is that the device can be connected to Wi-Fi, but the router can be disconnected. In this case, use a polling mechanism, for example, timely Ext.Ajax.request with lower timeouts. Timeout expired = offline.

+3
source

You can use PhoneGap NETWORK API

The network entity provides access to cellular cellular device and Wi-Fi information.

You can test it as follows:

  function onDeviceReady() { navigator.network.isReachable("phonegap.com", reachableCallback, {}); } // Check network status // function reachableCallback(reachability) { // There is no consistency on the format of reachability var networkState = reachability.code || reachability; var states = {}; states[NetworkStatus.NOT_REACHABLE] = 'No network connection'; states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection'; states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK] = 'WiFi connection'; alert('Connection type: ' + states[networkState]); } 
+1
source

You can add 'Ext.device.Connection' to your app.js app. And check that your device is online or offline using the code:

 if (Ext.device.Connection.isOnline()) { alert('Connected to internet'); } else{ alert('You are not connected to internet'); } 
+1
source

Just paste it in your tag

 <body onoffline="alert('PLEASE CHECK YOUR INTERNET SETTING');"> 
0
source

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


All Articles