0

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

Nicolás
  • 7,423
  • 33
  • 35
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

4 Answers4

10

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

powtac
  • 40,542
  • 28
  • 115
  • 170
10

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
  • 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 '' for instance). – 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
  • 2
    You 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
1

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,619
  • 11
  • 12
0
<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
  • 5,894
  • 16
  • 59
  • 83
  • I don't think you want to send up an alert window. – joejoeson Feb 05 '10 at 23:06
  • @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