【问题标题】:Redirect to specific page according to the browser根据浏览器重定向到特定页面
【发布时间】:2016-10-18 00:37:56
【问题描述】:

大家好,我希望我的页面在 Chrome 浏览器中打开时重定向为“GOOGLE”,在 Firefox 中打开时重定向为“YOUTUBE”。
我已尝试通过返回浏览器名称的navigator.appCodeName 检测用户的浏览器,但它不起作用,它仅在 mozilla 中工作。

&lt;body onload="myFunction()"&gt; <script> function myFunction() { var x = navigator.appCodeName; if(x.indexOf('Chrome')>=0){ window.location.href="http://youtube.com"; }else{ window.location.href="http://google.com"; } }
谁能帮我解决这个问题?

【问题讨论】:

    标签: javascript html url redirect


    【解决方案1】:
    • 使用 window.location.replace("---url here---")
    • 出于兼容性原因,所有现代浏览器都返回“Mozilla”。
    • 所以我在这里使用navigator.userAgent返回浏览器发送给服务器的用户代理头。

    • 如果是 mozilla 则返回 Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0

    • Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36 如果是 chrome。

      <body onload="myFunction()"> <script> function myFunction() { var x = navigator.userAgent; if(x.indexOf('Chrome')>=0){ window.location.replace("https://google.com") console.log("hello chrome"); }else{ window.location.replace("https://youtube.com"); console.log("hello mozilla"); } }
      希望对您有所帮助

    【讨论】:

    • 谢谢你说的对,但是使用href有什么问题吗?
    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多