1
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]);

                }

Here I want to display the alert if only the internet connection is not avialable?i am using if statment if(statusValue == 'none'){ alert } But here not working in my mobile app.Here i am using Phonegap-1.3.0.js

Raj
  • 193
  • 1
  • 3
  • 13

5 Answers5

4

just do

networkState = navigator.network.connection.type; 
if (networkState == Connection.NONE)
{
  alert('No internet connection ');
};
Ali Ben Messaoud
  • 11,690
  • 8
  • 54
  • 87
ghostCoder
  • 1
  • 9
  • 49
  • 72
  • 1
    its not working ya...i didnt get the alert box if i turn off the internet – Raj Apr 10 '12 at 05:25
  • 3
    If you want to get notified when the device goes offline then you want to register for the "offline" event. http://docs.phonegap.com/en/1.3.0/phonegap_events_events.md.html#offline – Simon MacDonald Apr 10 '12 at 15:49
2

Try this:

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


    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[network]);
    return states[network];
}
     if (states[network] == 'No network connection')
{
    alert('Connection type: ' + states[network]);
}
nida
  • 656
  • 3
  • 16
  • 38
1

you can also use navigator.network.isReachable to ping some sites like google with almost 100% uptime.Between is it not wokring in all mobile OS or some speciifc versions of Android?

Krishnanunni Jeevan
  • 1,719
  • 1
  • 15
  • 24
  • 1
    i am using android 4.0 version...navigator.network.isReachable is now working in higher version.i tried – Raj Apr 10 '12 at 04:58
1

This should help you :

// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
document.addEventListener("offline", whenOffline, false);
return false;
}

function whenOffline() {
navigator.notification.alert(
'Sorry your internet connection is not working, please enable it !', // message
alertDismissed, // callback
'Settings', // title
'Done' // buttonName
);
return false;
}
sjmach
  • 426
  • 6
  • 15
1

Try the below code.

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';

if ((states[networkState]) == states[Connection.NONE])
{
alert("Please check your internet connectivity and try again"); 
}
Sinu Varghese
  • 800
  • 1
  • 14
  • 39