Take the 2-minute tour ×

Possible Duplicate:
JavaScript: How to detect that the Internet connection is offline?

In facebook, when you loose the internet connection, you see a message "No internet connection. Try again?" and you are changed from online to offline. I want that in my website. How can I do that?

share|improve this question

marked as duplicate by Michael Mior, badcat, hakre, abbot, Ben D Dec 30 '12 at 17:45


Send a ping to the server. If no response, then they are offline and alert them using Javascript. –  Tim Withers Dec 30 '12 at 4:40
As of HTML5, you can use window.navigator.onLine, though not fully compatible –  Jeremy Dec 30 '12 at 4:42
add comment

2 Answers

up vote 3 down vote accepted

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(){
        url: "test.php",
    }).done(function( html ) {


<?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.

share|improve this answer
Thanks . what about changing the user from online to offline in my MySql database. Is it possible. How does facebook do that? –  user1763032 Dec 30 '12 at 6:08
add comment

Run a periodical ajax request to your server. If the request fails, the ajax engine would call a "fail" callback.

But in real life, you would probably attach a "fail" javascript callback to all requests to server side, and it would fire upon a failed request.

share|improve this answer
add comment
