How To Detect The Edge Browser And Redirect To Another Page In JavaScript

There will likely be cases where you have sites that were built for IE but are not caught up to Edge yet. What do you do in the interim? One idea that I applied on a legacy site was to inform take the Edge user to a specific page. You can always do a multitude of things when dealing with how to address the user (alerts, redirects, updates to labels on pages, etc.). I just want to show an easy way to detect and redirect.

The first thing you need to know is the Edge Browser’s User Agent String:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600

You can take advantage of the navigator.userAgent property in JavaScript and do a quick check of it. In this example I check for “Edge” and redirect if it is contained in the string.

Place the following in a script tag inside of the head section of your html:

<script type="text/javascript">
var ver = navigator.userAgent;
if (ver.indexOf('Edge') !== -1)
{
window.location = "https://www.afakewebsite.com/edgemessage.html";
}
else
{
// Not Edge, do nothing
}
</script>

Hope it helps.

Advertisements

2 thoughts on “How To Detect The Edge Browser And Redirect To Another Page In JavaScript

  1. Thanks, I was looking for this it was really helpful. the if IE code block doesn’t work for internet explorer version 10+ and Edge, so this fixes the problem with edge, but do you have any idea how to detect internet explorer versions 10 and above? I searched for the user agent string for internet explorer the common word in all of them is “windows” so I tried blocking that but it also blocks chrome. we want to redirect all users using internet explorer and edge to a page that asks them to use chrome or firefox for now as we don’t have the resources to fix the bugs at internet explorer at the moment.

    Thanks a lot.

    Like

    • Perhaps you could branch off of something like this to better meet your needs


      /**
      * detect IE
      * returns version of IE or false, if browser is not Internet Explorer
      */
      function detectIE() {
      var ua = window.navigator.userAgent;

      // Test values; Uncomment to check result …

      // 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';

      // IE 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 (IE 12+)
      // 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';

      var msie = ua.indexOf('MSIE ');
      if (msie > 0) {
      // IE 10 or older => return version number
      return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
      }

      var trident = ua.indexOf('Trident/');
      if (trident > 0) {
      // IE 11 => return version number
      var rv = ua.indexOf('rv:');
      return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
      }

      var edge = ua.indexOf('Edge/');
      if (edge > 0) {
      // Edge (IE 12+) => return version number
      return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
      }

      // other browser
      return false;
      }

      Like

Comments are closed.