I'm trying to use a script into a HTML (based on the solution suggested by "skara9" in this post and try to adapt it for my needs, but can't find how to do it, it doesn't work.
The basic has 2 functions:
the 1st one:
function fnBrowserDetect()and the 2nd function under the 1st one :
function redirect()
But when I tried to execute it, only the 1st function worked. And I didn't found out how to call the 2 functions, so I decided to try to merge them. But it doesn't work either. What am I doing wrong?
So here is my code now:
<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>
It would be too long to explain why I have to do that (the redirected links are only here as distinctive examples) but the thing is I am looking for a simple way to DETECT the browser used by the person and REDIRECT him to the correct place according the browser that person is using.
**HERE IS WHAT I ALREADY TRIED: **
First I tried to execute the script as it was as the beginning and for this, and looked for a way to call multiple functions. According some posts I read here, I read it is possible to do that with eitheir window.onload = function() OR window.addEventListener('load', ...) .
But (i'm sorry) it wasn't explained clearly enough for me...
So after that, I decided to try to merge my 2 functions based on a logic but it still doesn't work. I changed this part in particular:
REMOVED:
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");
REPLACED BY:
browserName="No browser detection";
}
return browserName;
if(browserName = "chrome"){
window.location.replace("https//google-chrome-website");
So I'm looking for someone to help me to correct it please.
Hoverwise, if you think it's better to call 2 functions rather than merge my code like I did:
Please specify me how and where to call the window.onload = function() instead of <body onload="function()"> . Do I need to add a div or a script like :
<script>
window.onload = function();
</script>
And also tell me how to call my 2 functions in specific order (function1 then function2).
Maybe it's better to use an external script.js ?
Just tell me. Thank you.
(Please avoid to suggest me other similar post because I already passed many days on them but they weren't clear enough for me ; I'm still learning and need clear explanations. Thx)
You have a lot of back and forth with what you removed and tried. So I just took the base functionality and cleaned it up.
First, your return stops everything from happening after that.
My answer returns two pieces of the data: The name of the browser and the GOTO URL.
Instead of if statements, I just a switch that works best for basic if/else statements like you had.
From there you can choose what you want to do with that data.