I am developing an iphone app on phonegap and I am using jQuery mobile. I want to implement a simple functionality on a page like when press a button on my html page, check if I have internet connection. If I do, then proceed on the next page else, display the error page.
I include the libraries:
<link rel="stylesheet" type="text/css" href="lib/jquery.mobile-1.0b1.min.css" />
<script type="text/javascript" src="lib/jquery-1.6.1.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.1.min.js"></script>
<script type="text/javascript" src="functionality.js"></script>
<script type="text/javascript" src="lib/jquery.mobile-1.0b1.min.js"></script>
my html code of page1.html is:
<a href = "#map_page" id='map_button' data-role="button" onclick = "connection();">Go!</a>
Now the functionality.js file contains the connection() function which is written according to the phonegap’s docs:
function connection(){
alert('checking...');
// check if there is internet connection
navigator.network.isReachable("google.com", isNetworkAvailable, {});
}
function isNetworkAvailable(status) {
var networkState = status.code;
alert('Connection Type ' + networkState + ' - ' + status.message); }
The problem is that I am never getting in the connection() function, not even when I use the following script in my html page:
$(‘#map_button’).click (function(event) {
checkConnection()});
I’ve been working on this piece of code for a few days without being able to make it work. I would really appreciate your help…
I was also having trouble getting checking for a connection but found an alternative method of doing it using var networkState = navigator.network.connection.type; I’ve implemented it quite differently binding a switched function to all my links. As it maybe on interest in the deviceReady method I bind all links to call a function that will decide whether or that link needs connection checking or not. Skip to second function for connection stuff!
This function works just fine for me and you can call it inline on your onclick event if you want. It doesn’t depend on jquery either.
I’m jquery / jquery mobile dependant
Not the most elegant example I know but I hope this helps;