6

When an user attempts to log in without Internet connection, I just need to check whether he/she is connected to internet or not.

I tried the following code :

if (status == '404') {
    $scope.error="No internet connection";
    return false;
}

But this status 404 comes even when my Web Service failed to connect. I need to differentiate both, if is the user's internet connection issue or the Web Service connection issue.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
jsduniya
  • 2,464
  • 7
  • 30
  • 45
  • 2
    You can use `navigator.onLine` javascript function or write a service for this, check [this link](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine), since it's just a dirty test to check connection, you can also [mock an AJAX request](http://stackoverflow.com/a/2384227/1431600). – Evandro Silva Jun 09 '14 at 13:33
  • @EvandroSilva: it works some, some time not..if disable network it works, if i disconnect my wired internet connection it won't work. – jsduniya Jun 09 '14 at 13:47

3 Answers3

9

Using navigator.onLine

You can use navigator.onLine and wrap it on a helper variable, like this (Credits to this answer)

myApp.run(function($window, $rootScope) {
      $rootScope.online = navigator.onLine;
      $window.addEventListener("offline", function () {
        $rootScope.$apply(function() {
          $rootScope.online = false;
        });
      }, false);
      $window.addEventListener("online", function () {
        $rootScope.$apply(function() {
          $rootScope.online = true;
        });
      }, false);
});

and then watch it in the controller:

$scope.$watch('online', function(newStatus) { ... });

But that just serves as a dirty check to know if the PC is actually connected to a network, not meaning that the internet is working.

Using a fake AJAX request

You can mock a service that does a fake request to a browser (warning: non-tested code below)

myApp.service('Internet', function($http){
  this.IsOk = function () {
    return $http({ method: 'HEAD', url: '/' + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000) })
    .then(function(response) {
      var status = response.status;
      return status >= 200 && status < 300 || status === 304;
    });
  }
});

And then use something in this context:

myApp.controller('TestController', function(Internet){

  Internet.IsOn().then(function(isok){
    if(isok) {...} else {...}
  });

});

Code for request mocking in this link.

Also note that it will not work using localhost, because the server will work even when disconnected to the internet.

Community
  • 1
  • 1
Evandro Silva
  • 1,392
  • 1
  • 14
  • 29
  • Why those response codes though? 200 through 300 and 304? I'd have thought some 4X codes would also be applicable, eg 404 – SamAko Jul 27 '16 at 10:32
2

Taken from MDN's summary of NavigatorOnLine.onLine.

Browsers implement this property differently.

In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. To learn more, see the HTML5 Rocks article, Working Off the Grid.

In Firefox and Internet Explorer, switching the browser to offline mode sends a false value. All other conditions return a true value.

You can see changes in the network state by listening for the events on window.onOnline and window.onOffline.

You can access that information via window.navigator.onLine, but as the documentation states, it's very inconsistent cross-browser.

You can also listen for changes in status using window.addEventListener as follows:

window.addEventListener("offline", function(e) {alert("offline");})

window.addEventListener("online", function(e) {alert("online");})
Joe
  • 2,596
  • 2
  • 14
  • 11
  • it works some, some time not..if disable network it works, if i disconnect my wired internet connection it won't work. – jsduniya Jun 09 '14 at 13:48
1

There is a JavaScript property, navigator.onLine, it returns a Boolean value that specifies wether the browser is in online mode or offline mode.

The navigator object contains information about the user's browser.

It is supported in all major browsers, so you sholdn't have any problem at all.

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • 1
    If by *supported by all major browsers* you mean [this inconsistant behaviour](http://stackoverflow.com/a/13076434/542251) then... – Liam Jun 09 '14 at 13:39