(Don't care about the version. IE or not IE.)
-
Are you using any particular language? – joejoeson Feb 05 '10 at 23:05
-
The above are all JavaScript (or a framework). – Marcel Korpel Feb 05 '10 at 23:07
-
The question was tagged JavaScript. – Dean Burge Feb 06 '10 at 00:01
4 Answers
http://api.jquery.com/jQuery.browser/
isIE = $.browser.msie;
$.browser
became deprecated in the meanwhile but is not yet deactivated. $.support
should be used instead. Example: $.support.boxModel

- 40,542
- 28
- 115
- 170
-
-
I added a post to give more info, but this is really the best answer. – Joe Mills Feb 05 '10 at 23:10
-
A User Agent string could easily be forged, at least in Opera and Safari. – Marcel Korpel Feb 05 '10 at 23:14
-
@Marcel - RE: forged user agents - but that's just the nature of HTTP; there's no way round that. – monojohnny Feb 05 '10 at 23:25
-
1Though 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 Feb 05 '10 at 23:47
-
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.

- 21,536
- 6
- 60
- 80
-
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 Feb 05 '10 at 23:21
-
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 Feb 05 '10 at 23:24
-
@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 ' – monojohnny Feb 05 '10 at 23:30
-
@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 Feb 05 '10 at 23:46
-
@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 Feb 05 '10 at 23:51
-
2You can use conditional comments to include a different `` tag on IE vs other browsers. Then you can use rules like `body.ie6 .something { ... }` for CSS fixes without hacks. – bobince Feb 06 '10 at 00:02
-
@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 Feb 06 '10 at 10:50
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.

- 1,619
- 11
- 12
-
-
-
@Marcel - works for me - you sure the site didn't prevent you viewing the page, because you have a forged User-Agent string ?? :-) – monojohnny Feb 05 '10 at 23:28
-
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 Feb 05 '10 at 23:55
<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.

- 5,894
- 16
- 59
- 83
-
-
@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 Feb 05 '10 at 23:12
-
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 Feb 05 '10 at 23:14