How to check if Firefox is connected to the Internet or not?

How to check if a Firefox OS device is connected to the Internet or not? I tried navigator.mozConnection, navigator.connection.type, navigator.onLinebut it will not work.

Please let me know. Thank.

+4
source share
4 answers

What Peter suggested is possible, although it is not stylish or does not follow best practices.

I supplement Jason Weathersby's answer with a code example and how this can be achieved.

Browser behavior

This is a very browser dependent implementation, you have to be careful when using these properties. As indicated in MDN,

Chrome Safari, (LAN) , ; . , , , , , ,

, .

Firefox Internet Explorer . .

navigator.onLine, (false/ undefined).

, , online = window.navigator.onLine;

, -

window.addEventListener("offline", function(e) {do something})

window.addEventListener("online", function(e) {do something})

, , . http://www.html5rocks.com/en/mobile/workingoffthegrid/, -

window.applicationCache.addEventListener("error", function(e) { alert("Error fetching manifest: a good chance we are offline"); });

, , , , , . , : ( ), .

+3

Ajax , , , , , , .

+1

To expand on what Peter has already suggested, here's how you can implement a workaround for distributed packaging applications.

From Offline Applications in MDN :

// We'll assume we aren't online.
var online = false;

// Assume we're a packaged app with systemXHR permissions.
// https://developer.mozilla.org/docs/Web/Apps/App_permissions
var request = new window.XMLHttpRequest({mozSystem: true});
request.open('HEAD', 'http://www.mozilla.org/robots.txt', true);
request.timeout = 5750;

request.addEventListener('load', function(event) {
   console.log('We seem to be online!', event);
   online = true;
});

var offlineAlert = function(event) {
   console.log('We are likely offline:', event);
}

I am not sure how to do this for hosted or unprivileged applications. Hope someone else can provide a better solution.

+1
source
if (navigator.onLine) {
//you code if internet is on
}

// Test on my firefox OS, ZTE OPEN

0
source

Source: https://habr.com/ru/post/1531573/


All Articles