魔法在这里完成
Heyoffline.prototype.networkEvents = function(event) {
return addEvent(window, event, this[event]);
};
哪个被调用attachEvents
Heyoffline.prototype.attachEvents = function() {
var event, field, _i, _j, _len, _len1, _ref, _ref1,
_this = this;
_ref = this.elements.fields;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
field = _ref[_i];
this.elementEvents(field);
}
_ref1 = this.events.network;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
event = _ref1[_j];
this.networkEvents(event); //Called here
}
return addEvent(window, 'resize', function() {
return _this.resizeOverlay();
});
};
事件,无论是在线还是离线都传递给这个addEvent
函数
addEvent
定义为:
addEvent = function(element, event, fn, useCapture) {
if (useCapture == null) {
useCapture = false;
}
return element.addEventListener(event, fn, useCapture);
};
所以真的按照我所看到的来使用在线/离线功能,你需要做的就是调用:
//online
window.addEventListener('online', function(){alert('You are online')}, false);
//offline
window.addEventListener('offline', function(){alert('You are online')}, false);
和鲍勃你叔叔
也在这里测试过:http: //jsfiddle.net/W4EKh/
在jsfiddle之后立即出现警报,所以我很确定它是如何工作的......
另外,这里有一些可以帮助的东西:)...
function networkListener(a, b){
var offlineF, onlineF;
if(a instanceof Function && b instanceof Function){
onlineF = a;
offlineF = b;
}else if(a instanceof Function){
if(typeof b === "string"){
if(b === "online"){
onlineF = a;
}else if(b === "offline"){
offlineF = a;
}
}else{
onlineF = a;
}
}
if(typeof onlineF !== "undefined"){
window.addEventListener('online', onlineF, false);
}
if(typeof offlineF !== "undefined"){
window.addEventListener('offline', offlineF, false);
}
}
在这里你可以试试...
http://jsfiddle.net/47N6L/2/