Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if there is an internet connection

How to check if there is an internet connection in Javascript? I have searched in Google but I have not found a good solution for my problem.

I have already got two eventhandler:

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 in the "onload"-function if I am online?

like image 492
spitzbuaamy Avatar asked Apr 16 '13 11:04

spitzbuaamy


1 Answers

In HTML5, according to the specs:

var online = navigator.onLine;
like image 102
UltraInstinct Avatar answered Oct 03 '22 20:10

UltraInstinct