vishalg
vishalg

Reputation: 437

html 5 code is not working on android using phonegap

<html>
  <head>
    <title>Cordova Offline Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Call onDeviceReady when Cordova is loaded.
    //
    // At this point, the document has loaded but cordova-1.9.0.js has not.
    // When Cordova is loaded and talking with the native device,
    // it will call the event `deviceready`.
    //

    function Network(){
        if(navigator.onLine)
            {
              alert('You are Online');
            }
        else
            {
              alert('You are Offline')
            }
     }
    </script>
  </head>
  <body >
  <input type="button" value="Check Network" onclick="Network()" />
  </body>
</html>

I am trying to check network connection using html code. It is working if i run this code on crome on my local machine but why it is not working on android.

Upvotes: 1

Views: 3112

Answers (3)

Aamirkhan
Aamirkhan

Reputation: 5784

it there are some phonegap specific events u have to use for some taks like internet connectivity checking issue

here is an example by which u can check internet connectivty

<!DOCTYPE html>

navigator.network.connection.type Example

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
// 
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
    checkConnection();
}

function checkConnection() {
    var networkState = navigator.network.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);
}

</script>

A dialog box will report the network state.

for more detail have a look at this Connection

Upvotes: 2

dhaval
dhaval

Reputation: 7659

navigator.onLine return the online status of the browser, so there are few browsers which provides capabilities to change the status to Work Offline like IE and Firefox, Chrome gives you onLine true as long as there it is connected to any available network

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

To make sure that your device is really connected to public/private network, you should do a simple AJAX call to check whether the target machine is reachable or not.

I followed the above approach in one of my application where I needed to connect to a private VPN network from Device, to make sure the required network is really available and execute the required code based on that.

Upvotes: 0

Padma Kumar
Padma Kumar

Reputation: 20031

//just add this below line at top of your code and now check it.

<!DOCTYPE HTML>

Upvotes: 0

Related Questions