navigator.onLine在我的手机不工作。如何检查互联网是在线。在Phonegap [英] navigator.onLine is not working in my mobile.How to check internet is online.offline??? In Phonegap
问题描述
我为我的应用程式使用phonegap。我的应用程序是从一个网站RSS_FEED basicaly。
,但我的要求是当互联网不存在应用程序应该警告
- 离线
。
I am using phonegap for my app. My app is basicaly for RSS_FEED from one website.
but my requirement is when internet is not there application should alert
-offline
.
当应用程序在线运行时,所有数据存储到数据库中。
,当互联网没有那个时间数据从数据库获取,所以我的问题只是应用程序显示我在线每一次,而使用 navigator.onLine
。
when application run on online all data stored into database.
and when internet is not there that time data fetched from DB so my problem is only that application shows me online every time while using of navigator.onLine
.
if(navigator.onLine) {
alert("check online");
loadScript("http://www.google.com/jsapi",msgOnFn)
loadScript("js/googleapi.js", msgOnFn);
} else {
loadScript("js/db.js",msgOffFn);
}
我尝试了这也会显示无或 WiFi 每次。我不知道为什么会发生这种情况。
I tried THIS ALSO In this it shows none or wifi everytime. I am not getting why this happening.
在网络浏览器,它是工作正常。 :)
in web browser,It is working fine. :)
推荐答案
请按照 phonegap documentation 。
您还可以更改给定函数以返回true或false:
You could also change the given function to return true or false:
function checkConnection() {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = false;
states[Connection.ETHERNET] = true;
states[Connection.WIFI] = true;
states[Connection.CELL_2G] = true;
states[Connection.CELL_3G] = true;
states[Connection.CELL_4G] = true;
states[Connection.CELL] = true;
states[Connection.NONE] = false;
if (states[networkState]) {
return true;
} else {
return false;
}
}
这篇关于navigator.onLine在我的手机不工作。如何检查互联网是在线。在Phonegap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!