We can check the internet connection so that if there is no internet in between we can let user know that no internet connection.
Here in below example we call the checkInternetConnection() function periodically. So that we can detect the internet connection on web page.
1
2
3
4
5
6
7
|
<script>
function checkInternetConnection() {
var status = navigator.onLine;
document.write(status);
}
setInterval(checkInternetConnection, 2000);
</script>
|
Here we are checking internet connection by navigator.onLine. It returns true if there is internet connection and returns false if there is no internet connection.