0

I use the code below to detect browser version to advice user use higher version IE. But it works in IE11 not correctly.

// forbid ie10 below visit
if(navigator.userAgent.match(/msie\s[5-9]/i))
{
 ... // show a advice page to change ie version
}

I find if the web publish to server, user will see the ie version change advice.

User agent string : Internet Explorer 11 (Default)

Document mode 5(Default)

When I change Document mode to 11, the web work well.

I also use the code below in html head, but it doesn't work.

<meta http-equiv="X-UA-Compatible" content="IE=11" />
Jun Yu
  • 375
  • 1
  • 5
  • 21
  • check if this helps . https://stackoverflow.com/questions/21825157/internet-explorer-11-detection – A Paul Dec 25 '20 at 08:42
  • A simpler and more flexible idea is to feature detect. Rather than check specifically for IE11, feature detect for something you know it doesn't support, eg. `WeakSet`. The advantage is it will also work for other outdated browsers, like very old versions of Chrome that really should be updated too. :) – Keith Dec 25 '20 at 08:45
  • @Keith I don't want to use feature detect, because my problem is the table view is bad in iE10 and below, and my users has all have IE11 but not use 11 as default document mode. – Jun Yu Dec 28 '20 at 01:07

1 Answers1

1

It looks like you want to inform your site user to use the IE 11 browser if they are using an older version of the IE browser.

Sample code:

<!doctype html>
<html>
<head>
<title>
Test to detect IE browser
</title>

</head>
<body >
<div id="info"></div><br>
<h2>Test Page...</h2>

<script>
function Detect_IE() {
           var ua = window.navigator.userAgent;
         
           var msie = ua.indexOf('MSIE ');
           if (msie > 0) {
            
             return "IE " + parseInt( ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
           }
         
           var trident = ua.indexOf('Trident/');
           if (trident > 0) {
            
             var rv = ua.indexOf('rv:');
             return "IE " + parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
           }

           // other browser
           return "false";
         }
         var result=Detect_IE();
         if (result=="false")
         {
            document.getElementById("info").innerHTML +="<h2>Not IE, any other browser....</h2>";
         }
     else if (result=="IE 11")
         {
            document.getElementById("info").innerHTML += "<h2>Dear user you are using " + result + ".</h2>";
         }
         else
         {
            document.getElementById("info").innerHTML += "<h2>Dear user you are using " + result + " This browser is outdated and not supported by this site. Kindly use supported browser...</h2>";
         }
</script>
</body>
</html>

Output:

enter image description here

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
  • Thanks! It works succefully. But why my code can't work? Does IE 11 has a mise userAgent when it set 5 or other versions as default document mode? Can I just to detect document mode and force document mode? – Jun Yu Dec 28 '20 at 01:09
  • It looks like there is some issue with the pattern you are trying to match in the `IF` condition. If you try to print it in the console then you can notice that it is returning NULL. – Deepak-MSFT Dec 28 '20 at 05:30
  • Thanks. I find that it return an object, and I mistake it as boolean. – Jun Yu Dec 28 '20 at 07:06
  • Hi, I find that if I use `index` to check, when I use IE11 with IE11 user Agent but IE5 document mode, it will be detect as IE5. Why the document mode affect the user Agent? – Jun Yu Dec 28 '20 at 08:47
  • Whether you change the **Document mode** or **User-agent string** the web page will get rerender with every change. If we talk about your situation first, you have set the User-agent string. So web page got rendered based on the User-agent string. then you change the Document-mode. So web page again got rendered based on the Document-mode. – Deepak-MSFT Dec 28 '20 at 09:27
  • Hi, thanks for explantion. Do you mean we can't distinct the **User-agent** and **Document mode** by js browser checking? – Jun Yu Dec 28 '20 at 09:31
  • We can distinguish the User-agent and Document mode by JS. To get the user-agent we can use `navigator.userAgent;` , to get the document-mode we can use `document.documentMode;` Please see the working example [here](https://justpaste.it/8xc18). – Deepak-MSFT Dec 29 '20 at 02:33