65.9K
CodeProject is changing. Read more.
Home

Tip to detect Online/Offline Mode

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (12 votes)

Oct 1, 2014

CPOL
viewsIcon

14890

How to detect Online/Offline mode through code

Introduction

In today's development world, we are creating web applications which can run in both modes (i.e. online and offline mode) using HTML5 capabilities.

Below is the sample code to detect current mode using JavaScript:

<script> 
window.addEventListener('load', function (e) { 
}, false); 
window.addEventListener('online', function (e) { 
alert("you are online"); //alert for online status 
}, false); 
window.addEventListener('offline', function (e) { 
alert("you are offline");//alert for offline status 
}, false); 
</script> 

Use the above code and play with online /offline mode. :)