Javascript 将Safari重定向到Chrome
我正在尝试将我的移动webapp的用户重定向到使用Chrome而不是Safari。我尝试使用以下方法:Javascript 将Safari重定向到Chrome,javascript,html,Javascript,Html,我正在尝试将我的移动webapp的用户重定向到使用Chrome而不是Safari。我尝试使用以下方法: <script> javascript:location.href="googlechrome"+location.href.substring(4); </script> javascript:location.href=“googlechrome”+location.href.substring(4); 然而,这会在一个循环中不断打开Chrome中的选项卡。你
<script>
javascript:location.href="googlechrome"+location.href.substring(4);
</script>
javascript:location.href=“googlechrome”+location.href.substring(4);
然而,这会在一个循环中不断打开Chrome中的选项卡。你知道我怎样才能成功地做到这一点吗
干杯,
Dara这将导致每次加载网页时都打开网页,无论您是在Safari还是Chrome中。这也是非常糟糕的用户体验,只是将用户转发到另一个浏览器而没有他们的输入 最好能让用户用Chrome打开你的网站,并解释为什么需要它
https
和回调还有其他方案:
此Web应用程序最好在Google Chrome中查看
镀铬
var openInChrome=函数(){
if(/^((?!Chrome)。*(Safari)+.*$/.test(navigator.userAgent)){
var协议=location.href.substring(0,location.href.indexOf(':');
var url=location.href.substring(location.href.indexOf(':');
如果(协议==“http”){
location.href=“googlechrome”+url;
}
否则{
location.href=“googlechromes”+url;
}
}
}
编辑:
添加了一个检查来验证它们是否在Safari中。好吧,原因很明显;Chrome也被指示打开Chrome。您只需要一个userAgent条件
if (navigator.userAgent.indexOf("CriOS") == -1) {
location.href="googlechrome"+location.href.substring(4);
}
我会继续我的标准咆哮关于用户代理检查是坏的,但我相信你所说的这是一个私人网络应用。由于iOS不允许您更改默认浏览器,我想这是一个公平的解决办法。您当时期望什么?每次加载页面时,您都会设置href,这肯定会创建一个无限循环。对不起,我应该指定更多,这是一个私有的webapp,所有用户都希望在Chrome中使用它,而不是Safari。这就是为什么我尝试自动设置它的原因。正是因为如此,由于iOS迫使你使用Safari,我才试图找到一个解决办法。不幸的是,这个脚本仍然会引起一个循环:(@DaraG抱歉;我还以为Chrome是iOS safari中的签名字符串。我刚刚编辑了我的帖子。现在就试试看(我没有iPhone要测试,所以它在你身上)现在很好用。非常感谢!:)我真的很感激。
if (navigator.userAgent.indexOf("CriOS") == -1) {
location.href="googlechrome"+location.href.substring(4);
}