This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var statusElem = document.getElementById('status') | |
setInterval(function () { | |
statusElem.className = navigator.onLine ? 'online' : 'offline'; | |
statusElem.innerHTML = navigator.onLine ? 'online' : 'offline'; | |
}, 250); | |
// better way | |
var addEvent = (function() { | |
if (document.addEventListener) { | |
return function(el, type, fn) { | |
if (el && el.nodeName || el === window) { | |
el.addEventListener(type, fn, false); | |
} else if (el && el.length) { | |
for (var i = 0; i < el.length; i++) { | |
addEvent(el[i], type, fn); | |
} | |
} | |
}; | |
} else { | |
return function(el, type, fn) { | |
if (el && el.nodeName || el === window) { | |
el.attachEvent('on' + type, function() { | |
return fn.call(el, window.event); | |
}); | |
} else if (el && el.length) { | |
for (var i = 0; i < el.length; i++) { | |
addEvent(el[i], type, fn); | |
} | |
} | |
}; | |
} | |
})(); | |
addEvent(window, 'online', function () { | |
document.getElementById('vialistener').innerHTML = 'Online'; | |
}); | |
addEvent(window, 'offline', function () { | |
document.getElementById('vialistener').innerHTML = 'Offline'; | |
}); |
No comments:
Post a Comment