How to check if internet is connected with browser using JS?

0 votes
44 views
Ravi Vishwakarma asked 12 days ago in Web Application by Ravi Vishwakarma

How to check if internet is connected with browser using JS?

1 Answer

1 votes
Ethan Karla answered 10 days ago by Ethan Karla
<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
    <p id='status'></p>
    <script>
        var i = 0;
        setInterval(checkConnection, 1000);
        function checkConnection() {
            if (window.navigator.onLine) {
                document.getElementById('status').innerHTML = 'Connected !!!';
                window.document.body.style.backgroundColor = 'green';
            }
            else {
                document.getElementById('status').innerHTML = 'Disconnected !!!';
                window.document.body.style.backgroundColor = 'red';
            }
        }
    </script>
</body>
</html>