Search code examples
cssinternet-explorerinternet-explorer-11feature-detection

Detecting IE version using CSS Capability/Feature Detection


IE10+ no longer supports browser detection tags to identify a browser.

For detecting IE10 I am using JavaScript and a capability-testing technique to detect certain ms prefixed styles are defined such as msTouchAction and msWrapFlow.

I want to do the same for IE11, but I am assuming that all the IE10 styles will be supported in IE11 as well. Can anyone help me identify IE11 only styles or capabilities that I could use to tell the two apart?

Extra Info

  • I don't want to use User Agent type detection because it's so spotty, and can be changed, as well as I think I've read that IE11 is intentionally trying to hide the fact it's Internet Explorer.
  • For an example of how the IE10 capability testing works, I used this JsFiddle (not mine) as a basis for my testing.
  • Also I am expecting a lot of answers of "This is a bad idea...". One of my needs for this is that IE10 claims it supports something, but it is very badly implemented, and I want to be able to differentiate between IE10 and IE11+ so I can move on with a capability-based detection method in the future.
  • This test is coupled with a Modernizr test that will simply make some functionality "fallback" to less glamorous behavior. We are not talking about critical functionality.

I am already using Modernizr, but it doesn't help here.


Solution

  • So I found my own solution to this problem in the end.

    After searching through Microsoft documentation I managed to find a new IE11 only style msTextCombineHorizontal

    In my test, I check for IE10 styles and if they are a positive match, then I check for the IE11 only style. If I find it, then it's IE11+, if I don't, then it's IE10.

    Code Example: Detect IE10 and IE11 by CSS Capability Testing (JSFiddle)

     /**
      Target IE 10 with JavaScript and CSS property detection.
      
      # 2013 by Tim Pietrusky
      # timpietrusky.com
     **/
    
     // IE 10 only CSS properties
     var ie10Styles = [
         'msTouchAction',
         'msWrapFlow',
         'msWrapMargin',
         'msWrapThrough',
         'msOverflowStyle',
         'msScrollChaining',
         'msScrollLimit',
         'msScrollLimitXMin',
         'msScrollLimitYMin',
         'msScrollLimitXMax',
         'msScrollLimitYMax',
         'msScrollRails',
         'msScrollSnapPointsX',
         'msScrollSnapPointsY',
         'msScrollSnapType',
         'msScrollSnapX',
         'msScrollSnapY',
         'msScrollTranslation',
         'msFlexbox',
         'msFlex',
         'msFlexOrder'];
    
     var ie11Styles = [
         'msTextCombineHorizontal'];
    
     /*
      * Test all IE only CSS properties
      */
     var d = document;
     var b = d.body;
     var s = b.style;
     var ieVersion = null;
     var property;
    
     // Test IE10 properties
     for (var i = 0; i < ie10Styles.length; i++) {
         property = ie10Styles[i];
    
         if (s[property] != undefined) {
             ieVersion = "ie10";
             createEl("IE10 style found: " + property);
         }
     }
    
     // Test IE11 properties
     for (var i = 0; i < ie11Styles.length; i++) {
         property = ie11Styles[i];
    
         if (s[property] != undefined) {
             ieVersion = "ie11";
             createEl("IE11 style found: " + property);
         }
     }
    
     if (ieVersion) {
         b.className = ieVersion;
         $('#versionId').html('Version: ' + ieVersion);
     } else {
         createEl('Not IE10 or 11.');
     }
    
     /*
      * Just a little helper to create a DOM element
      */
     function createEl(content) {
         el = d.createElement('div');
         el.innerHTML = content;
         b.appendChild(el);
     }
    
     /*
      * List of IE CSS stuff:
      * http://msdn.microsoft.com/en-us/library/ie/hh869403(v=vs.85).aspx
      */
    body {
        font: 1.25em sans-serif;
    }
    div {
        background: red;
        color:#fff;
        padding: 1em;
    }
    .ie10 div {
        background: green;
        margin-bottom:.5em;
    }
    .ie11 div {
        background: purple;
        margin-bottom:.5em;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <h1>Detect IE10 and IE11 by CSS Capability Testing</h1>
    
    
    <h2 id="versionId"></h2>

    I will update the code example with more styles when I discover them.

    NOTE: This will almost certainly identify IE12 and IE13 as "IE11", as those styles will probably carry forward. I will add further tests as new versions roll out, and hopefully be able to rely again on Modernizr.

    I'm using this test for fallback behavior. The fallback behavior is just less glamorous styling, it doesn't have reduced functionality.