Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to detect with just plain javascript (no frameworks) when the browser is IE10 despite the browser mode setting.

Some comments: - I do need to detect the browser, it isn't an option to detect just features since the purpose is mitigating a browser bug. - I have tried the common ways (like UA string or feature detection) with no success, when I switch browser mode to IE9 every aspect that could suggest being in IE10 just vanishes.

like image 697
gztomas Avatar asked Apr 04 '13 19:04

gztomas


People also ask

How can I tell if Internet Explorer is being used?

To detect whether the current browser is Internet Explorer, you can make use of the navigator. userAgent property. The userAgent property returns the value of the user-agent header sent by the browser to the server. It contains information about the name, version, and platform of the browser.

Is IE9 still supported?

As of January 12, 2016, Microsoft has ended mainstream support for IE9 and IE10. As such, Essent support of IE9 and IE10 is scheduled to end on March 31, 2017.

Is browser IE JavaScript?

JavaScript is a browser-based scripting language that is used by web developers to add dynamic interactions and functionalities to web pages. Today, modern web browsers like Internet Explorer 11 have JavaScript enabled by default, allowing users access to enjoy user-interactive experiences on the internet.


1 Answers

If you want to detect the browser you're working with, IE has a special feature, called conditional compilation - http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

To get the version, you'd use @_jscript_version. So in Javascript, I'd use:

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

I don't have IE10, but when testing with IE9, it seems to work. If I change the browser mode to 7 or 8, @_jscript_version has the true browser JScript version (it stays as 9 in my case).

To see the list of JScript versions, you can see them here - http://en.wikipedia.org/wiki/JScript#Versions . It doesn't list IE10, but I'd assume it's 10. Before IE9, they used numbers inconsistent with the browser version, but are possibly on the right track since 9. You'll have to see what @_jscript_version is by default for 10, but I'd assume it starts with "10" and has a minor version possibly.


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).

like image 145
Ian Avatar answered Nov 15 '22 12:11

Ian