THBBFT THBBFT - 3 months ago 8
Javascript Question

A way to test if an IE7 based browser is online

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;
}
if (online) {
window.location.replace('http://the.site.com/');
}

}());

Answer

If you have an image that you know should be online, you can create an image tag and have onerror indicate that the user is offline:

var googleLogo = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
var image = new Image();
image.src = googleLogo;
image.onerror = function() {
   // user is offline
}
Comments