views:

81

answers:

5

In Javascript or jquery

A: 

You are better off doing feature detection http://www.modernizr.com/

PetersenDidIt
Nice link .....
Hugh
Why the down vote? The statement is true.
PetersenDidIt
For production code I would definitely agree with you. But that's not what the questioner asked. Maybe he is testing some code - maybe he wants to educate himself.
plodder
The guy didn't say much so you have to guess what he is trying to do so wouldn't say this is a bad answer.
PetersenDidIt
+1  A: 

There is a very lightweight version for jQuery available Here:

http://www.stoimen.com/blog/2009/07/16/jquery-browser-and-os-detection-plugin/

Jud Stephenson
Feature detection is better, but if you must detect the OS, the above is lightweight and works.
Jud Stephenson
A: 

You can use:

<html>
<body>

<script type="text/javascript">
document.write("Browser CodeName: " + navigator.appCodeName);
document.write("<br /><br />");
document.write("Browser Name: " + navigator.appName);
document.write("<br /><br />");
document.write("Browser Version: " + navigator.appVersion);
document.write("<br /><br />");
document.write("Cookies Enabled: " + navigator.cookieEnabled);
document.write("<br /><br />");
document.write("Platform: " + navigator.platform);
document.write("<br /><br />");
document.write("User-agent header: " + navigator.userAgent);
</script>

</body>
</html>

ref: http://www.w3schools.com/js/js_browser.asp

Hugh
+1  A: 

You could check the userAgent like this:

return navigator.userAgent.indexOf(\"Mac OS X\") != -1

But, this isn't a reliable method as it can be spoofed...but since there is no javascript absolute for this, not a terrible option. Feature detection is a better alternative if you want to see what the browser will/won't support...depends if you're after metrics of actually enabling/disabling features.

Nick Craver