views:

112

answers:

5

(Don't care about the version. IE or not IE.)

+9  A: 

http://api.jquery.com/jQuery.browser/

isIE = $.browser.msie;
powtac
+1 for jquery - save yourself the hassle of cross-browser support!
AdaTheDev
I added a post to give more info, but this is really the best answer.
Joe Mills
A User Agent string could easily be forged, at least in Opera and Safari.
Marcel Korpel
@Marcel - RE: forged user agents - but that's just the nature of HTTP; there's no way round that.
monojohnny
Though as the jQuery docs point out, in most cases, you want feature detection, not browser detection. Unless, of course, you're just using browser detection to remind people to upgrade to a good browser.
Bob Aman
@monojohnny: in this case, there is, see my solution.
Marcel Korpel
A: 
<html>
<head>
</head>
<body>
<script type="text/javascript">
    alert(navigator.appName);
</script>
</body>
</html>

This replies back with 'netscape' (for Firefox) or 'Microsoft Internet Explorer' (when I tried IE7 - not tried other platforms)

See the link below for more information , for instance.

http://www.javascriptkit.com/javatutors/navigator.shtml

And here's the same sort of thing with an 'if'/'else' structure.

<html>
<head>
</head>
<body>
<script type="text/javascript">
    if (navigator.appName=="Microsoft Internet Explorer") {
        alert("This is IE!");
    }
    else {
        alert("This is not IE!");
    }
</script>
</body>
</html>

Actually you should probably opt for the other post : stick to a well known library like jquery to sort out all the edge cases.

monojohnny
I don't think you want to send up an alert window.
Jeremy Petzold
@Jeremy: I don't think that was the point.Using alert lets the OP try it and see what appName's value is in different browsers. Then he can use that variable in real code.
Nicolás
Yeah, the alert was just an illustration - but I have now included an 'if'/'else' for clarity.I think the OP actually wants jquery here in any case. (I hadn't noticed the tag originally).
monojohnny
A: 

Are you using any particular language?

Jeremy Petzold
Just a heads up for future - this should be a comment really, instead of an answer
AdaTheDev
The above are all JavaScript (or a framework).
Marcel Korpel
The question was tagged JavaScript.
Dean Burge
+1  A: 

While this isn't the "simplest" way, here is a really good page to help with browser detection.

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

Yay for the W3C.

Joe Mills
+1 for w3schools - one of the web's true wonders !
monojohnny
Hmm, link doesn't work, at the moment...
Marcel Korpel
@Marcel - works for me - you sure the site didn't prevent you viewing the page, because you have a forged User-Agent string ?? :-)
monojohnny
No, LOL. Not in Firefox, Chrome nor Opera. But perhaps it's because I'm using Windows at the moment and not Linux, as I'm used to be. ;) (no flame war intended)
Marcel Korpel
+10  A: 

I wouldn't do it via browser sniffing (i.e. either directly or via a Javascript framework), because a User Agent string could easily be forged (and, in these cases, is depending on JavaScript, which could be turned off).

In this case (IE or not), I would use conditional comments in your HTML. They'll always work, whether JavaScript is enabled or not.

Marcel Korpel
Never knew about these - interesting +1. Its true that an agent string can be forged (in fact Opera used to have an option to masquerade as IE): but the main point of broswer sniffing is to provide what is expected to work on the user's browser: if they lie about what they are, then its their lookout - I totally agree with your point about javascript - probably the best route (though still not perfect) is to check at the server-end. [which I think these conditional comments are ?]
monojohnny
But there is no big need to difference between browsers if you dont want to use JavaScript. For CSS you would use Css browser depended hacks.
powtac
@powtac - Assuming that most people use 'modern' browsers, then I would agree with you, but to be pendantic , older browsers also had different ideas about what HTML should be rendered (or even had their special tags '<marquee>' for instance).
monojohnny
@monojohnny: no, conditional comments are at the client side, in your HTML. I think it's better to do feature detection, like http://www.nczonline.net/blog/2009/12/29/feature-detection-is-not-browser-detection/ describes. This way you're always sure a wanted function exists.
Marcel Korpel
@powtac: I prefer browser detection via conditional comments over CSS hacks ("* html" hack, etc.). You'll never know if future browsers (future versions of IE?) will also interpret these rules. With conditional comments you're sure.
Marcel Korpel
You can use conditional comments to include a different `<body class="something">` tag on IE vs other browsers. Then you can use rules like `body.ie6 .something { ... }` for CSS fixes without hacks.
bobince
@monojohnny for this "old browser" problems jQuery offers `jQuery.support` as replacement of jQuery.browser. For example `jQuery.support.boxModel`: Is equal to true if the page is rendering according to the W3C CSS Box Model (is currently false in IE 6 and 7 when they are in Quirks Mode). This property is null until document ready occurs.
powtac