Best way to detect <= IE10

David Diem picture David Diem · Jun 12, 2013 · Viewed 11.5k times · Source

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

  • conditional comments don't do IE9, IE10
  • modernizr feature detection doesn't help here, because the reason is external (IE bug in a mapbox map I want to embed)
  • jQuery + migrate plugin: if this helps, what are the basic steps?

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

Answer

Joe picture Joe · Jun 12, 2013

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.