Test for Internet Explorer in JavaScript

var isMSIE = /*@cc_on!@*/0;

if (isMSIE) {
  // do IE-specific things
} else {
  // do non IE-specific things
}

Comments

  1. User Avatar
    Rodney Reid
    Permalink to comment#


    if (-[1,]) {
    // do non IE-specific things
    } else {
    // do IE-specific things
    }

  2. User Avatar
    Ruben Vreeken
    Permalink to comment#

    If you need to know if an IE browser is used purely for the sake of implementing IE-specific css rules, you can simply put the following code at the top of your html file. Basically it places an tag with a css class for ie 6 through 9.

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

    This way u dont need any javascript to use IE-specific css code.

    • User Avatar
      Stacey
      Permalink to comment#

      Can someone explain the following to me? I’m not sure that I get this conditional comment. What is its intent?

      <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->

    • User Avatar
      vikas
      Permalink to comment#

      @stacey: that condition says, if the browser is Greater than IE9 or Not any of the IE then do this , which will be for browsers other than IE like chorme etc.

  3. User Avatar
    Lonesome Walker
    Permalink to comment#

    The question was not: how to be done without JS, it was explicit WITH js…

    Thanks, helped me a lot!

  4. User Avatar
    eman on
    Permalink to comment#

    Oh the pains of IE – thanks

  5. User Avatar
    jonathan marzullo
    Permalink to comment#

    It is better to use feature detection:

    // IE feature detection
    var isIE9 = document.addEventListener,
    isIE8 = document.querySelector,
    isIE7 = window.XMLHttpRequest;

    if(isIE9){
    // is IE9
    } else if(isIE8) {
    // is IE8
    } else if(isIE7) {
    // is IE7
    }

    hope this helps!

  6. User Avatar
    Melanie
    Permalink to comment#

    If you’re using jQuery…

    
    
    
    if($('html').hasClass('lt-ie8')) { //Do IE7 specific stuff }
    
    
    • User Avatar
      Melanie
      Permalink to comment#

      … in combination with:

      
      <!--[if IE 7]><html class="no-js lt-ie9 lt-ie8" > <![endif]-->"
      
      
    • User Avatar
      Flak DiNenno
      Permalink to comment#

      minor nitpick, but, I don’t think you meant to include that last double-quote(“) on your 2nd comment here. Also, you might want to use the HTML conditionals first and nest the jQuery w/in it (that may be what you meant, but I thought I’d add that for clarity):

      <!--[if IE 7]>
        <html class="no-js lt-ie9 lt-ie8" > 
        if($('html').hasClass('lt-ie8')) { //Do IE7 specific stuff }
      <![endif]-->"
      
    • User Avatar
      Kim Wilson
      Permalink to comment#

      Thanks Melanie! Just what I was looking for!

  7. User Avatar
    Steve Oldner (@soldner)
    Permalink to comment#

    Using Jquery mobile but need to determine if users are using their desktop. IE is standard here and company’s default is no outside library so it looks bad. This is only for mobile portal – we have a full desktop portal already. Question is 2 parts and looking for best practices.
    1. How do I detect big screen?
    2. How can I tell if they allow JQuery?

    Thanks!

  8. User Avatar
    Seetpal singh
    Permalink to comment#

    hi,

    below code works perfectly

    var isMSIE = /*@cc_on!@*/1;
    window.onload   =   function(){
        if (!isMSIE) {
            // do IE-specific things
            alert('saf');
            var bod =   document.getElementsByTagName('body');
            bod[0].setAttribute('class','IE');
        } 
    }
    

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag