How to detect the connection to the internet via javascript? [duplicate]

5

Is there any way to "know", via javascript , if the internet connection has been lost?

For example, to attempt a reconnection with websocket, if it detects that the internet connection has been lost and then reestablished.

    
asked by anonymous 03.08.2015 / 15:59

1 answer

6

navigator.onLine works really; However "onLine" does not refer to an internet connection, but a connection whatsoever. If the user is connected to a VPN, but this VPN does not have internet - navigator.onLine will return true also;

The best solution is to make a request of GET for ajax to any website and if this link does not fail you know that the user (most likely) is online.

    
04.08.2015 / 13:24