How to detect the operating system on the client machine ?

 Posted by Bharathi Cherukuri on 6/18/2012 | Category: JavaScript Interview questions | Views: 6433 | Points: 40
Answer:

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

Example:

// 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:

Your OS: Windows


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Bhupentiwari on: 6/27/2012 | Points: 10
This my code but its not giving proper output

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<title>Untitled Page</title>
<script type ="text/C#" language ="javascript" >

function x()
{
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);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="Button1" runat="server" onclientclick="x();" Text="Button" />

</div>
</form>
</body>
</html>

Posted by: Bhupentiwari on: 6/27/2012 | Points: 10
please reply

Login to post response