David Diem
David Diem

Reputation: 97

Best way to detect <= IE10

What's an up-to-date way for detecting IE?

Yeah, I'm rather new to this. Cheers!

Upvotes: 9

Views: 11875

Answers (2)

gs-rp
gs-rp

Reputation: 377

    <!-- IE10 -->
    <script>
    if(navigator.userAgent.match(/MSIE 10/)){

        var headID = document.getElementsByTagName("head")[0];   
        var newLink = document.createElement('link');

        newLink.rel = 'stylesheet';
        newLink.type = 'text/css';
        newLink.href = 'css/ie10.css';

        headID.appendChild(newLink);
    } else {
       /* do something for other versions */
    }
    </script>

Upvotes: 3

Joe
Joe

Reputation: 15528

Use conditional comments for IE6-9 and a little custom function for IE10. For example: HTML:

<!--[if lt IE 7 ]> <html class="ie6 ie"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7 ie"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8 ie"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9 ie"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->

JS:

if ($('html').hasClass('ie') || (Function('/*@cc_on return document.documentMode===10@*/')())){
    isIE = true;
}else{
    isIE = false;
}

or you can just use:

if (Function('/*@cc_on return document.documentMode===10@*/')()){
    document.documentElement.className+=' ie';
}
// Then..
if($('html').hasClass('ie')){...} // to check for IE10 and below.

Other answers focus on specifically detecting IE10 but I thought it would be helpful to show complete detection code in one place.

Upvotes: 19

Related Questions