2010-03-04 16:09:30 +01:00
< title > on/offline event capture< / title >
< style >
li {
font-weight: bold;
}
li::before {
font-weight: normal;
}
#viabody::before {
content: 'via body "on" events: ';
}
#viaon::before {
content: 'via window "on" events: ';
}
#vialistener::before {
content: 'via window.addEventListener: ';
}
< / style >
< article >
< p > Test required here: File -> Work Offline - toggle the value and there < em > should< / em > be three list items notifying of online and offline.< / p >
< ul >
< li id = "viabody" > < / li >
< li id = "vialistener" > < / li >
< li id = "viaon" > < / li >
< / ul >
2010-03-04 16:10:20 +01:00
< p > Note that the test shows that < code > window.ononline< / code > and < code > window.onoffline< / code > doesn't work (which I thought I had read in the specs somewhere...).< / p >
2010-03-04 16:09:30 +01:00
< / article >
< script >
function update(online) {
document.getElementById('viabody').innerHTML = navigator.onLine ? 'Online' : 'Offline';
}
window.addEventListener('online', function () {
document.getElementById('vialistener').innerHTML = 'Online';
}, false);
window.addEventListener('offline', function () {
document.getElementById('vialistener').innerHTML = 'Offline';
}, false);
window.ononline = function () {
document.getElementById('viaon').innerHTML = 'Online';
};
window.onoffline = function () {
document.getElementById('viaon').innerHTML = 'Offline';
};
document.body.onOnline = update;
document.body.onOffline = update;
document.body.onload = update;
< / script >