Detect Internet Explorer

<script type="text/javascript">

if (https://cdn.css-tricks.com/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion>=8)
  document.write("You're using IE8 or above")
 else if (ieversion>=7)
  document.write("You're using IE7.x")
 else if (ieversion>=6)
  document.write("You're using IE6.x")
 else if (ieversion>=5)
  document.write("You're using IE5.x")
}
else
 document.write("n/a")

</script>

Reference

Comments

  1. User Avatar
    Luca
    Permalink to comment#

    Sniffing user agent is not a “safe” solution.
    I prefer the Dean Edwards one:


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

    (More here: http://dean.edwards.name/weblog/2007/03/sniff/ )

  2. User Avatar
    CaTaHaC
    Permalink to comment#

    Or simply use what has already been built and not rewrite it:
    if ($.browser.ie) { … }

  3. User Avatar
    Mahroof Ali

    using conditional comments is a lot easier, is’nt it?

    • User Avatar
      Michael
      Permalink to comment#

      The problem with conditional comments is that IE 10 no longer supports them

    • User Avatar
      ajay
      Permalink to comment#

      but conditional comments is not working in ie 10

  4. User Avatar
    Prashant

    this code does not work for IE11, can you modify the MSIE to detect IE11 also, Please help.

  5. User Avatar
    Nicola
    Permalink to comment#

    You can try this:

    if(document.documentMode) {
      document.documentElement.className+=' ie'+document.documentMode;
    }
    
  6. User Avatar
    Nicola Zanon
    Permalink to comment#

    Np! :)

  7. User Avatar
    Damian
    Permalink to comment#

    Nice work Nicola. I didn’t know this was a specific IE property: http://www.w3schools.com/jsref/prop_doc_documentmode.asp

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.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag