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?
Louis source share