How to detect in HTML5/JQueryMobile App if there is internet connection available?

I have basic JQuery Mobile/Javascript HTML5 app. No PhoneGap etc. Just pages in a browser. I want to sent data to a server if I can detect an available connection? How an this be detected? Thanks

Upvotes: 2

Views: 2196

Answers (2)

intuitivepixel
intuitivepixel

Reputation: 23322

Since you are using HTML5, you can check the window.navigator.onLine boolean status.

if (window.navigator.onLine) {
  alert("Online")
} else {
  alert("Offline")
}

You can also listen to the offline/online events see here for reference:

https://developer.mozilla.org/en-US/docs/DOM/window.navigator.onLine

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

You can also use heyoffline.js if you don't mind having another dependence.

Hope it helps

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 31

You can use window.navigator.onLine. It returns true if the device is online, false otherwise. I used it while creating an app in jQuery mobile for iOS and it works.

https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.onLine.

Upvotes: 3

Related Questions