Reputation: 2441
Checking of internet connection by requesting an image does not work after the image gets cached in the browser. After one or two time, this doesn't work as the image is stored in the browser's cache so is there any solution for that? Or what do I need to check whether a connection is available or not? window.navigator.online
is not reliable. so looking for the other interesting and reliable solution.
Upvotes: 0
Views: 156
Reputation: 2441
The perfect solution for checking internet connectivity is to give a ajax call to your server to the dummy page and make that dummy page's entry in the application cache manifest file in the network section so that it may not get cached in the browser, otherwise it wl take that file from browser and show you as online even if u are in offline mode.
Upvotes: 0
Reputation: 8230
Using XMLHttpRequest object and POST protocol. POST-request does not cached. But this works only if image (or any other resource) loaded from the same domain as page (XMLHttpRequest limitation).
var xhr = new XMLHttpRequest();
xhr.open("POST", "url_for_image_file", true);
xhr.onreadystatechange = function() {
if(xhr.readyState === 4)
if(xhr.status!==200) {
// no image loaded
alert("fail connect to server");
}
else {
alert("connection success");
}
};
xhr.send(null);
Upvotes: 1
Reputation: 207521
Use a cache buster querystring. The querystring forces the browser to check the servver for the new image.
yourImageObj.src = "newImage.png?time=" + new Date().getTime();
Upvotes: 3