Computer >> Computer tutorials >  >> Programming >> Javascript

How to find operating system in the client machine using JavaScript?


To detect the operating system on the client machine, your script can analyze the value of navigator.appVersion or navigator.userAgent.

Let us look at detecting the client OS using a simple script −

Example

var operatingSystem = "Unknown OS";
// Check For Windows
if (navigator.appVersion.indexOf("Win") !=- 1) operatingSystem = "Windows";
// Check for Mac
if (navigator.appVersion.indexOf("Mac") !=- 1) operatingSystem = "MacOS";
// Check for UNIX
if (navigator.appVersion.indexOf("X11") !=- 1) operatingSystem = "UNIX";
// Check for Linux
if (navigator.appVersion.indexOf("Linux") !=- 1) operatingSystem = "Linux";
console.log('Your OS: ' + operatingSystem);

Output

If you have a windows system, it'll give the output:

Your OS: Windows

This can also be used to find the browser of the user.