If you want your visitors to redirect to any specific page based on "Internet On" or "Internet Off", then this article is for you.


Your audience will visit the first page, for example "index.html" or "www.example.com" where you will use the code below.



<html>
<head>
<title>
Page Title</title>
</head>
<body>

<script>
window.onload = checkInternetConnection;
function checkInternetConnection() {
  var isOnLine = navigator.onLine;
   if (isOnLine) {
      window.location.replace('online.html');
   } else {
     window.location.replace('offline.html');
   }
}

</script>

</body>
</html>

 

 

How it works?

  • If internet works, then index.html will relocate audience to online.html or any other URL, like https://www.google.com
  • If internet doe not work, then index.html will relocate audience to offline.html or any other URL, like https://www.facebook.com

 

 

Where to use the Code?

  1. In Android WebView, you can use this Code. App will open index.html and if there is Internet, it will relocate users to www.example.com, but if there is No Internet, users will be relocated to offline.html page. Although you can do it in Java, but this is a HTML level alternative solution.
  2. You may use this special code in any of your live website, and next time if there is no internet, then audience will be relocated to any pre-downloaded Offline page. It means you will have to add some extra code in the website that browser can download and cache a offline.html page earlier while Internet is On.