0

I want to find out if the computer is connected to the internet with JavaScript or another language.

The static variable "navigator.onLine" only gives information if the browser is in offline modus or not. I want to find out if there is no connection to the internet. It seems as if the JavaScript library offline.js also only reacts on going in offline modus with the browser and is not reacting on disconnecting to the internet.

The user of my website can print out private and public keys for bitcoin and it is neccessary that the user is offline, because of security reasons. I want to send a warning message if the user is still online. Going only in offline mode with the browser doesnt seem to be secure enough and most users only know how to go offline physically.

I have no clue how to program an if condition in JavaScript that checks if the user is offline or online. As mentioned above the following code doesnt do it.

if(navigator.onLine)
    alert("Please go offline before printing.");  

or with offline.js

if(Offline.state == up)
    alert("Please go offline before printing.");  
  • 2
    possible duplicate of [Check if Internet Connection Exists with Javascript?](http://stackoverflow.com/questions/2384167/check-if-internet-connection-exists-with-javascript) – Qantas 94 Heavy May 30 '15 at 09:41

2 Answers2

0

I don't know Jquery but here is it in js:

if(navigator.onLine){
    alert("Please go offline before printing."); 
}
-1

userOnline = true;

ononline = function(){
userOnline = true;
}

onoffline = function(){
userOnline = false;
}

// on bitcoin related event
if(userOnline){
      alert("Please go offline before printing.");
}
vinayakj
  • 5,591
  • 3
  • 28
  • 48
  • This does not provide an answer to the users question. Logical part is pretty easy. He asks for an event-state like `navigator.onLine` which probably exists. http://www.w3schools.com/jsref/prop_nav_online.asp – lin May 30 '15 at 12:30