Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect with AngularJS if the app is offline?

We have many customers in remote areas and lost of connectivity is frequent. How can I detect if the javascript app is offline? Meaning cannot reach the server and load the templates.

like image 860
Sebastien Rothlisberger Avatar asked Oct 16 '25 04:10

Sebastien Rothlisberger


2 Answers

I finally found a JSFiddle that does exactly that: Check if the web application is online. It uses window.navigator.onLine.

JSFiddle: http://jsfiddle.net/rommsen/QY8w2/

Source: https://groups.google.com/d/msg/angular/ZncvSVUc9y4/S4jH1e_XgGoJ

like image 137
Sebastien Rothlisberger Avatar answered Oct 17 '25 18:10

Sebastien Rothlisberger


$scope.online = $window.navigator.onLine;

$window.addEventListener("offline", function() {
    $scope.online = false;
});

$window.addEventListener("online", function() {
    $scope.online = true;
});
like image 32
justnajm Avatar answered Oct 17 '25 18:10

justnajm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!