user2021740
user2021740

Reputation: 174

Cannot read property 'connection' of undefined. in Cordova 3.2.0

I could not able to check connection state. I had successfully add plugin org.apache.facebook.networkinformation. But still I got error.

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

     document.addEventListener("deviceready", onDeviceReady(), false);


     function onDeviceReady() {
         alert(navigator.network.connection.type);
         alert(1);
     }


     function checkConnection() {

         alert(navigator.network.connection.type);
         var networkConnectionType = 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[networkConnectionType]);
     }

</script>

Upvotes: 0

Views: 2018

Answers (1)

Sithys
Sithys

Reputation: 3803

I just checked your code and i think there is sth broken. Try this script and tell me, if it worked for you!

function checkConnection() {
    var networkState = navigator.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.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

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

checkConnection();

and in your HTML-Code you can put a button like ->

<input type="button" onclick="checkConnection();">

Edited because of the users comment

 document.addEventListener("deviceready", onDeviceReady, false);


 function onDeviceReady {
    var networkState = navigator.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.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

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

Upvotes: 1

Related Questions