0

I need some help with my html code. I need to integrate javascript in it which upon loading checks whether internet is available or not. If active it should perform specific task, else it should show "no internet".

`<html>
 <head>
 </head>
 <body onload="myClickHandler();
        startTime()">
<script language="JavaScript">
        var t;
        document.onclick = myClickHandler;
        function myClickHandler() {
            clearTimeout(t);
            t = setTimeout("location.href='index.html'", 60000);       
    //for refreshing the page in every 60 sec.
        }
</script>
<h1> test </h1>
<script type="text/javascript">
var url = "https://www.google.co.in";
  var img = new Image();
  img.src = url;    
 img.onerror = function()
 {
    // If the server is down, do that.
alert ("no connection");        
}
 img.onload = function()
{
    // If the server is up, do this.
 //some task to perform.
  return true;  
}
</script>
</body>
</html>`    

This is what i code, but page is keep on loading after the performance also we need to manually stop the loading.

  • What research have you done so far? Show us your code and error/problem you get. – Jun Rikson Aug 18 '17 at 06:34
  • Look at this solution it may help you out: https://stackoverflow.com/questions/2384167/check-if-internet-connection-exists-with-javascript – Ashish sah Aug 18 '17 at 06:34

1 Answers1

0

Use the below code. Thanks to this post .

To know more about navigator.online plz check the below link

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

<!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>
yasarui
  • 6,209
  • 8
  • 41
  • 75