Question

How do we check if Firefox OS device is connected to the internet or not? I have tried navigator.mozConnection, navigator.connection.type, navigator.onLine but it doesn't work.

Please let me know. Thanks.

Was it helpful?

Solution

What Peter suggested is possible, although it isn't classy or follows best practices.

I'm complementing Jason Weathersby answer by providing some code example and how this can be achieved.

Browser Behavior

This is a very browser-dependant implementation, you should be cautious by using those properties. As outlined in MDN,

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

Which means that you can be connected to a LAN without internet access and still have it returning true.

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

If the browser doesn't support navigator.onLine the above example will always come out as falsy (false/undefined).

Code Sample

To check if the you have connection, try online = window.navigator.onLine;

If you want to listen on connection changes, you should do something like

When connection is turned off

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

When connection is back on

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

Other suggestions

Those solutions can be unreliable, so there are a few fallbacks on this if you want to be on safe grounds here. As http://www.html5rocks.com/en/mobile/workingoffthegrid/ shows, you can try doing something like

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

The reason why this works, is because it always makes an attempt to request the manifest to check to see if it needs to update its list of assets. If that requests fails it is normally one of two things, the manifest file is no longer being used (that is, it is not hosted) or the system is unable to access the network to fetch the file.

OTHER TIPS

You can make an Ajax request and see if it goes through, repeat a few times, if nothing happens, then you can deduce that there is no internet connection and do whatever you want from there.

To expand on what Peter has already suggested, here is how you can implement the workaround for privilleged packaged apps.

From Offline Apps on 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 non-privileged apps. Hopefully someone else can provide a better solution.

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

//Test in my firefox OS, ZTE OPEN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top