Home > Back-end >  Auto detect the browser and redirect
Auto detect the browser and redirect

Time:01-11

I'm still beginner. Is there a way to do this kind of redirect? As an example, I made the redirect link of the website and that website should be open in Microsoft Edge. If I open that file in IE or Chrome, it redirect to MS Edge. But if i open that redirect file in Ms Edge, it just showing Redirecting.

Is there a way to auto detect? What I mean is, I open that redirect file in IE and Chrome, it has to redirect and if I open in Ms Edge, it doesn't need to do redirect and just open the website on Edge by itself. Is there any resources for codes?

CodePudding user response:

You can detect User-Agent to check browser version. If Edge, doing nothing; if IE and Chrome, using microsoft-edge: URI scheme to open link in Edge.

Sample code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div>test page</div>
    <script>
        function detectbrowser() {
            var ua = window.navigator.userAgent;
            if (ua.indexOf("Edg") > 0) {  //Edge
                return;
            }
            else if (ua.indexOf("Trident") > 0 || ua.indexOf("Chrome") > 0) {  //IE or Chrome
                window.location = "microsoft-edge:"   window.location.href;
            }
        }
        detectbrowser();
    </script>
</body>
</html>
  •  Tags:  
  • Related