javascript - How to check if Firefox OS is connected to the internet or not - Stack Overflow

How do we check if Firefox OS device is connected to the internet or not? I have triednavigator.mozCon

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.

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.

Share Improve this question asked Mar 13, 2014 at 12:50 paolooopaolooo 4,1632 gold badges21 silver badges34 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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

I'm plementing 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 e 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./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.

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/docs/Web/Apps/App_permissions
var request = new window.XMLHttpRequest({mozSystem: true});
request.open('HEAD', 'http://www.mozilla/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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744265875a4565861.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信