I have an app with an embedded IE 7 browser. I need to test in a static HTML page, javascript is okay, whether the user has a connection to the Internet.
Offline.js
though an excellent library will not work here because the javascript is not supported.
The window.navigator.onLine
object.property does not exist.
Simply using a meta
redirect is not an option because if the internet is not accessible I will be leaving the user on the current page. The logic should look something like this:
function UserIsOnlineTest(){
// needed code goes here
// boolean return value
}
if (UserIsOnlineTest()) {
window.location.replace('http://theOnlineSite.com/');
}
Thoughts?
The complete solution I ended up with (based on @RobM. answer)
(function(){
var testImage= 'http://the.site.com/testimage.png';
var image = new Image();
var online = true;
image.src = testImage;
image.onerror = function() {
online = false;
}
setTimeout(function() {
if (online) {
window.location.replace('http://the.site.com/');
},1000);
}
}());
Even more finalized, I needed to add a delay in the test for the error. Since this was the practically the only code in the file it was actually testing the online variable before the image was downloaded.