Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if the network is (dis)connected?

How can I know, in Xul, if the network is (dis)connected?

--update

Using:

    function observe(aSubject, aTopic, aState) {
        if (aTopic == "network:offline-status-changed") {
            write("STATUS CHANGED!");
        }
    }
    var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
    os.addObserver(observe, "network:offline-status-changed", false);

and the preference:

pref("network.manage-offline-status", true);

it's not working.. There's a bug report here, but I don't think it has something to do with it.

--

Actually I think it's not possible to be notified, as even in Firefox we're never notified, and the user need to manually mark "work offline" if he wants the browser to know that it's offline..

--

Screenshot my of Firefox "about:config" filtering for "offline" string, unfortunately, there no "network.manage-offline-status":

enter image description here

like image 563
The Student Avatar asked Mar 01 '11 20:03

The Student


1 Answers

You should be able to use navigator.onLine. Here is the help page

https://developer.mozilla.org/en/Online_and_offline_events

navigator.onLine is a property that maintains a true/false value (true for online, false for offline). This property is updated whenever the user switches into "Offline Mode" by selecting the corresponding menu item (File -> Work Offline in Firefox).

Another solution (as commented by @Neil):

Components.classes["@mozilla.org/observer-service;1"]
    .getService(Components.interfaces.nsIObserverService)
    .addObserver(myF­unction, "network:offline-status-changed", false);
like image 66
Amir Raminfar Avatar answered Oct 21 '22 18:10

Amir Raminfar