(Don't care about the version. IE or not IE.)
-
Are you using any particular language? – joejoeson Feb 5 '10 at 23:05
-
The above are all JavaScript (or a framework). – Marcel Korpel Feb 5 '10 at 23:07
-
The question was tagged JavaScript. – Dean Burge Feb 6 '10 at 0:01
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
-
-
I added a post to give more info, but this is really the best answer. – Joe Mills Feb 5 '10 at 23:10
-
A User Agent string could easily be forged, at least in Opera and Safari. – Marcel Korpel Feb 5 '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 5 '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 5 '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.
-
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 5 '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 5 '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 '<marquee>' for instance). – monojohnny Feb 5 '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 nczonline.net/blog/2009/12/29/… describes. This way you're always sure a wanted function exists. – Marcel Korpel Feb 5 '10 at 23:46
-
2You can use conditional comments to include a different
<body class="something">
tag on IE vs other browsers. Then you can use rules likebody.ie6 .something { ... }
for CSS fixes without hacks. – bobince Feb 6 '10 at 0:02
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.
-
-
-
@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 5 '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 5 '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.
-
-
@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 5 '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 5 '10 at 23:14