Raj
Raj

Reputation: 193

how to display the alert box if there is no internet connection using Phonegap?

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

Upvotes: 1

Views: 3209

Answers (5)

Sinu Varghese
Sinu Varghese

Reputation: 800

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"); 
}

Upvotes: 1

nida
nida

Reputation: 656

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

Upvotes: 2

sjmach
sjmach

Reputation: 434

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

Upvotes: 1

ghostCoder
ghostCoder

Reputation: 7655

just do

networkState = navigator.network.connection.type; 
if (networkState == Connection.NONE)
{
  alert('No internet connection ');
};

Upvotes: 4

Krishnanunni Jeevan
Krishnanunni Jeevan

Reputation: 1759

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?

Upvotes: 1

Related Questions