Check internet connection with telephone connection

I see several different options for checking my phoneโ€™s Internet connection. There is document.addEventListener("online", onOnline, false); , and there is also navigator.network.connection.type ... but I'm not sure which one is the best. I would also like to be able to prevent a bad case when the phone is connected to a Wi-Fi network but does not have an Internet connection.

 $(document).on('pagecreate','#explanation-short', function(){ if ( isPhoneGap() ) { if (checkConnection() == "none" ) { connectionStatus = 'offline'; } else { connectionStatus = 'online'; } function checkConnection() { var networkState = navigator.network.connection.type; var states = {}; states[Connection.UNKNOWN] = 'Unknown connection'; states[Connection.ETHERNET] = 'Ethernet connection'; states[Connection.WIFI] = 'WiFi connection'; states[Connection.CELL_2G] = 'Cell 2G connection'; states[Connection.CELL_3G] = 'Cell 3G connection'; states[Connection.CELL_4G] = 'Cell 4G connection'; states[Connection.NONE] = 'No network connection'; //console.log('Connection : ' + Connection); //console.log('Connection type: ' + states[networkState]); return networkState; } } else { connectionStatus = navigator.onLine ? 'online' : 'offline'; } console.log("connectionStatus : "+connectionStatus); }); 

For example, this code works if the phone is connected to Wi-Fi, but we are not sure that the Internet is really available.

What are the best practices for working with a 3.3+ phone and jQuery mobile 1.4?

+6
source share
3 answers

Event,

 document.addEventListener("online", onOnline, false); 

It starts only when your application is already loaded and restores the connection. This does not start when the application starts.

Therefore, you must use the checkConnection() method when starting your application, and then if you need to check later when the application is running, you use an event listener.

+7
source

Inside the device ready function, I use the following lines to check my internet connection

 if(navigator.connection.type==0) { alert('This application requires internet. Please connect to the internet.'); } else if(navigator.connection.type=='none') { alert('This application requires internet. Please connect to the internet.'); } else { //Hurray I'm online } 

This currently serves me well. But I still do not have such a way to detect connected Wi-Fi, but not the Internet . I would also like to know how to detect this.

+4
source

Phonegap has two event listeners, online and offline. Use them for a quick response. Please check the example:


 var internet; document.addEventListener('online',online,false); document.addEventListener('offline',offline,false); function online(){ internet=true; } function offline(){ internet=false; } if(internet){ // internet is connected }else{ // internet is not connected } 

For more information, visit http://docs.phonegap.com/en/3.0.0/cordova_events_events.md.html

0
source

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


All Articles