Skip to main content
CSS-Tricks
  • Articles
  • Videos
  • Almanac
  • Newsletter
  • Guides
  • DigitalOcean
  • DO Community
Search
Code Snippets → JavaScript → Test for Internet Explorer in JavaScript

Test for Internet Explorer in JavaScript

Avatar of Chris Coyier
Chris Coyier on Oct 8, 2010
var isMSIE = /*@[email protected]*/0;

if (isMSIE) {
  // do IE-specific things
} else {
  // do non IE-specific things
}
Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

Comments

  1. Rodney Reid
    Permalink to comment# December 20, 2010


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

    Reply
    • Max
      Permalink to comment# August 3, 2011

      doesn’t work ie 9

  2. Ruben Vreeken
    Permalink to comment# February 2, 2011

    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.

    Reply
    • Stacey
      Permalink to comment# January 14, 2012

      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]-->

    • vikas
      Permalink to comment# November 4, 2012

      @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. Lonesome Walker
    Permalink to comment# November 14, 2011

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

    Thanks, helped me a lot!

    Reply
  4. eman on
    Permalink to comment# October 18, 2012

    Oh the pains of IE – thanks

    Reply
  5. jonathan marzullo
    Permalink to comment# January 15, 2013

    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!

    Reply
  6. Melanie
    Permalink to comment# March 12, 2013

    If you’re using jQuery…

    
    
    
    if($('html').hasClass('lt-ie8')) { //Do IE7 specific stuff }
    
    
    Reply
    • Melanie
      Permalink to comment# March 12, 2013

      … in combination with:

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

      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]-->"
      
    • Kim Wilson
      Permalink to comment# August 5, 2014

      Thanks Melanie! Just what I was looking for!

  7. Steve Oldner (@soldner)
    Permalink to comment# April 10, 2013

    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!

    Reply
  8. Seetpal singh
    Permalink to comment# August 5, 2014

    hi,

    below code works perfectly

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CSS-Tricks is powered by DigitalOcean.

Keep up to date on web dev

with our hand-crafted newsletter

DigitalOcean
  • DigitalOcean
  • DigitalOcean Community
  • About DigitalOcean
  • Legal
  • Free Credit Offer
CSS-Tricks
  • Email
  • Guest Writing
  • Book
  • Advertising
Follow
  • Mastodon
  • Twitter
  • Instagram
  • YouTube
  • CodePen
  • iTunes
  • RSS
Back to Top