0

I am able to extract the user`s OS using JavaScript code:

if (navigator.appVersion.indexOf("Win") != -1)

But i am not able to block users of other OSs from accessing the site.

Will Taylor
  • 1,650
  • 9
  • 23
  • 3
    First question that comes to mind is: Why? – Baruch Apr 14 '20 at 10:23
  • Well actually i need to create a website only for windows users , there is no reason for why....... – Smalldeveloper Apr 14 '20 at 10:25
  • @Smalldeveloper have you tried using `navigator.userAgent`? – Sebastian Kaczmarek Apr 14 '20 at 10:26
  • @SebastianKaczmarek yes , well actually i am able to receive the user os but my problem is how to restric website for other users rather than windows – Smalldeveloper Apr 14 '20 at 10:29
  • 1
    I wouldn’t get fixated on this, I can just turn JavaScript off or fake the version. – Zoe Edwards Apr 14 '20 at 10:31
  • Does this answer your question? [Best way to detect Mac OS X or Windows computers with JavaScript or jQuery](https://stackoverflow.com/questions/10527983/best-way-to-detect-mac-os-x-or-windows-computers-with-javascript-or-jquery) – Mark Schultheiss Apr 14 '20 at 10:33
  • @Smalldeveloper It depends on the desired behavior. You can either achieve this on the Apache/Nginx/whatever www server (based on the User-Agent) or using JavaScript - just add a simple `if` statement and if that's not Windows user then redirect him elsewhere (`location = "..."`) – Sebastian Kaczmarek Apr 14 '20 at 10:33
  • or https://stackoverflow.com/q/9514179/125981 – Mark Schultheiss Apr 14 '20 at 10:34
  • or https://stackoverflow.com/q/11219582/125981 – Mark Schultheiss Apr 14 '20 at 10:34
  • @ThomasEdwards there is no problem regarding fake the version or turn off javascript but for now i need solution is this possible ?? – Smalldeveloper Apr 14 '20 at 10:34
  • sorry for my english but My Question i want to restric website for users rather that windows , (Not To Find The User Agent) – Smalldeveloper Apr 14 '20 at 10:37
  • 1
    Hello @Smalldeveloper, you will not be able to use client side code to effectively block access. At most you will be able to show a message and redirect, but that's silly. Only the server can really deny access. – Salketer Apr 14 '20 at 10:46

1 Answers1

1

It is a strange requirement, and I don't know what you mean by block - you could either redirect the user, or perhaps display some kind of message explaining that the user's OS is not supported.

As you said, you are able to detect the OS and so could effectively block the user from accessing the content of your website by updating the HTML. Eg.

if (navigator.appVersion.indexOf("Win") !== -1) {
    const body = document.getElementsByTagName("body")[0];
    body.innerHTML = `
      <div>Your operating system is not supported.</div>
    `;
}
Will Taylor
  • 1,650
  • 9
  • 23