blog

Home / DeveloperSection / Blogs / Online and Offline Events in HTML5

Online and Offline Events in HTML5

Anonymous User4089 27-Nov-2014

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

Description:

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:

 

<script>
        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);
        });
    </script>

 

 

CssCode: 

 

<style>
        #status {
            position: fixed;
            width: 100%;
            font: bold1emsans-serif;
            color: #FFF;
            padding: 0.5em;
        }
 
        #log {
            padding: 2.5em0.5em0.5em;
            font: 1emsans-serif;
        }
 
        .online {
            background: green;
        }
 
        .offline {
            background: red;
        }
    </style>

 

HTMLCode:
<p>
            <h1>Online and Offline Events</h1>
        </p>
        <divid="status"></div>
        <divid="log">Logs<br/>
        </div>

 

Output:

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


Updated 27-Nov-2014
I am a content writter !

Leave Comment

Comments

Liked By