1

The lowest browser my application supports is IE 7. I have some code that works fine in IE 8, but needs to be avoided in IE 7. There are a ton of questions/answers on here that indicate how to detect IE 7 with .browser but none that indicate how to detect only IE 7 with .support. The jQuery page detailing .support does not make it clear which supported features are present in which browsers, so not getting much help there.

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • I think this is one of the cases where checking the user agent string (= detecting based on *name* and not *features*) may be the right way to go. – Pekka Oct 17 '12 at 22:33
  • Look at this question: [http://stackoverflow.com/questions/3165489/how-to-detect-ie7-with-jquery][1] [1]: http://stackoverflow.com/questions/3165489/how-to-detect-ie7-with-jquery – weexpectedTHIS Oct 17 '12 at 22:34
  • @weexpectedTHIS ah, yeah, saw both before I posted. Both use `.browser` which is deprecated. – ubiquibacon Oct 17 '12 at 22:35
  • 2
    `.browser` is deprecated for the **exact same reason** that you're trying to use it for. The *new standard* is to use proper feature detection instead of sniffing userAgent strings or blocking solely older versions of IE. It is very possible that someone may have Firefox 2 installed or some hipster browser that has absolutely no HTML5 support, that's why simply testing for older versions of IE is deprecated. – Fabrício Matté Oct 17 '12 at 23:10
  • What is the hack code you are using for IE7? Maybe if you showed that, we could give you a better solution. – epascarello Oct 17 '12 at 23:27
  • Also, unfortunately `jQuery.support` is populated on every page load and used mostly internally by the jQuery core - it only has the required tests for the core to run, and some properties may be removed in future to reduce the init overhead. **tl;dr:** `.support` is not a full feature detection utility and (IMO) not reliable for the long term if you plan on upgrading jQuery often. Modernizr is a library for that purpose, but if you need to detect just a very few features, you can update the post with those (as commented by epascarello) and it'd be easier to provide a proper answer. – Fabrício Matté Oct 17 '12 at 23:32
  • possible duplicate of [How to detect IE7 and IE8 using jQuery.support](http://stackoverflow.com/questions/8890460/how-to-detect-ie7-and-ie8-using-jquery-support) – Chris Moschini Apr 02 '13 at 04:58

3 Answers3

3

The reason they deprecated .browser is to try to encourage us to test for features instead of browsers.

However.. if you still need to...

Have you considered using H5BP's conditional <html> trick and then just testing for that? Header:

<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->

Then in jQuery,

$(function() {
    if( !$('html').hasClass('lt-ie8') ) {
        // Do stuff
    }
});
Matt Stauffer
  • 2,706
  • 15
  • 20
  • I have seen this code before, but didn't realize what it was doing. Is there any caveat to doing browser detection like this? – ubiquibacon Oct 17 '12 at 22:55
  • I couldn't say for sure, but H5BP is created (and audited) by people a lot smarter than me, so that's a good indication it's ok. More details here: https://github.com/h5bp/html5-boilerplate/blob/v4.0.0/doc/html.md – Matt Stauffer Oct 17 '12 at 23:00
0

It is nearly always better to identify the features you are targeting in IE8 and use "feature detection" to see if they are available (by testing the feture's functionality) and if they are not available, then use alternate code (for IE7 or any other browser without that feature).

If you offer more specifics on the functionality that doesn't work in IE7, we could help you solve this problem with feature detection, not browser version detection.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Kinda the same problem with IE 8. The jQuery `.support` page does not do a good job of indicating what features are available in what browsers so I don't know how to narrow down that a browser is IE 8 using `.support`. – ubiquibacon Oct 17 '12 at 22:41
  • @typoknig - I'm talking about doing your own feature detection code, not relying on undocumented things in jQuery so I'm not talking about using jQuery.support at all. It's easy to detect most features yourself. – jfriend00 Oct 17 '12 at 22:46
0

Did you look at the API for $.browser?

http://api.jquery.com/jQuery.browser/#jQuery-browser-version2

But there is a reason why it is deprecated, it is a bad thing to do. You should sniff using feature detection.

epascarello
  • 204,599
  • 20
  • 195
  • 236