Detecting Operating System Through Java Script

To detect the Operating System on the Client Machine, your script should analyze the navigator.appVersion string. Below is a simple example of a script that sets the variable OSName to reflect the actual client OS.paste this script after the <Title> Tag of the <Head> Tag of the Page.

   1: <script language="javascript" type="text/javascript">
   2:  
   3: // This script sets OSName variable as follows:
   4: // "Windows"    for all versions of Windows
   5: // "MacOS"      for all versions of Macintosh OS
   6: // "Linux"      for all versions of Linux
   7: // "UNIX"       for all other UNIX flavors 
   8: // "Unknown OS" indicates failure to detect the OS
   9:  
  10: var OSName="Unknown OS";
  11: if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
  12: if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
  13: if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
  14: if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
  15:  
  16: document.write('Your OS: '+OSName);
  17:  
  18: </script>

On your system, this script yields the following result: Your OS: Windows

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s