0

I know how to test an ie version using the conditional comments. But, I found out a good and clean way to do it, by testing a javascript method and check if it's supported or not, like:

if(  document.addEventListener  ){
    alert("you got IE9 or greater");
}

But, I don't know how to test it if it's an specific version (like, I want to do something if it's IE8, but not if it's IE7 or IE9 nor other version.

I could use James Panolsey's solution

var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());

But it does test using Conditional Comments.


Why not Conditional Comments? Because of the Standards mode. As Microsoft states in their website and in the dev center, they do NOT support it in their new versions:

Important As of Internet Explorer 10, conditional comments are no longer supported by standards mode. Use feature detection to provide effective fallback strategies for website features that aren't supported by the browser. For more info about standards mode, see Defining Document Compatibility.


Testing by a valid Method is more efficient in my POV, But I don't know how to do it in a way to specific the version ... ("Using this you can test for that version"). The only one that I know is the above one that I used as example. Is there others methods that I could use to test it? or not?

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
  • 2
    If you only need to test for a specific version that *does* support conditional comments, and ignore all newer versions, then you can use conditional comments just fine. Otherwise, just use feature detection as in your `addEventListener` example. – BoltClock May 05 '14 at 13:25
  • Have a look at http://modernizr.com/ – Jay Blanchard May 05 '14 at 13:25
  • 1
    test for different things, like for canvas <= ie9 – toesslab May 05 '14 at 13:25
  • @BoltClock I needed to test if it's ie for a function and if it's ie >= 11 for other. I just don't know where to look to find when some methods started being supported. so I could use them to test for specific functions. I just know the `addEventListener` atm. @pc-shooter said to test for canvas for example, that is a good one, but I want more, and don't know any nor where to look. – Michel Ayres May 05 '14 at 13:29
  • 1
    @MichelAyres - Look for the actual features you need. Don't try to guess at the browser based upon something or another being true. This is a fragile mess that the web community has been breaking free of for the past 10 years. – Jeremy J Starcher May 05 '14 at 13:34
  • @JeremyJStarcher What do you mean by "Don't try to guess at the browser based upon something or another being true." (it's about the `if` being used to detect the browser?) and also "This is a fragile mess that the web community has been breaking free of for the past 10 years" ? – Michel Ayres May 05 '14 at 13:38
  • @MichelAyres - http://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection – Jeremy J Starcher May 05 '14 at 13:40

1 Answers1

0

While feature detection can be very useful, and is sometimes the quickest way to distinguish between supportive and non-supportive browser versions, detecting IE and its versions is actually a piece of cake:

var uA = navigator.userAgent;
var browser = null;
var ieVersion = null;
var htmlTag = document.documentElement;

if (uA.indexOf('MSIE 6') >= 0) {
    browser = 'IE';
    ieVersion = 6;
}
if (uA.indexOf('MSIE 7') >= 0) {
    browser = 'IE';
    ieVersion = 7;
}
if (document.documentMode) { // as of IE8; strictly IE proprietary 
    browser = 'IE';
    ieVersion = document.documentMode;
}

// Using it can be done like this: 
if (browser == 'IE' && ieVersion == 11)
    htmlTag.className += ' ie11';

.
That's all you need. You're catching higher IEs in lower Modes/Views as well with this script, including Compatibility ~.

  • While this could be helpful, you should emphasize a bit more that this doesn't actually detect the browser *version*, but just the browser *mode*. That may be what people want - but it may not. (For example, maybe they want to tell people, "Hey; You are in IE9 mode, but you have IE11!") – Andrew Barber Jun 26 '14 at 14:07
  • @AndrewBarber: That's true in itself. But I would think the OP has a more sophisticated objective than that. – Frank Conijn - Support Ukraine Jun 26 '14 at 14:24
  • I hope so, too; but just in case not - and for the others who will find this via Google; just to make the (potentially minor) difference known. That is definitely *the* way to detect documentMode, though. A lot less fragile and weird than previous things for versions. – Andrew Barber Jun 26 '14 at 14:26
  • @AndrewBarber: Actually, I don't think one can tell IE versions from modes/views. An IE11 in IE8 mode carries/sends out the uA of IE8. The original IE8 uA, without any 'marking' that it is actually IE11 in IE8 mode/view. – Frank Conijn - Support Ukraine Jun 26 '14 at 20:40
  • Correct; you can't. Which is why I suggested adding the clarification. :) – Andrew Barber Jun 26 '14 at 21:08