我正在尝试将脚本用于 HTML(基于 this post 中“skara9”建议的解决方案,并尝试根据我的需要进行调整,但找不到如何做,它没有工作。
基本有2个功能:
第一个:
function fnBrowserDetect()
和第一个功能下的第二个功能:
function redirect()
但是当我尝试执行它时,只有第一个功能起作用。而且我没有找到如何调用这 2 个函数,所以我决定尝试合并它们。但它也不起作用。 我做错了什么?
现在这是我的代码:
<body onload="fnBrowserDetect()">
<script>
function fnBrowserDetect(){
let userAgent = navigator.userAgent;
let browserName;
if(userAgent.match(/chrome|chromium|brave|crios/i)){
browserName = "chrome";
}else if(userAgent.match(/firefox|fxios/i)){
browserName = "firefox";
}else if(userAgent.match(/opr\//i)){
browserName = "opera";
} else if(userAgent.match(/edg/i)){
browserName = "edge";
}else{
browserName="No browser detection";
}
return browserName;
if(browserName = "chrome"){
window.location.replace("https//specific-just-working.better-google-chrome-page/");
} else if(browserName = "firefox"){
window.location.replace("https://specific-just-working.better-firefox-page/");
} else if(browserName = "edge"){
window.location.replace("https://specific-just-working.better-edge-microsoft-page/");
} else if(browserName = "opera"){
window.location.replace("https://specific-just-working.better-opera-page/");
}
}
</script>
</body>
解释为什么我必须这样做太长了(重定向链接在这里只是作为独特的例子)但问题是我正在寻找一种简单的方法来检测该人使用的浏览器并将他重定向到正确的根据该人使用的浏览器放置。
**这是我已经尝试过的:**
首先,我尝试按原样执行脚本,为此,我寻找一种调用多个函数的方法。根据我在这里阅读的一些帖子,我读到可以用他们的
window.onload = function()
或window.addEventListener('load', ...)
来做到这一点。
但是(我很抱歉)对我来说解释得不够清楚......
所以在那之后,我决定尝试根据逻辑合并我的 2 个函数,但它仍然不起作用。 我特别更改了这部分:
删除:
browserName="No browser detection";
}
document.querySelector("h1").innerText="You are using "+ browserName +" browser";
return browserName;
}
function redirect(){
if(fnBrowserDetect() != "firefox"){
window.location.replace("https://www.google.com");
替换为:
browserName="No browser detection";
}
return browserName;
if(browserName = "chrome"){
window.location.replace("https//google-chrome-website");
所以我正在找人帮我改正它。
Hoverwise,如果你认为调用 2 个函数比像我那样合并我的代码更好:
请指定我如何以及在何处调用
window.onload = function()
而不是 <body onload="function()">
。我需要添加一个 div 还是一个脚本,比如 :
<script>
window.onload = function();
</script>
还要告诉我如何按特定顺序调用我的 2 个函数(function1 然后 function2)。
也许使用外部 script.js 会更好?
告诉我。谢谢。
(请避免向我推荐其他类似的帖子,因为我已经过了很多天,但对我来说还不够清楚;我还在学习,需要明确的解释。谢谢)
你对你删除和尝试的东西有很多来回。所以我只是采用了基本功能并对其进行了清理。
首先,你的回归阻止了之后发生的一切。
我的回答返回两条数据:浏览器名称和 GOTO URL。
我只是一个最适合基本 if/else 语句的开关,而不是 if 语句。
从那里你可以选择你想用这些数据做什么。
function fnBrowserDetect() {
let userAgent = navigator.userAgent;
let returns = {
"browser": "",
"url": ""
};
if (userAgent.match(/chrome|chromium|brave|crios/i)) {
returns["browser"] = "chrome";
} else if (userAgent.match(/firefox|fxios/i)) {
returns["browser"] = "firefox";
} else if (userAgent.match(/opr\//i)) {
returns["browser"] = "opera";
} else if (userAgent.match(/edg/i)) {
returns["browser"] = "edge";
} else {
returns["browser"] = "No browser detection";
}
switch (returns["browser"]) {
case "chrome":
returns["url"] = "https //specific-just-working.better-google-chrome-page/";
break;
case "firefox":
returns["url"] = "https://specific-just-working.better-firefox-page/";
break;
case "edge":
returns["url"] = "https://specific-just-working.better-edge-microsoft-page/";
break;
case "opera":
returns["url"] = "https://specific-just-working.better-opera-page/";
break;
default:
returns["url"] = "https://google.com/";
break;
}
return returns;
}
let browser = fnBrowserDetect();
document.querySelector("#browser").innerText = browser.browser;
document.querySelector("#url").innerText = browser.url;
//location.href = browser.url;
<div id="browser"></div>
<div id="url"></div>