将 Safari 重定向到 Chrome

Dar*_*a G 2 html javascript

我正在尝试将我的移动网络应用程序的用户重定向到使用 Chrome 而不是 Safari。我尝试使用以下方法:

<script>
javascript:location.href="googlechrome"+location.href.substring(4);
</script>
Run Code Online (Sandbox Code Playgroud)

然而,这会不断地在 Chrome 中循环打开标签。你知道我怎么能成功地做到这一点吗?

干杯,达拉

mac*_*115 5

这将导致每次加载网页时都会打开页面,无论您使用的是 Safari 还是 Chrome。这也是非常糟糕的用户体验,只是将用户转发到另一个浏览器而无需他们的输入。

最好有一些方法让用户在 Chrome 中打开您的网站,并解释为什么需要它。

还有其他方案https和回调:https : //developer.chrome.com/multidevice/ios/links#uri_schemes

<p>This webapp is best viewed in Google Chrome</p>
<button type="button" onclick="openInChrome()">Open in Chrome</button>
<script>
    var openInChrome = function() {
        if (/^((?!Chrome).)*(Safari)+.*$/.test(navigator.userAgent)) {
            var protocol = location.href.substring(0,location.href.indexOf(':'));
            var url = location.href.substring(location.href.indexOf(':'));

            if (protocol === "http") {
                location.href = "googlechrome" + url;
            }
            else {
                location.href = "googlechromes" + url;
            }
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

编辑:

添加了一项检查以验证它们是否在 Safari 中。