最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to know if the network is (dis)connected? - Stack Overflow

programmeradmin3浏览0评论

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/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":

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/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":

Share Improve this question edited Mar 2, 2011 at 20:11 Student asked Mar 1, 2011 at 20:19 StudentStudent 28.4k70 gold badges165 silver badges267 bronze badges 8
  • Have you tried configuring the preferences to use network manager, and seeing whether that makes Firefox go offline automatically? – Neil Commented Mar 2, 2011 at 19:47
  • @Neil looks like there's no such property, but I'll you check the behavior with the existing "offline" properties.. (see my screenshot) – Student Commented Mar 2, 2011 at 20:11
  • Actually the name of the preference changed recently, but either way it should default to true. I've since noticed the manageOfflineStatus property on nsIIOService2 which you could also check. – Neil Commented Mar 2, 2011 at 20:34
  • @Neil Yes, looks like the answer is in that way.. I found this code, but I'm having no success using it's approach.. Like he does ioService.manageOfflineStatus = false; I did ioService.manageOfflineStatus = true;, but my app is still not able to know if it goes offline.. – Student Commented Mar 2, 2011 at 21:24
  • That looks like the Thunderbird code that ports its old preferences to work with the new nsIIOService2 properties. Not sure that it really relates to your problem. – Neil Commented Mar 2, 2011 at 21:26
 |  Show 3 more ments

3 Answers 3

Reset to default 5

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

https://developer.mozilla/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 mented by @Neil):

Components.classes["@mozilla/observer-service;1"]
    .getService(Components.interfaces.nsIObserverService)
    .addObserver(myF­unction, "network:offline-status-changed", false);

The best way I found is to use the following javascript code, that behaves like a ping, and make the test with some big websites, and assume that if none of them answers, so the network must be disconnected.

var ping = {};
ping = {
    img:null,
    imgPreload:null,
    timer:null,
    init:function() {
        var sess = new Date();
        var nocache = sess.getTime();
        var imguri = ping.img+"?time="+nocache;
        var ping.imgPreload = new Image();
        ping.imgPreload.onload = function() {
            clearTimeout(ping.timer);
            ping.timer = null;
            alert("Domain is available");
        };
        ping.imgPreload.src = imguri;
        ping.timer = setTimeout("ping.fail_to_ping()",60000);
    },
    fail_to_ping:function() {
        clearTimeout(ping.timer);
        ping.timer = null;
        ping.imgPreload = null;
        alert("Ping to domain failed!");
    }
};

(from http://crynobone./ci/index.php/archive/view/852)

--update

But, as it's not a reliable solution (as you can't rely that the image will be in the website forever), the best solution might be to develop a new XPCom ponent.

Eh... as per HTML5 (read echmascript 5), the on-/offline events are available.

See it here at Mozilla Hacks

Edit 20/4/2011:
I just encountered an update for this answer, when i was watching a podcast from MS MIX11:
http://channel9.msdn./Events/MIX/MIX11/HTM14 around time 43:36, the lecturer is actually talking about the window.navigator.onLine property, where he uses it for detecting if the browser (and the puter) is online. Then he uses the online event to do something when he gets online again.

This method is only available in modern browsers, however. So IE 8 and below have to poll for the connection.

发布评论

评论列表(0)

  1. 暂无评论