25

How can I detect IE 7 and IE 8 using jQuery.support properties?

Is it possible to detect the browser versions using jQuery.support or just those blah blah blah browser features?

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

12 Answers12

41

This is totally possible with support:

if (!$.support.leadingWhitespace) {
    //IE7 and 8 stuff
}

This also detects IE 6 however, if you don't want IE 6 to run this code block, you will need another flag to exclude it

A good reason for not using browser is it is a deprecated feature, and it will likely be removed to a plugin with version 1.9. (See the comments on the answer to How to detect IE7 with jQuery?)

"We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery" http://api.jquery.com/jQuery.browser/

here is a working example: http://jsfiddle.net/AGtG8/16/

Community
  • 1
  • 1
jas
  • 537
  • 1
  • 7
  • 12
8
if (jQuery.support.leadingWhitespace == false){

... code for IE7-IE8 ...

}
else {
... 
}
IberoMedia
  • 2,226
  • 7
  • 36
  • 61
piciu
  • 81
  • 1
5

Be careful with the following because it also includes IE10:

if ($.browser.msie && $.browser.version.substr(0,1)<7) {
//<IE7
}

better use:

if ($.browser.msie && parseInt($.browser.version,10)<7) {
//<IE7
}
Bob Hamblok
  • 131
  • 1
  • 2
  • 3
    `$.browser` is no longer included in jQuery 1.9+, you can add it back using the jQuery Migrate plugin if needed. – nullability Jun 28 '13 at 18:56
3

One trick I found is to add Conditionnal Tags in the HTML

<!--[if lt IE 7]><body class="ie ie6 lte9 lte8 lte7"><![endif]-->
<!--[if IE 7]><body class="ie ie7 lte9 lte8 lte7"><![endif]-->
<!--[if IE 8]><body class="ie ie8 lte9 lte8"><![endif]-->
<!--[if IE 9]><body class="ie ie9 lte9"><![endif]-->
<!--[if !IE]><!--><body class="not-ie"><!--<![endif]-->

Then use JQuery like this :

$('body.ie7')

It also helps for CSS on IE specific

body.ie6{ margin: 0; }

I don't know if it's good for you but anyways, that's still an option.

SequenceDigitale.com
  • 4,038
  • 1
  • 24
  • 23
  • many times when writing plugins which runs on other websites, you can't do this. also, this is byte-wasting and very makes your code messy. – vsync Jan 15 '13 at 22:04
  • 1
    @vsync No it doesn't. It's a good practice that [HTML5 Boilerplate preaches too.](https://github.com/h5bp/html5-boilerplate/blob/master/index.html#L1-L5) – MT. Jul 24 '13 at 20:51
  • @MT - and why should I listen to HTML5 Boilerplate? I've been in the business long enough to posses opinions of my own..probably longer than the guys there. – vsync Jul 27 '13 at 10:25
  • @MT, I've found this code a few years ago on EA official website. Those guys are paid to make good stuff. You are right, you can code however you want, there is no perfect code. This one seems messy for you, but anyone's code is messy to someone else. This code works for me. It is clean and easy to understand, way better than tons of line and functions in JS. If you have a better solution, share it. I will be happy to learn new stuff. – SequenceDigitale.com Aug 02 '13 at 01:06
  • @SequenceDigitale.com You probably wanted to mention vsync and not me :) – MT. Aug 03 '13 at 18:54
  • Microsoft have removed conditional comments from IE10 so this won't work going forward. See here: http://www.sitepoint.com/microsoft-drop-ie10-conditional-comments/ @vsync – doiley Sep 04 '13 at 01:43
  • @doiley - yes, as a web developer it is my first most duty to know such things, so please don't teach me the basic details of my own profession. and just fyi, IE10 is a pretty damn fine browser, and the chances you'll need to target it are probably slim. – vsync Sep 04 '13 at 08:02
  • @vsync - It wasn't a lesson, take a chill pill Mr. Professional! I added you in reference to your post above, because I thought it would be useful to validate your answer given the changes in IE10 – doiley Sep 04 '13 at 22:25
  • what answer? I didn't answer the OP's question in the matter. and the question here regards only IE7 & 8, to detect all IE's, I would recommend using `` – vsync Sep 05 '13 at 10:05
  • 1
    @vsync, your code didn't show up there, fyi. Seeing as the OP is specifically targeting oldIE - the browser and not the feature set (which is valid, say, for including jQuery 1.x vs 2.x) - using CCs can *guarantee* you won't target other browsers which lack a specific feature, but don't have the exact same deficiencies that IE8/9 does. Re: H5BP, it's not 'experience in the game' that counts on each issue, it's the amount of public and detailed debate that's gone into the final decision. IE10 does still have problems but they're easier to feature-detect-and-shim in IE10. – iono Sep 10 '13 at 04:34
2

jQuery.support is for detecting browser features. To detect browser version use jQuery.browser:

if ($.browser.msie && $.browser.version.substr(0,1)<7) {
//IE7

}

Update: $.browser is deprecated now, don't use it.

AndreyM
  • 1,403
  • 2
  • 12
  • 23
  • I think this will only detect IE6 and below. You should use the comparision operator `<=` instead if you want to include IE7 too. – Dzhuneyt Dec 21 '12 at 16:48
2

as explained, .support is for feature-detection. if you want to detect the browser, just use .browser.

 var ua = $.browser;
 if ( ua.msie && ua.version.slice(0,1) == "8" ) {
   alert('IE 8');
 } else if ( ua.msie && ua.version.slice(0,1) == "7" ) {
   alert('IE 7');
 } else {
   alert('something else');
 }
Frodik
  • 14,986
  • 23
  • 90
  • 141
oezi
  • 51,017
  • 10
  • 98
  • 115
1

I've noticed many different ways of doing this, this DOESN'T use $.support although I've found that this works very well. Also just as a note, jquery has just announced that they will be removing any support for IE 6,7,8 in jquery 2.0 (http://blog.jquery.com/2012/06/28/jquery-core-version-1-9-and-beyond/).

var msVersion = navigator.userAgent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/),
      msie = !!msVersion,
      ie6 = msie && parseFloat(msVersion[1]) < 7;

  // Help prevent flashes of unstyled content
  if (!ie6) {
    //Do Something here.
  }

Happy coding!

Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95
0

I use this to check if browser is ie 8 or less

to change the ie version simply update the 8 to another ie verion

if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 8 ) {   
    runIECode();
} else {
    runOther();
}
Dan
  • 1,295
  • 2
  • 22
  • 46
0

I added few lines of Javascript to make it work again:

jQuery.uaMatch = function (ua) {
    ua = ua.toLowerCase();

    var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
            /(webkit)[ \/]([\w.]+)/.exec(ua) ||
            /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
            /(msie) ([\w.]+)/.exec(ua) ||
            ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
            [];

    return {
        browser: match[ 1 ] || "",
        version: match[ 2 ] || "0"
    };
};

if (!jQuery.browser) {
    matched = jQuery.uaMatch(navigator.userAgent);
    browser = {};

    if (matched.browser) {
        browser[ matched.browser ] = true;
        browser.version = matched.version;
    }

    // Chrome is Webkit, but Webkit is also Safari.
    if (browser.chrome) {
        browser.webkit = true;
    } else if (browser.webkit) {
        browser.safari = true;
    }

    jQuery.browser = browser;
}
coorasse
  • 5,278
  • 1
  • 34
  • 45
0
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { 
    var ieversion = new Number(RegExp.$1);
    alert(ieversion < 9);
}
CSchulz
  • 10,882
  • 11
  • 60
  • 114
madhi
  • 1
  • 3
-1

Use jQuery.browser.version instead of support

Grrbrr404
  • 1,809
  • 15
  • 17
  • 1
    Yeah and i told you to use browser.version instead of support. It is not possible with support. – Grrbrr404 Jan 17 '12 at 06:30
  • 1
    FWIW, `jQuery.browser.version` may work for users actually using older versions of IE, but it fails for testing using compatability mode for an earlier version with IE9 (the version shows as "9.0" instead of the earlier version.) However, `jQuery.support` does work with IE9 compatability mode. – Peter Gluck Nov 01 '12 at 00:41
  • It's deprecated in jquery 1.9> – Shannon Hochkins Mar 25 '13 at 22:41
  • 1
    actually... it's now removed – Kevin Apr 01 '13 at 13:55
-1

Try $.browser, it will let you know if browser version is less than IE9

 var isM = false;
 $.each($.browser, function (i, val) {
    if ($.browser.msie && parseInt(val) < 9) isM = true;
});
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46