3

im working on a phonegap application i want to test if theres a connection internet display an iframe, else display a message "please check your internet connection". i used a javascript code, it works for me on the navigator, but when i build the application it didn't work for me, my code is like this:

<div id="app"></div>
<script type="text/javascript">
var online = navigator.onLine;
if (online == true) {
  $( "div#app" ).html("<iframe src='#' frameborder=0 width=100%></iframe>");
} else {
  $( "div#app" ).html('NO INTERNET CONNEXION');
} 
</script>
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
hamzanatek
  • 404
  • 3
  • 11
  • Possible duplicate of [navigator.onLine is not working in my mobile.How to check internet is online.offline??? In Phonegap](http://stackoverflow.com/questions/22448591/navigator-online-is-not-working-in-my-mobile-how-to-check-internet-is-online-off) – Sebastian Paaske Tørholm Mar 31 '16 at 16:10
  • 2
    Possible duplicate of [Android 2.3 and Phonegap, navigator.online does not work](http://stackoverflow.com/questions/12478073/android-2-3-and-phonegap-navigator-online-does-not-work) – VirginieLGB Mar 31 '16 at 16:16
  • At least 2 duplicates found... Google is your friend? :p – VirginieLGB Mar 31 '16 at 16:17
  • 1
    Google "cordova-plugin-network-information", there's no need for iframes and GET's – Chris W. Mar 31 '16 at 16:27

1 Answers1

3

There are other ways to check for internet access with cordova/phonegap using plugins, but if you want to check if an image is available or not, then you can always check its size in the cache.

Either of these solutions should steer you forward:

Determining image file size + dimensions via Javascript?

OR other possible answer

https://stackoverflow.com/a/18018330/4278933

If you check for an image, make sure a parameter timestamp is appended to the url... thus... call http://www.myserver.com/img/blank.gif?x=1122334455

This will ensure that your test does not return with a false positive, which would be the case if the image URL was previously cached within the client device.

Community
  • 1
  • 1
  • Then http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-network-information/index.html or http://ngcordova.com/docs/plugins/network/ should help you. –  Mar 31 '16 at 16:28