Check offline / online internet connection in HTML 5 using JavaScript Code

    Check offline / online internet connection in HTML 5 using JavaScript code

    This blog helps you determine the internet connection for your web application and identify whether you are online or offline using JavaScript.

      
    This helps the developer to know the status of the user and will perform some accurate messages while they are offline.
     
    You can use many methods to achieve this, but I will show the best of HTML5.
     
    HTML5 contains an event that asks you about the status of the Internet. This is the easiest way,
     
    navigator.onLine
    It is very easy to get internet status. It provides a boolean value, true for online and false for offline.


    <!doctype html><html><head><script>  

            function CheckOnlineStatus(msg) {  

                var status = document.getElementById("status");  

                var condition = navigator.onLine ? "ONLINE" : "OFFLINE";             

                var state = document.getElementById("state");  

                state.innerHTML = condition;             

            }  

            function Pageloaded() {  

                CheckOnlineStatus("load");  

                document.body.addEventListener("offline", function () {  

                    CheckOnlineStatus("offline")  

                }, false);  

                document.body.addEventListener("online", function () {  

                   CheckOnlineStatus("online")  

                }, false);  

            }  

        </script><style>  

            ...</style></head><body onload="Pageloaded()"><div id="status"><p id="state"></p></div></body></html>  

     
    Note: The error is not supported in all browsers and different browsers apply this feature differently. However, this is not a full-fledged solution and may not be accurate in all situations.
     
    You can also use other ways to check if the user has an internet connection by registering events in window.onOnline and window.onOffline.

    Newer post Older post
    ads1

    পরিচিতদেরকে জানাতে শেয়ার করুন

    ডোমেইন হোস্টিং সার্ভিস এই লিংকে চাপ দিন

    ওয়েব ডিজাইন সার্ভিস এই লিংকে চাপ দিন

    গ্রাফিক্স ডিজাইন সার্ভিস এই লিংকে চাপ দিন

    ডোমেইন চেকার এই লিংকে চাপ দিন

    আপনার জন্য আরো কিছু পোস্ট

    RELATED ARTICLES

      ads3