jQuery/Javascript to detect OS without a plugin?

Solution 1:

Try:

var os = navigator.platform;

Then handle the os variable accordingly for your result.

You can also loop through each object of the navigator object to help get you more familiarized with the objects:

<script type="text/javascript">
for(var i in navigator){
    console.log(i+"="+navigator[i]+'<br>');
}
</script>

Solution 2:

Plain JavaScript might be all you need.

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);

As Nick suggested you could use navigator.platform as well.

Solution 3:

As far as I know the platform is the less spoofed property on the navigator Object. You can use this to get booleans.

var isMac = navigator.platform.toUpperCase().indexOf('MAC')!==-1;
var isWindows = navigator.platform.toUpperCase().indexOf('WIN')!==-1;
var isLinux = navigator.platform.toUpperCase().indexOf('LINUX')!==-1;

If you need to differentiate Macs between the old PowerPc and new Intel.

var isMacPpc=navigator.platform==="MacPPC";
var isMacIntel=navigator.platform==="MacIntel";

https://developer.mozilla.org/en/DOM/window.navigator.platform

Solution 4:

Try:

alert(navigator.appVersion);

That should give you a string that you can parse for the OS.

Solution 5:

<script>

osName = 'Unknown';

function nav(x, y, z) {
    z = z || y;
    if (navigator[x] && navigator[x].indexOf(y) !== -1) {
        osName = z;
    }
}

/*   navigator     value     download  */
nav( "appVersion", "X11",    "UNIX"    );
nav( "appVersion", "Mac",    "MacOS"   );
nav( "appVersion", "Linux"             );
nav( "userAgent",  "Linux"             );
nav( "platform",   "Linux"             );
nav( "appVersion", "Win",    "Windows" );
nav( "userAgent",  "Windows"           );
nav( "platform",   "Win",    "Windows" );
nav( "oscpu",      "Windows"           );

document.getElementById("download"+osName).className = "knownOS";

</script>

Make sure the right download link is easy to find, but without hiding the other OS links. The people might still want those.

<style>

#downloadUNIX, #downloadMacOS, #downloadLinux, #downloadWindows {
    color:#6D94F2;
    line-height:35px;
    margin:24px 0 24px 0;
    padding:10px;
}
.knownOS {
    background-color:#F7ECAD !important;
    border:2px solid #E8913A;
    color:#133CC4 !important;
    font-weight:bold;
}

</style>

And some html

<ul>
    <li><a id="downloadUNIX"    href="unix Link Here"   >Download Napster-9000 for UNIX</a></li>
    <li><a id="downloadWindows" href="windows Link Here">Download Napster-9000 for Windows</a></li>
    <li><a id="downloadMacOS"   href="mac os link here" >Download Napster-9000 for OS X</a></li>
    <li><a id="downloadLinux"   href="linux Link Here"  >Download Napster-9000 for Linux</a></li>
</ul>

Now the user may disable or block javascripts if he wants. The links will still be there, as opposed to writing the links with Javascript, which requires javascript to work.

Here is a fiddle

http://jsfiddle.net/7fmJb/