Click here to Skip to main content
15,898,995 members
Articles / Programming Languages / Javascript
Tip/Trick

Tip to detect Online/Offline Mode

Rate me:
Please Sign up or sign in to vote.
4.60/5 (14 votes)
1 Oct 2014CPOL 14.5K   10   5
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:

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. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNo explanation needed. Great! Pin
Brian Stevens6-Oct-14 15:40
Brian Stevens6-Oct-14 15:40 
QuestionHelpful tip Pin
Member 111278443-Oct-14 5:21
Member 111278443-Oct-14 5:21 
GeneralMy vote of 1 Pin
SZeeshanAli3-Oct-14 4:30
SZeeshanAli3-Oct-14 4:30 
GeneralMy vote of 1 Pin
EsseyG3-Oct-14 3:40
professionalEsseyG3-Oct-14 3:40 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)2-Oct-14 5:41
Shemeemsha (ഷെമീംഷ)2-Oct-14 5:41 
Good One

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.