admin管理员组文章数量:1403438
I want to check if the device has internet access. From the official cordova documentation:
This code, is only to get the connection type
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
checkConnection();
But how can I check if the device really have internet access? That is, because it may be that the device is connected to a router using a WiFi connection type, but that it has no access to internet but only local access. In this case the checkConnection();
will return: WiFi connection
.
Or that is connected to a 3G network phone operator, but the user does not have sufficient credits for the internet. In this case the checkConnection();
will return: Cell 3G connection
But in both cases the user have not a real internet access eventhough if connected.
The only thing that I think is to ping google or another server to check if there really has internet access, in the case of states[networkState] != No network connection
Or do you think navigator.onLine
can run on all devices (Android, iOS, BlackBery 10?
I hope I have expressed well, my English really sucks.
I want to check if the device has internet access. From the official cordova documentation:
This code, is only to get the connection type
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
checkConnection();
But how can I check if the device really have internet access? That is, because it may be that the device is connected to a router using a WiFi connection type, but that it has no access to internet but only local access. In this case the checkConnection();
will return: WiFi connection
.
Or that is connected to a 3G network phone operator, but the user does not have sufficient credits for the internet. In this case the checkConnection();
will return: Cell 3G connection
But in both cases the user have not a real internet access eventhough if connected.
The only thing that I think is to ping google or another server to check if there really has internet access, in the case of states[networkState] != No network connection
Or do you think navigator.onLine
can run on all devices (Android, iOS, BlackBery 10?
I hope I have expressed well, my English really sucks.
Share Improve this question edited Jun 20, 2022 at 14:04 snoob dogg 2,8954 gold badges36 silver badges67 bronze badges asked Aug 21, 2015 at 15:12 candlejackcandlejack 1,2092 gold badges24 silver badges52 bronze badges2 Answers
Reset to default 4Hmm... here is a discussion http://iswwwup./t/f3e5374b74ca/android-cordova-plugin-to-detect-internet-connection.html
And the solution they mentioned...
A function to test this address to check if the device has access to internet would look like this :
function testInternet(win,fail){
$.get("http://www.google.fr/blank.html").done(win).fail(fail);
}
Or ,
function testInternet(win,fail){
$.ajax({
url:"http://www.google.fr/blank.html",
timeout:5000, //timeout to 5s
type: "GET",
cache: false
}).done(win).fail(fail);
}
There is a plugin that helps you to do that: Network information
try it with a test function to an URL that test your request if it fails then you do somthing else, as many answers explained here, or use
document.addEventListener("online", onOnline, false);
function onOnline() { // Handle the online event}
document.addEventListener("offline", onOffline, false);
function onOffline() { // Handle the offline event}
Remember to wrap these functions inside a document.addEventListener('deviceready', function() {}, false);
本文标签: javascriptWhat is the best way to check if the device has Internet accessStack Overflow
版权声明:本文标题:javascript - What is the best way to check if the device has Internet access? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744388059a2603841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论