I am developing an app that uses an embedded IE 7 browser and I need to verify if the user has an active internet connection using a static HTML page with JavaScript.
Although Offline.js
is a great library, it won't work in this scenario as JavaScript support is limited.
The window.navigator.onLine
property does not exist to check for connectivity status.
Simply using a meta
redirect won't suffice because if there is no internet access, the user will be stuck on the current page. The logic should resemble something like this:
function UserIsOnlineTest(){
// required code goes here
// should return a boolean value
}
if (UserIsOnlineTest()) {
window.location.replace('http://theOnlineSite.com/');
}
Any suggestions or ideas?
After considering @RobM.'s response, I have come up with a complete solution:
(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);
}
}());
Upon further refinement, I realized the need to introduce a delay in error testing. Since this was essentially the only code in the file, it was testing the online variable before the image finished downloading.