How to check network coneection

if(window.Connection) {
if(navigator.connection.type == Connection.NONE) {
title: “Internet Disconnected”,
content: “The internet is disconnected on your device.”
.then(function(result) {
if(!result) {

i have used this code.But this is not working.

For this functionality you have to add the network-connection plugin

ionic plugin add

If you want to listen to the offline event, you can do the following:

document.addEventListener("offline", myCallbackFunction, false);

in every controller i need to add this?

still not working can you please tell to use it with example code.

As the documenation sais, you can add this code in your function:

document.addEventListener("offline", onOffline, false);

function onOffline() {
    // Handle the offline event
    alert('you are offline');

this is not working i have added this code to still not working.

I have installed the plugin to my app and everything is working fine.

This is my code:

$ionicPlatform.ready(function() {

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

	    console.log('Connection type: ' + states[networkState]);
	}, 5000)

	document.addEventListener("offline", onOffline, false);

	function onOffline() {
	   // Handle the offline event
	   alert('you are offline');

I disabled wifi, then 4g and then comes the alert. With chrome://inspect I get the changes listed from the interval output.

1 Like

plugin is installed . then i copy your code still it is not giving any alert.

1 Like

Do you get any errors? Do you use emulator, or device (which device)? Did you disable any network connection?

there is no error.I am using samsung device.i enable wifi and then disable it it does not give any alert.

what samsung do u have? and wich android version is installed on it? I have tested the code on my Note 4 with android 4.4.4 on it

android 4.2.2 samsung s4 mini

I have tested my code now on Galaxy Note 1 (4.1.2) and S3 Mini (4.3). Both are alerting when I disable network connection.

Maybe you should remove and add android platrom again with ionic.

Some times with cordova plugins, you need to make sure you build the project again after adding the plugin, or else the native project won’t know of the new code.

If that doesn’t work, as @bwasnie1 has suggested, removing/adding the platform should do the trick.


Dont try to reinvent the wheel. Drifty has a project for you out there: ngCordova.

Just install ngCordova in your project and use the $cordovaNetwork Angular service to check network status or the online and offline events being broadcasted on the $rootScope:

$rootScope.$on('$cordovaNetwork:online', function() {});
$rootScope.$on('$cordovaNetwork:offline', function() {});

Works like a charm.


By the way, if you consider your app will be ran in a browser for testing purpose first, you must get know, that Chrome provides such thing with its navigator object like onLine = !$window.navigator || $window.navigator.onLine


He is just using it wrong, internet should be disabled before app is launched.

uh… nope.
can disable afrter it’s launched and it detects network is off. tried it multiple time.

It can, but not in this example, because he is probably adding network check code to app.js that is run only once on launch.

May be need to use