4

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.

1

2 Answers 2

7

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

5
  • 7
    Note that windows.navigator.onLine has different implementations depending on the browser, and a status of true only indicates connection to a LAN or router, not to a specific resource or the broader internet. You may need to specifically test connection to your desired resource and check for 404 to properly test offline conditions. Oct 13, 2013 at 23:57
  • 1
    You may want to check out Offline.js, an open-source library built for just this purpose.
    – Adam
    Oct 27, 2013 at 22:31
  • I didn't D/L it, I just opened up their simulator. rather than using their checkbox to pretend, I actually pulled my Ethernet cable. In Chrome & MS IE, I go the "you are offline" alert. FF did not show it. See also stackoverflow.com/questions/16242389/… which indicates that FF seems to be non-standard
    – Mawg
    May 17, 2014 at 10:27
  • I just checked the Plunk in the awarded answer : works with Chrome Version 34.0.1847.137 m, MS IE v11.0.x.x, but NOT with FireFox v29.0.1
    – Mawg
    May 17, 2014 at 10:30
  • This fiddle seems not working. Note that the rootScope cannot be injected in factory.
    – benek
    Oct 17, 2014 at 15:52
1
$scope.online = $window.navigator.onLine;

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.