6

I just want to target only IE10 and below versions IE browsers. Please help me for this. I have tried below code

var userAgent = navigator.userAgent;
            var regexIe8 = new RegExp("Trident(\/4.0)|(Trident\/5.0)");
            if (regexIe8.test(userAgent)) {
                alert("test1");
            }
            else {
                alert("test2");
            }
priya
  • 151
  • 1
  • 6
  • Okay, so you've tried this code... what happened when you tried it? I guess it didn't work, but what symtoms did you get? Have you tried to debug it? This technique should work, but there are plenty of other ways of detecting IE; have you tried any of them? Different techniques are better suited to different use-cases: What is your use-case for needing to detect IE? For many use-cases, version detection isn't necessary at all; you may well find that feature detection is a better match for your needs. – Simba Jan 13 '16 at 10:38
  • I just want to show a notification bar in IE 10 and below browsers. While i used above code it doesn't throw any error. Simply output doesn't provide. I have used below code also. if (navigator.userAgent.indexOf('MSIE') != -1) var detectIEregexp = /MSIE (\d+\.\d+);/ else var detectIEregexp = /Trident.*rv[ :]*(\d+\.\d+)/ if (detectIEregexp.test(navigator.userAgent)) { var ieversion = new Number(RegExp.$1) if (ieversion <= 10) $("#ie-notification").css("display", "block");But this code displays my notification in all browsers including chrome and firefox. – priya Jan 13 '16 at 12:15
  • 1
    why don't you use conditional comments? – Daniel A. White Jan 13 '16 at 14:22
  • Yep, I agree. Conditional comments seem like a more appropriate technique for your use-case. – Simba Jan 13 '16 at 14:48
  • 2
    Conditional comments don't work here as IE10 dropped support for them. – ajbeaven May 23 '18 at 22:37

3 Answers3

12

In Internet Explorer 11, Microsoft took out the "MSIE", so it now looks like this:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

So you could just check for the presence of "MSIE" to tell you if it's an old version.

if (navigator.userAgent.indexOf("MSIE") >= 0) {
    document.write("You are using an unsupported version of Internet Explorer. Please upgrade to Internet Explorer 11 or use a different web browser.");
}

Just note that if Internet Explorer is in compatibility view, and you haven't specified an X-UA-Compatible header, it will identify itself as IE 7 and act like it too.

libertyernie
  • 2,590
  • 1
  • 17
  • 13
3

Using conditional comments instead of Javascript detection:

<!--[if IE]>
<div class='old-browser'>You are running an un-supported version of Internet Explorer. Please upgrade</div>
<![endif]-->

Just stick that directly in your page where you want the notification to appear. No Javascript required (although you can, of course, use JS here if you want to).

Note that IE10 no longer supports conditional comments, so you just need to check [if IE] to catch all versions of IE up to IE9.

averell
  • 3,762
  • 2
  • 21
  • 28
Simba
  • 4,952
  • 3
  • 19
  • 29
  • 5
    Referring to the official docs: https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx _"As of Internet Explorer 10, conditional comments are no longer supported by standards mode."_ This means that conditional comments only work through IE9. 10 and up ignore them. – Arthur G N McLean Jun 30 '17 at 19:16
0

Here is a great CodePen with a complete solution for all versions of IE and Edge:

https://codepen.io/gapcode/pen/vEJNZN

From the linke, here are examples of the various values of window.navigator.userAgent:

  // IE 10
  // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';

  // IE 11
  // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';

  // Edge 12 (Spartan)
  // ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';

  // Edge 13
  // ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
theUtherSide
  • 3,338
  • 4
  • 36
  • 35