Pie*_*ouy 105 javascript jquery offline network-connection jquery-events
我正在尝试使用HTML5在线和离线事件准确地检测浏览器何时脱机.
这是我的代码:
<script>
// FIREFOX
$(window).bind("online", applicationBackOnline);
$(window).bind("offline", applicationOffline);
//IE
window.onload = function() {
document.body.ononline = IeConnectionEvent;
document.body.onoffline = IeConnectionEvent;
}
</script>
Run Code Online (Sandbox Code Playgroud)
当我在Firefox或IE上点击"脱机工作"时,它工作正常,但是当我实际拔掉电线时它会随机工作.
检测此变化的最佳方法是什么?我想避免重复带有超时的ajax调用.
Reb*_*cca 68
浏览器供应商无法就如何定义离线达成一致.某些浏览器具有脱机工作功能,他们认为这与缺乏网络访问是分开的,这与互联网访问不同.整件事情一团糟.某些浏览器供应商在实际网络访问丢失时更新navigator.onLine标志,而其他浏览器供应商则不会.
从规格:
如果用户代理明确脱机(与网络断开连接),则返回false.如果用户代理可能在线,则返回true.
当此属性的值更改时,将触发联机和脱机事件.
该navigator.onLine属性必须返回false,如果用户遵循链接或当脚本请求远程页面(或者知道这种尝试会失败),当用户代理不接触网络,而且必须,否则返回true.
最后,规格说明:
该属性本质上是不可靠的.计算机可以连接到网络而无需访问Internet.
the*_*man 31
主要的浏览器供应商在"离线"意味着什么不同.
Chrome和Safari会自动检测您何时"离线" - 这意味着当您拔下网络电缆时,"在线"事件和属性会自动触发.
火狐(Mozilla的),Opera和IE采取不同的方法,并考虑你的"在线",除非你明确选择"离线模式"在浏览器中 - 即使你没有一个有效的网络连接做的.
有关Firefox/Mozilla行为的有效参数,这些参数在此错误报告的注释中列出:
https://bugzilla.mozilla.org/show_bug.cgi?id=654579
但是,要回答这个问题 - 您不能依赖在线/离线事件/属性来检测是否存在网络连接.
相反,您必须使用其他方法.
此Mozilla Developer文章的"Notes"部分提供了两种替代方法的链接:
https://developer.mozilla.org/en/Online_and_offline_events
"如果API未在浏览器中实现,您可以使用其他信号来检测您是否处于脱机状态,包括侦听AppCache错误事件和XMLHttpRequest响应"
这链接到"侦听AppCache错误事件"方法的示例:
http://www.html5rocks.com/en/mobile/workingoffthegrid/#toc-appcache
...以及"侦听XMLHttpRequest失败"方法的示例:
http://www.html5rocks.com/en/mobile/workingoffthegrid/#toc-xml-http-request
HTH, - 乍得
Len*_*rri 18
今天有一个开源JavaScript库可以完成这项工作:它被称为Offline.js.
自动向用户显示在线/离线指示.
https://github.com/HubSpot/offline
请务必查看完整的自述文件.它包含您可以挂钩的事件.
这是一个测试页面.这很漂亮/顺便说一下有很好的反馈用户界面!:)
Offline.js Simulate UI是一个Offline.js插件,允许您测试页面如何响应不同的连接状态,而无需使用强力方法来禁用实际连接.
tma*_*ini 15
现在适用于所有主要浏览器的最佳方法是以下脚本:
(function () {
var displayOnlineStatus = document.getElementById("online-status"),
isOnline = function () {
displayOnlineStatus.innerHTML = "Online";
displayOnlineStatus.className = "online";
},
isOffline = function () {
displayOnlineStatus.innerHTML = "Offline";
displayOnlineStatus.className = "offline";
};
if (window.addEventListener) {
/*
Works well in Firefox and Opera with the
Work Offline option in the File menu.
Pulling the ethernet cable doesn't seem to trigger it.
Later Google Chrome and Safari seem to trigger it well
*/
window.addEventListener("online", isOnline, false);
window.addEventListener("offline", isOffline, false);
}
else {
/*
Works in IE with the Work Offline option in the
File menu and pulling the ethernet cable
*/
document.body.ononline = isOnline;
document.body.onoffline = isOffline;
}
})();
Run Code Online (Sandbox Code Playgroud)
资料来源:http://robertnyman.com/html5/offline/online-offline-events.html
Epo*_*poc 11
该window.navigator.onLine属性及其相关活动是对某些Web浏览器(目前不可靠尤其是Firefox的桌面)作为@Junto说,所以我写了一个小功能(使用jQuery)能够定期检查网络连接状态,并提高相应的offline和online事件:
// Global variable somewhere in your app to replicate the
// window.navigator.onLine variable (it is not modifiable). It prevents
// the offline and online events to be triggered if the network
// connectivity is not changed
var IS_ONLINE = true;
function checkNetwork() {
$.ajax({
// Empty file in the root of your public vhost
url: '/networkcheck.txt',
// We don't need to fetch the content (I think this can lower
// the server's resources needed to send the HTTP response a bit)
type: 'HEAD',
cache: false, // Needed for HEAD HTTP requests
timeout: 2000, // 2 seconds
success: function() {
if (!IS_ONLINE) { // If we were offline
IS_ONLINE = true; // We are now online
$(window).trigger('online'); // Raise the online event
}
},
error: function(jqXHR) {
if (jqXHR.status == 0 && IS_ONLINE) {
// We were online and there is no more network connection
IS_ONLINE = false; // We are now offline
$(window).trigger('offline'); // Raise the offline event
} else if (jqXHR.status != 0 && !IS_ONLINE) {
// All other errors (404, 500, etc) means that the server responded,
// which means that there are network connectivity
IS_ONLINE = true; // We are now online
$(window).trigger('online'); // Raise the online event
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它:
// Hack to use the checkNetwork() function only on Firefox
// (http://stackoverflow.com/questions/5698810/detect-firefox-browser-with-jquery/9238538#9238538)
// (But it may be too restrictive regarding other browser
// who does not properly support online / offline events)
if (!(window.mozInnerScreenX == null)) {
window.setInterval(checkNetwork, 30000); // Check the network every 30 seconds
}
Run Code Online (Sandbox Code Playgroud)
要收听离线和在线活动(借助jQuery):
$(window).bind('online offline', function(e) {
if (!IS_ONLINE || !window.navigator.onLine) {
alert('We have a situation here');
} else {
alert('Battlestation connected');
}
});
Run Code Online (Sandbox Code Playgroud)
Har*_*ene 11
从最近开始,navigator.onLine在所有主流浏览器上显示相同,因此是可用的.
if (navigator.onLine) {
// do things that need connection
} else {
// do things that don't need connection
}
Run Code Online (Sandbox Code Playgroud)
以正确方式支持此功能的最古老版本包括:Firefox 41,IE 9,Chrome 14和Safari 5.
目前,这几乎代表了所有用户,但您应该始终检查页面用户的功能.
在FF 41之前,它仅显示false用户是否手动将浏览器置于脱机模式.在IE 8中,属性是在body,而不是window.
来源:caniuse
navigator.onLine是一团糟
我在尝试对服务器进行ajax调用时遇到此问题.
客户端脱机时有几种可能的情况:
我使用的解决方案是使用javascript控制自己的状态.我设置了成功呼叫的条件,在任何其他情况下我假设客户端处于脱机状态.像这样的东西:
var offline;
pendingItems.push(item);//add another item for processing
updatePendingInterval = setInterval("tryUpdatePending()",30000);
tryUpdatePending();
function tryUpdatePending() {
offline = setTimeout("$('#offline').show()", 10000);
$.ajax({ data: JSON.stringify({ items: pendingItems }), url: "WebMethods.aspx/UpdatePendingItems", type: "POST", dataType: "json", contentType: "application/json; charset=utf-8",
success: function (msg) {
if ((!msg) || msg.d != "ok")
return;
pending = new Array(); //empty the pending array
$('#offline').hide();
clearTimeout(offline);
clearInterval(updatePendingInterval);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在HTML5中,您可以使用该navigator.onLine属性.看这里:
http://www.w3.org/TR/offline-webapps/#related
可能你当前的行为是随机的,因为javascript只准备好"浏览器"变量然后知道你是否离线和在线,但它实际上并没有检查网络连接.
如果您正在寻找,请告诉我们.
亲切的问候,
| 归档时间: |
|
| 查看次数: |
110810 次 |
| 最近记录: |