Search code examples
javascriptandroidhtml.htaccess

How do I hide the content of the site only for Android devices?


Is there any way to hide the site only for android devices?
Maybe there is an option to redirect android users to a separate page?
Thanks.


Solution

  • You can firstly check the user agent to distinguish Android devices.

     const userAgent = navigator.userAgent || navigator.vendor || window.opera;
    

    If is an Android device do the redirect

     if (/android/i.test(userAgent)) {
       window.location.href = "http://www.google.com";
     }
    

    Wrap all the code in the page load function

    window.onload = function() {
       const userAgent = navigator.userAgent || navigator.vendor || window.opera;
       if (/android/i.test(userAgent)) {
         window.location.href = "http://www.google.com";
       }
    };