How to detect the internet connection via javascript?

Asked

Viewed 737 times

5

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

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

  • navigator.onLine really works?

  • Create a route from your site to use as ping.

1 answer

6


navigator.onLine works for real; however "online" does not refer to an internet connection, but any connection. If the user is connected to a VPN, but this VPN has no internet - navigator.onLine will return true also;

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

  • @danguilherme! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.