If you are developing html5 offline application leveraging app-cache and finding it hard to detect the true internet availability, read on.
As you may already know window.navigator.onLine is not reliable. I found this handy script but it didn’t work quite well with safari browser on Mac, iPad and iPhone when I had the fallback for unavailable resources. The workaround that worked quite well is the following php script.
<?php
header(‘HTTP/1.0 204 No Content’);
exit;
Save this php script with a name like check.php and the use following modification to the javascript function to check internet availability
function isOnline() {
if(window.navigator.onLine){
var s = $.ajax({
type: “HEAD”,
url: ‘check.php’ + “?” + Math.random(),
async: false
}).status;
// Check if 204 status code is returned
return s === 204;
}
return false;
}
This js script returns true network availability . Hope that will be helpful and somebody won’t have to scratch his head for days.