79588064

Date: 2025-04-23 07:35:01
Score: 0.5
Natty:
Report link

Bulletproof way to determine Internet Explorer. Tested extensively via BrowserStack. Supports minification.

var isIE = (function(window, IEmap) {

        if (typeof window == 'object' && window && window.window == window && typeof window.document == 'object') {

            var is_default_IE11 = !!(window.msCrypto && !document.currentScript); 

            /* 
                Conditional compilation is a special comment syntax that executes in IE only. Regular browsers, and IE11** interpret them as normal comments.
                `@_jscript_version` is an IE-specific conditional constant. 
                **If the `documentMode` is changed, IE11 reports `@_jscript_version` as "11", and "is_default_IE11" returns `false`. (Yep, textbook Microsoft) 
                Shoutout to https://stackoverflow.com/users/5807141/j-j for the overlooked comment on msCrypto: which is only defined in IE11 as IE11 doc mode.
                NOTE: window.msCrypto check is future-proofed by also checking for the non-existence of `document.currentScript`.
                Sources: https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto , https://stackoverflow.com/questions/21825157/internet-explorer-11-detection
            */

            var jscript_version = Number( new Function("/*@cc_on return @_jscript_version; @*\/")() ) || (is_default_IE11 ? 11 : undefined);

            // Workaround Test for Windows Service Pack Update (IE6 / 7). Document mode wasnt introduced until IE8, so this check works fine.
            if (jscript_version === 5.7 && !window.XMLHttpRequest) { jscript_version = 5.6 }
            if (!jscript_version) { return false }

            var envir = { 
                'jscript': jscript_version, 'mode': document.documentMode, 'is_default_IE11': is_default_IE11, 
                'browser': (IEmap[String(jscript_version)] || jscript_version)
            };

            envir[envir.browser] = (envir.browser == envir.mode); // Make sure if we're screening for IE.x as IE.x that its running as that with same doc mode

            return envir;
        } else {
            return false;
        }

    })(window, {'5': 5, '5.5': 5.5, '5.6': 6, '5.7': 7, '5.8': 8, '9': 9, '10': 10, '11': 11}); // `@_jscript_version` mapped to IE browser versions.
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Ashraile