Check if there is an internet connection

How to check for internet connection in javascript? I searched on google, but I did not find a good solution for my problem.

I already have two event handlers:

document.body.addEventListener("offline", function () { //alert("offline") masterViewModel.online(false) }, false); document.body.addEventListener("online", function () { //alert("online") masterViewModel.online(true); }, false); 

But how to check the "onload" function if I'm online?

+4
source share
4 answers

In HTML5 according to specifications :

 var online = navigator.onLine; 
+16
source

Hi You can do this:

 var connectionMessage = "internet connection"; var noConnectionMessage = "No internet connection."; window.onload = checkInternetConnection; function checkInternetConnection() { var isOnLine = navigator.onLine; if (isOnLine) { alert(connectionMessage); } else { alert(noConnectionMessage); } } 
+3
source

var online = navigator.onLine; not working every time. Sometimes your connection is turned on, but you cannot retrieve data from the Internet or not access the site, even if you are online ... so the online return time is true because you are online. At that time, "navigator.onLine" is not usable.

β€œYou can receive false positives, for example, when virtualization software with virtual Ethernet adapters that are alwaysβ€œ connected ”is running on the computer. Therefore, if you really want to determine the online status of the browser, you must develop additional tools for checks. To learn more, see the HTML5 Rocks Article, " check this article

you should use ajax request to check ....

0
source

Why not just make an ajax call to a known address, say StackOverflow. If you do not get 404, you are online.

0
source

All Articles