How do I detect IE10 using javascript?

periback2 picture periback2 · May 3, 2013 · Viewed 23.2k times · Source

As many already posted in other questions (also in jQuery documentation), the old jQuery.browser.version is deprecated and works only in jquery1.3.

Do you know another simple way to detect it, that I can include in my code before:

function handleInfoDivPopupVisibility(dynamicEleId, staticEleId){
var parentContainer = $('headerSummaryContainer');
var dynamicEle = $(dynamicEleId);
var staticEle = $(staticEleId);

if(isIE() && parentContainer){
    if (jQuery.browser.version != 10) { // I need to find a way to detect if it's IE10 here.
        parentContainer.style.overflow = 'visible'; 
    }
}
dynamicEle ? dynamicEle.style.display = '' : '';
if(dynamicEle && staticEle)
    gui_positionBelow(dynamicEle, staticEle);
}

Before you say it's duplicated question of this or this, I'd like to reinforce that I don't want to use css hacks. Is there a way to detect it just as simple as I could do before?

if (jQuery.browser.version != 10) {...

Answer

J0HN picture J0HN · May 3, 2013

In general it's a bad idea to check for browser version, it's considered a better practice to check for browser features. But if you're sure what you're doing:

function getIEVersion(){
    var agent = navigator.userAgent;
    var reg = /MSIE\s?(\d+)(?:\.(\d+))?/i;
    var matches = agent.match(reg);
    if (matches != null) {
        return { major: matches[1], minor: matches[2] };
    }
    return { major: "-1", minor: "-1" };
}

var ie_version =  getIEVersion();
var is_ie10 = ie_version.major == 10;

We have the following code in production, so it works and well-tested.

And yes, we did have a need to detect IE10, not just a particular feature that exists in IE10 but not in earlier versions.