Operating System

JavaScript FAQ | Client & Browser Configuration FAQ  

Question: Can I use JavaScript to detect the operating system on the client machine?

Answer: To detect the operating system on the client machine, your script can analyze the value of navigator.appVersion or navigator.userAgent. Below is a simple example of a script that sets the variable OSName to reflect the actual client OS.

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);
On your system, this script yields the following result:

(To get more detailed OS information, your script should perform a more sophisticated analysis of navigator.appVersion or navigator.userAgent, but the idea would be the same.)

Caution! Some clients may spoof their navigator.userAgent string, i.e. substitute another OS name (or any string at all) for the user agent value. Generally, your code should not rely on navigator.userAgent to do anything important.

See also:
• How do I detect the browser name (vendor)?
• How do I detect the browser version?
• How do I get the screen size on the client machine?
• How do I determine the browser window size?
• Is Java enabled in the browser?
• Are cookies enabled in the browser?

Copyright © 1999-2012, JavaScripter.net.