45

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

ine
  • 14,014
  • 8
  • 55
  • 80
  • $.browser has been deprecated. Refer http://stackoverflow.com/questions/1944169/detecting-ie-using-jquery more details – Mandeep Jain Jan 17 '13 at 13:37

13 Answers13

102

As simple as this:

if($.browser.msie && $.browser.version=="6.0") alert("Im the annoying IE6");

Update

Please note that $.browser is removed from jQuery 1.9

If you still need to use $.browser in jQuery 1.9 (or other deprecated functions), try jQuery-migrate (https://github.com/jquery/jquery-migrate/ - http://code.jquery.com/jquery-migrate-1.2.1.js)

Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
  • 2
    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 Sep 16 '09 at 15:52
  • 3
    Another way can be: ($.browser.msie && /6.0/.test(navigator.userAgent) ) – Aamir Afridi Oct 08 '09 at 17:35
  • 19
    ...until they get to IE version 16.0, or 8.6.0, etc. – nickf Jan 19 '10 at 11:56
  • 3
    Note: `jQuery.browser` is removed in 1.9. A plugin is needed. – Tikkes Aug 19 '13 at 08:43
  • 1
    If you still need to use $.browser in jQuery 1.9 (or other deprecated functions), also add a reference to jQuery-migrate (http://code.jquery.com/jquery-migrate-1.2.1.js) – FoxHound May 20 '14 at 15:31
50

You could also ask IE directly.

<!--[if lte IE 6]>
<script type="text/javascript">
  var isRunningIE6OrBelow = true;
</script>
<![endif]-->
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
17

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

https://api.jquery.com/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.

user1438038
  • 5,821
  • 6
  • 60
  • 94
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 7
    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 Aug 24 '10 at 19:03
8
if ($.browser.msie && parseInt($.browser.version, 10) <= 6) {
  alert("I'm not dead yet!"); 
}

-- update

Please not that $.browser is removed from jQuery 1.9

Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236
6

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
  • 64,384
  • 92
  • 238
  • 332
5

If ActiveXObject exists and XMLHttpRequest does not, it's IE6:

/* IE6 Check */
(!!window.ActiveXObject && !window.XMLHttpRequest) ? true : false;

In IE7, it would be:

(!!window.ActiveXObject && !!window.XMLHttpRequest) ? true: false;

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
3

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.

3

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
  • 455
  • 1
  • 5
  • 15
  • 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 Jan 19 '10 at 11:59
1

Just to let people know $.browser.msie && /6.0/.test(navigator.userAgent) is not reliable, I've come to this question looking for an answer to a problem with the JQuery bgiframe which breaks on my machine because I have 'Media Center PC 6.0' in my navigator.userAgent string. I've now edited the source to use the $browser.version test.

Its silly of JQuery to deprecate these browser tests because ugly as they may be they are necessary to deal with the ugly state of the browser ecosystem.

James Ellis-Jones
  • 3,042
  • 21
  • 13
1

i always used this solution

http://sinfinity.pl/blog/2012/02/12/detect-internet-explorer-and-browser-version-in-jquery/

mihau
  • 247
  • 3
  • 9
0

"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.

0

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
  • 3,555
  • 5
  • 22
  • 34
  • 21
    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 Jun 25 '09 at 13:54
  • 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. – Ron Aug 10 '09 at 13:42
  • Above link is broken, should be: http://api.jquery.com/jQuery.browser/#jQuery.browser.version – Douglas Jun 06 '10 at 23:47
0

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