Search code examples
javascriptinternet-explorerinternet-explorer-10

Check for IE 10


How can I make a message box appear on page load if the user is using IE 10?

function ieMessage() {
    alert("Hello you are using I.E.10");
}

My webpage is a JSF facelet (XHTML).


Solution

  • The real way to detect this, without conditional comments and without User Agent sniffing is with conditional compilation:

    <script type="text/javascript">
        var isIE10 = false;
        /*@cc_on
            if (/^10/.test(@_jscript_version)) {
                isIE10 = true;
            }
        @*/
        console.log(isIE10);
    </script>
    

    After running this code, you can use following anytime after:

    if (isIE10) {
        // Using Internet Explorer 10
    }
    

    Reference: How can I detect IE10 from JS when browser mode is IE9?


    UPDATE:

    To avoid minification of comments, you can use something like:

    var IE = (function () {
        "use strict";
    
        var ret, isTheBrowser,
            actualVersion,
            jscriptMap, jscriptVersion;
    
        isTheBrowser = false;
        jscriptMap = {
            "5.5": "5.5",
            "5.6": "6",
            "5.7": "7",
            "5.8": "8",
            "9": "9",
            "10": "10"
        };
        jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();
    
        if (jscriptVersion !== undefined) {
            isTheBrowser = true;
            actualVersion = jscriptMap[jscriptVersion];
        }
    
        ret = {
            isTheBrowser: isTheBrowser,
            actualVersion: actualVersion
        };
    
        return ret;
    }());
    

    And access the properties like IE.isTheBrowser and IE.actualVersion (which is translated from internal values of JScript versions).