views:

17979

answers:

10

I'm new to jquery and was wondering: is a simple way to detect whether a browser is Internet Explorer 6 or below?

+11  A: 

jQuery checks for features, rather than "browsers". That said, you can use the jQuery.support method to detect what the users browser is capable of.

Deprecated Methods (Do not use)

  • $.browser
  • $.browser.version
  • $.boxModel

http://docs.jquery.com/Utilities/jQuery.support will give you a summary of which features are supported by which browsers. Taking that data, you'll develop a couple conditional checks to determine if the browser being used is your target browser or not.

Jonathan Sampson
This is relevant if the user wants to detect that functionality, but there's no jQuery.support method to check for: "does the browser add hashes to the browser history" or the like, so it's more appropriate at that point to detect browser version instead of "does it support .cssFloat and .htmlSerialized?
ebynum
+1  A: 

http://docs.jquery.com/Utilities/jQuery.browser.version is how

EDITED: Corrected link from Douglas

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

I mean, if you had the old version not the latest 1.3...

Also, another good point is that with JQuery, you are not supposed to worry about the version. JQuery does feature testing and handles all that malarky for you. Worrying about versions and platforms is pretty 1999

MrChrister
$.browser is deprecated.
Jonathan Sampson
Companies still demand support for IE6 and you often have to do things specifically for it. I'm pretty baffled that .browser is removed, that's just stupid.
alex
Alex, I'm with you on that. Off topic, I just happened to notice that you are the creator of SyntaxHighlighter. Nice work, I use it on my personal site.
Ronnie
Above link is broken, should be: http://api.jquery.com/jQuery.browser/#jQuery.browser.version
Douglas
A: 

Try this out:

 jQuery.each(jQuery.browser, function(i, val) {
  $("<div>" + i + " : <span>" + val + "</span>")
            .appendTo(document.body);
});

if you need more info refer to this:

http://docs.jquery.com/Utilities/jQuery.browser

+7  A: 

Worrying about versions and platforms is pretty 1999

Too bad a good chunk of the web is still living in that era...

Antilogic
+1 'cos "Worrying about versions and platforms is pretty 1999" is about the most ridiculous thing I've heard today. Oh to live in that world :)
da5id
It's easy to be flippant and say "That's so 1999", but some problems aren't easily solved by looking at feature support. How would you handle Transparent PNG support, for example? There's no test I'm aware of that can tell you if a browser supports PNG fully? We're left testing for IE6 (and lower).
Axeva
Avexa: that is indeed a problem I haven't found a way to feature detect, but that is the only one I can think of.
Tim Down
+1  A: 

Very nice way to detect IE is:

if ('v'=='\v') {
   welcome to IE )) 
}

Unfortunately it can't recognize its version but it's not always nessecary.

Roman
+19  A: 

You could also ask IE directly.

<!--[if lte IE 6]>
<script type="text/javascript">
  var isRunningIE6OrBelow = true;
</script>
<![endif]-->
Alister Bulman
+33  A: 

As simple as this:

if($.browser.msie && $.browser.version=="6.0") alert("Im the annoying IE6");
AamirAfridi.com
For detecting a specific version of Internet Explorer only, Aamir's approach is almost perfect in my opinion.However, I think you should test the value of $.browser.version as an integer, not as a string. Then you can do more interesting things, like apply bindings on document load to fix common bugs across IE7 and IE6 simultaneously (for example).But I write only from the explicit point of view that I only care about 1) Microsoft Internet Explorer, and 2) what version it is if detected. Deprecated or not, querying $.browser and $.browser.version was the most direct approach for me.
markedup
AamirAfridi.com
...until they get to IE version 16.0, or 8.6.0, etc.
nickf
+1  A: 

Also, another good point is that with JQuery, you are not supposed to worry about the version.

That doesn't help if you're using jquery to fix an IE6 css rendering bug though.

SteveB
A: 

"While it is unlikely jQuery.browser will be removed, every effort to use jQuery.support and proper feature detection should be made."

So I say go ahead and use it. They have to maintain backwards-compatibility with scripts that still use the user-agent sniffing method.

+2  A: 

I often check the version of a browser. The .support method is great, but it doesn't really help when you need to hide selects when there's an overlay. There is no "supports selects are windowed controls". You just need to check the browser version, so I would say err towards the .support method where you can, and use the .browser where necessary.

Paul Deen
I wholeheartedly agree. So many people shout the praises of `$.support`, but I've never *ever* found it useful. It is designed for internal- and plugin-use only, IMO.
nickf