Last active
August 29, 2015 14:19
navigator.onLine
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'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment