Checking if the browser is online

There is a easy way to check if the browser as internet access or not. In the wireless and mobile world a check if the loaded page has internet access can be very good, a very easy check can save you alot of problem.

The only thing you need to do is

 Javascript |  copy code |? 
1
if(navigator.onLine==true){
2
 alert("Online")
3
}else{
4
 alert("Offline")
5
}

4 Comments.

  1. To be clear, does this mean that the browser is in offline mode or does it mean that has a valid internet connection?

    Could be useful thanks :)

    • Fredrik Norling Fredrik Norling

      I have checked it by loading up a page and removing the wireless connection. And yes it will detect this so it’s not only offline mode in the webbrowser that is detected.

  2. Tense version:

    alert(navigator.onLine ? “Online” : “Offline);

    you don’t actually need a == true since it is boolean already;