Having this url : mysite.com/index.html?r=124567
Would like to extract 4 numbers of variable r and redirect with javascript to :
- if first 1 digit begins with 2 = https://radio20.servidorderadio.net:2020/public/8020
- if first 1 digits begins with 1 = https://radio12.servidorderadio.net/cp/widgets/player/dj/?p=4567
the code that chatgpt gave me was this, but it does not work
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Redirect</title>
<script>
function redirect() {
const urlParams = new URLSearchParams(window.location.search);
const radio = urlParams.get("r");
if (radio.substring(0, 1) === "2") {
const newUrl = `https://radio${radio.substring(0, 2)}.servidorderadio.net:2020/public/${radio.substring(2, 4)}`;
window.location.replace(newUrl);
} else {
const newUrl = `https://radio${radio.substring(0, 2)}.servidorderadio.net/cp/widgets/player/dj/?p=${radio.substring(2, 4)}`;
window.location.replace(newUrl);
}
}
window.onload = redirect;
</script>
</head>
<body>
</body>
</html>