This should work, but I would probably handle it differently. It just sends a query to a page on your server every 15 seconds. If the query fails, then it is probably offline (either you or the server).
function checkOnline(){
$.ajax({
url: "test.php",
}).done(function( html ) {
if(html=='ok')
setTimeout('checkOnline()',15000);
else
alert('Error');
}).fail(function(){
alert('Offline');
});
}
test.php
<?php exit('ok'); ?>
I would personally attach a fail function to my other Ajax or polling queries. If those failed, I would trigger the Offline message and cease further polling or queries until the page was reloaded or after a certain time interval. No sense in just polling the server for the heck of it.