If youThere are using JavaScript on a website(i.e; or any front2 answers forthis for two different senarios:-end part) The simplest way
If you are using JavaScript on a website(i.e; or any front-end part) The simplest way to do it is:
<h2>The Navigator Object</h2> <p>The onLine property returns true if the browser is online:</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "navigator.onLine is " + navigator.onLine; </script>
But if you're using js on server side(i.e; node etc.), You can determine that the connection is lost by making failed XHR requests.
The standard approach is to doretry the request a few times. If it is:doesn't go through, alert the user to check the connection, and fail gracefully.
<!DOCTYPE html>
<html>
<body>
<h2>The Navigator Object</h2>
<p>The onLine property returns true if the browser is online:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "navigator.onLine is " + navigator.onLine;
</script>
</body>
</html>