0

I am well aware of how to check if the Google API has fully loaded.

I need to check whether the google API is still available at runtime. The scenario:

  • Application is started with internet conncetion
  • Google API is loaded
  • Application loses internet connection (API still loaded)
  • Application tries to create a Google Map

At this point my application will crash. How can I check if the Google services are still reachable?

Community
  • 1
  • 1
Anonymoose
  • 2,389
  • 6
  • 36
  • 69

2 Answers2

0

EDIT

Add event listeners to the online and offline events on the window object. Do not set window.ononline or window.onoffline to functions directly

Original Answer

window.onoffline and window.ononline events would come in handy here.

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

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • Thank you. I am already using these (or at least the cordova variants) but they don't always fire. Like when UTP cable is unplugged or tablet is disconnected from docking station. Therefor I'd really need a check right before I try to initialize the google Maps – Anonymoose Dec 12 '13 at 15:00
0

use navigator.onLine

Note - do not use window.ononline and window.onoffline, as detailed in the above answer - that page specifically says not to use those properties for compatibility reasons.

source: http://www.w3.org/TR/offline-webapps/#related

matt
  • 9,113
  • 3
  • 44
  • 46
  • Sorry if my answer was confusing, however that page does not say do not use it, it just says to make sure you use them as events (and do not set their values to functions directly). The page I linked to contains cross-browser compatible code that works exactly as the OP would require. – Adam Jenkins Dec 12 '13 at 15:09
  • It says it right here: "(Note: using window.ononline or window.onoffline will not work for compatibility reasons.)". You should *not* be using `window.ononline` and `window.onoffline` - at best, you should use either `document.ononline/onoffline` or `document.body.ononline/onoffline`. – matt Dec 12 '13 at 15:14
  • You obviously are misunderstanding - it shows right there in the working code sample that you use it by using events, and not `DOM0`-style by assigning these properties a function. Here's the code they show on the page (and state you should use): `window.addEventListener('online', updateOnlineStatus); window.addEventListener('offline', updateOnlineStatus);` – Adam Jenkins Dec 12 '13 at 15:23
  • 1
    I see what you're saying - I think I misunderstood an earlier version of your original post. My point is that you should not be setting `window.ononline/onoffline` directly. Adding event listeners to the `window` object for the `online` and `offline` events is definitely acceptable. – matt Dec 12 '13 at 15:39