Javascript 根据浏览器重定向到特定页面

Javascript 根据浏览器重定向到特定页面,javascript,html,url,redirect,Javascript,Html,Url,Redirect,大家好,我希望我的页面在Chrome浏览器中打开时重定向为“GOOGLE”,在firefox中打开时重定向为“YOUTUBE”。 我试图通过返回浏览器名称的navigator.appCodeName来检测用户的浏览器,但它不起作用,它只在mozilla中工作。 函数myFunction(){ var x=navigator.appCodeName; 如果(x.indexOf('Chrome')>=0){ window.location.href=”http://youtube.com"; }

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


函数myFunction(){
var x=navigator.appCodeName;
如果(x.indexOf('Chrome')>=0){
window.location.href=”http://youtube.com";
}否则{
window.location.href=”http://google.com";
}
}

有谁能帮我解决这个问题吗

  • 使用window.location.replace(“--url here--”)
  • 出于兼容性原因,所有现代浏览器都返回“Mozilla”
  • 因此,Iam使用here
    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,如Gecko)Chrome/51.0.2704.84 Safari/537.36(如果是Chrome)

    
    函数myFunction(){
    var x=navigator.userAgent;
    如果(x.indexOf('Chrome')>=0){
    window.location.replace(“https://google.com")
    log(“hello chrome”);
    }否则{
    window.location.replace(“https://youtube.com");
    log(“你好,mozilla”);
    }
    }

    希望这有帮助

      • 使用window.location.replace(“--url here--”)
      • 出于兼容性原因,所有现代浏览器都返回“Mozilla”
      • 因此,Iam使用here
        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,如Gecko)Chrome/51.0.2704.84 Safari/537.36(如果是Chrome)

        
        函数myFunction(){
        var x=navigator.userAgent;
        如果(x.indexOf('Chrome')>=0){
        window.location.replace(“https://google.com")
        log(“hello chrome”);
        }否则{
        window.location.replace(“https://youtube.com");
        log(“你好,mozilla”);
        }
        }

        希望这有帮助


      谢谢你,这是正确的,但是使用href有什么错。?谢谢你,这是正确的,但是使用href有什么错。?