Online and Offline Events in HTML5

Hi everyone in this blog I’m explaining about online and offline events in html5.


HTML 5 has one of the best features, which is the creation of offline web applications using HTML 5, in other words if you load your application once in your browser and then you disconnect your web connection and you reload your page then it will still reload.

Offline web application:

Web pages are things that you download to your browser and then the browser renders it. As we know to download anything you need a network connection, but the question is how to download something when you are offline or you do not have a network connection ? the answer is you can't download but after once downloading a web application you can surf the website offline or without an internet connection. This is called an offline web application.  

In this article I am not discussing offline web applications or how to create an offline web application using HTML 5, but In this article I am explaining offline and online events. 

If you want to build a good web application with offline capability or if you want to build a good offline web application, you need to know whether your application is offline or online. 

But why do we need to know that your web application is offline or online? 

Answer:  Suppose you are using a broadband connection at your home and you are surfing an offline web application and suppose you want to submit the data from that web application to be stored into the database server. But suddenly your network connection has been lost and if you submit your data then that data will also be lost, so you can store that data in a Web-SQL Database or anywhere within the browser. To store the data within the browser first of all you need to know whether you are offline or online. When you are online then you need to know that the web application is now online so you can re-synchronize your application with the server. 

Api for online or offline event: 

navigator.onLine has a property to ensure that you are offline or online, navigator.onLine has a true or false value.

1.   If the value is true then the application is online

2.   If the value is false then the application is offline 

The navigator.onLine must return false if the client PC is not connected to the network. 

Online and Offline Events:

The following example tells how can you use offline and online events with your web application.


JavaScript Code:


        window.addEventListener('load', function () {
            var status = document.getElementById('status');
            function Status(event) {
                var condition = window.navigator.onLine ? 'online' : 'offline';
                status.className = condition;
                status.innerHTML = 'You are : ' + condition.toUpperCase();
                log.insertAdjacentHTML('beforeend', 'Event: ' + event.type + '; Status: ' + condition + '<br/>');
            window.addEventListener('online', Status);
            window.addEventListener('offline', Status);





        #status {
            position: fixed;
            width: 100%;
            font: bold 1em sans-serif;
            color: #FFF;
            padding: 0.5em;
        #log {
            padding: 2.5em 0.5em 0.5em;
            font: 1em sans-serif;
        .online {
            background: green;
        .offline {
            background: red;


            <h1>Online and Offline Events</h1>
        <div id='status'></div>
        <div id='log'>Logs<br />



1. Initial when page load:

When page load first time that time I’m online

Online and Offline Events in HTML5


2. When Offline:

Online and Offline Events in HTML5


3. When Online:

Online and Offline Events in HTML5

 in my next post i'll explain about Difference between MVC 3, MVC 4, MVC 5, and MVC 6

Last updated:11/27/2014 6:57:09 AM
Anonymous User

Anonymous User

I am a content writter !


Leave Comment