神奇的是在這裏完成
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/
PS。那麼這個問題就出現了。事情發生了變化,因爲 – FabianCook 2013-05-03 13:00:16