0

I want to offer a download of an ".exe" File on my website. Is it possible, to restrict the download only to windows users? Obvious it's useless on android devices. Thx.

  • You can take a look at [this Stack Overflow question](https://stackoverflow.com/questions/38241480/detect-macos-ios-windows-android-and-linux-os-with-js). – frangaren Sep 03 '20 at 11:32
  • this helps : [visit](https://stackoverflow.com/questions/11219582/how-to-detect-my-browser-version-and-operating-system-using-javascript) – Rayees AC Sep 03 '20 at 11:34

1 Answers1

0

The following code shows a download button only if the user is on windows.

<!DOCTYPE HTML>
<html>

<head>
  <title>
    Remove button when os is not windowns
  </title>
</head>

<body style="text-align:center; margin-top: 20px;">

  <button style="display: none;" id="download_btn">
        Download
    </button>

  <script>
    var btn = document.getElementById("download_btn");

    if (navigator.userAgent.indexOf("Win") != -1) {
      btn.style.display = "block";
    }
  </script>
</body>

</html>

For better understanding of how this works check the following snippet:


<!DOCTYPE HTML>
<html>

<head>
  <title>
    OS detection
  </title>
</head>

<body style="text-align:center; margin-top: 20px;">

  <p>Click on the button to know the OS of User's System.</p>
  <button onclick="Check_OS()"> 
        Click Here
    </button>
  <p id="result">
  </p>
  <script>
    var result = document.getElementById("result");
    var Name = "Unknown OS";
    if (navigator.userAgent.indexOf("Win") != -1) Name =
      "Windows OS";
    if (navigator.userAgent.indexOf("Mac") != -1) Name =
      "Macintosh";
    if (navigator.userAgent.indexOf("Linux") != -1) Name =
      "Linux OS";
    if (navigator.userAgent.indexOf("Android") != -1) Name =
      "Android OS";
    if (navigator.userAgent.indexOf("like Mac") != -1) Name =
      "iOS";

    function Check_OS() {
      result.innerHTML = Name;
    }
  </script>
</body>

</html>