I have the following listener for checking if the user has an active internet connection:
$(function() {
$( window ).bind(
"online offline",
function( event ){
console.log("-------------------window bind------------------------------");
if(hasInternets()){
console.log("window bind online---------------------------------------------");
sendAllPendingData();
}else {
console.log("window bind offline--------------------------------------------");
}
}
);
});
function hasInternets() {
console.log("hasInternets: " + window.location.href.split("?")[0] + "?" + Math.random());
var s = $.ajax({
type: "HEAD",
url: window.location.href.split("?")[0] + "?" + Math.random(),
async: false
}).status;
console.log("s: " +s);
//thx http://www.louisremi.com/2011/04/22/navigator-online-alternative-serverreachable/
return s >= 200 && s < 300 || s === 304;
}
It works fine in my laptop. I'm not sure if it works fine on other machines and devices. I'm not knowledgeable in this stuff. So will really appreciate it if anyone can advise. And by the way, while I'm doing my research, I stumbled upon this TrueOnline on the jquery plugin, have anyone tried this? http://archive.plugins.jquery.com/project/trueonline
Thanks.