如何知道网络是否连接(断开)?
在 Xul 中,我如何知道网络是否已连接(断开)?
--update
使用:
function observe(aSubject, aTopic, aState) {
if (aTopic == "network:offline-status-changed") {
write("STATUS CHANGED!");
}
}
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
os.addObserver(observe, "network:offline-status-changed", false);
和首选项:
pref("network.manage-offline-status", true);
它不起作用..有一个 错误报告在这里,但我认为这与此无关。
--
实际上我认为不可能收到通知,因为即使在 Firefox 中我们也不会收到通知,并且如果用户希望浏览器知道其处于离线状态,则需要手动标记“离线工作” ..
--
Firefox“about:config”过滤“offline”字符串的屏幕截图,不幸的是,没有“network.manage-offline-status”:
How can I know, in Xul, if the network is (dis)connected?
--update
Using:
function observe(aSubject, aTopic, aState) {
if (aTopic == "network:offline-status-changed") {
write("STATUS CHANGED!");
}
}
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
os.addObserver(observe, "network:offline-status-changed", false);
and the preference:
pref("network.manage-offline-status", true);
it's not working.. There's a bug report here, but I don't think it has something to do with it.
--
Actually I think it's not possible to be notified, as even in Firefox we're never notified, and the user need to manually mark "work offline" if he wants the browser to know that it's offline..
--
Screenshot my of Firefox "about:config" filtering for "offline" string, unfortunately, there no "network.manage-offline-status":
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够使用 navigator.onLine。这是帮助页面
https://developer.mozilla.org/en/Online_and_offline_events
另一个解决方案(如@Neil 评论):
You should be able to use navigator.onLine. Here is the help page
https://developer.mozilla.org/en/Online_and_offline_events
Another solution (as commented by @Neil):
我发现最好的方法是使用下面的 javascript 代码,其行为类似于
ping
,并在一些大网站上进行测试,并假设如果它们都没有响应,那么网络必须断开。(来自 http://crynobone.com/ci/index.php/archive/view /852)
--update
但是,由于这不是一个可靠的解决方案(因为您不能相信该图像将永远存在于网站中),最好的解决方案可能是开发一个新的 XPCom 组件。
The best way I found is to use the following javascript code, that behaves like a
ping
, and make the test with some big websites, and assume that if none of them answers, so the network must be disconnected.(from http://crynobone.com/ci/index.php/archive/view/852)
--update
But, as it's not a reliable solution (as you can't rely that the image will be in the website forever), the best solution might be to develop a new XPCom component.
呃...根据 HTML5(请阅读 echmascript 5),
on-/offline
事件可用。请参阅 Mozilla Hacks
编辑 20/4/2011:< br>
当我观看 MS MIX11 的播客时,我刚刚遇到了这个答案的更新:
http://channel9.msdn.com/Events/MIX/MIX11/HTM14 43:36 左右,讲师实际上正在谈论
window.navigator.onLine
属性,他使用它来检测浏览器(和计算机)是否在线。然后,当他再次上线时,他使用online
事件执行某些操作。但是,此方法仅在现代浏览器中可用。因此 IE 8 及以下版本必须轮询连接。
Eh... as per HTML5 (read echmascript 5), the
on-/offline
events are available.See it here at Mozilla Hacks
Edit 20/4/2011:
I just encountered an update for this answer, when i was watching a podcast from MS MIX11:
http://channel9.msdn.com/Events/MIX/MIX11/HTM14 around time 43:36, the lecturer is actually talking about the
window.navigator.onLine
property, where he uses it for detecting if the browser (and the computer) is online. Then he uses theonline
event to do something when he gets online again.This method is only available in modern browsers, however. So IE 8 and below have to poll for the connection.