Alert box when no internet connection - Phonegap

后端 未结 9 2696
南方客
南方客 2021-02-20 06:55

I\'m trying to get a pop-up to, well, pop up when there is no internet connection on the device.

I got the following example working, but now I want the alert only to sh

9条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 07:39

    Old-ish question, but here's how I'd do it - You can use events to detect if the device is online or offline. This is ideal for this situation as the pop-up will appear as soon as the device goes offline:

    document.addEventListener("offline", function(){ alert("You're offline") }, false);

    And to do the same, but when the device regains an internet connection?:

    document.addEventListener("online", function(){ alert("You're online") }, false);

    Check out the events docs here: http://docs.phonegap.com/en/1.8.1/cordova_events_events.md.html#offline

    UPDATE :

    as of cordova 5 these events have been moved to cordova-plugin-network-information

提交回复
热议问题