繁体   English   中英

我如何更改将根据 document.referrer 加载的 URL

[英]how I change the URL that will be loaded according to document.referrer

我需要根据他来自的网站将用户定向到某个 URL。

<script>
    function abc(){
    var ref = document.referrer;
    if (ref = /site1/) {
    window.location.href = "https://www.site1.wow";
    } else if (ref = /site2/) {
    window.location.href = "https://www.site2.wow";
    } else {
    window.location.href = "https://www.site.wow";
 }
}
</script>

新版本,但仍然无法正常工作,返回未定义

function getParam(_parameter){
const parameters = new URLSearchParams(window.location.search);
if (parameters.get("utm_medium") && parameters.get("utm_medium") === "facebook") {
window.location.href = 'https://link1.wow', '_blank';
} else if (parameters.get("gclid=") && parameters.get("gclid=") === "gclid=") {
window.location.href = 'https://link2.wow', '_blank';
} else {
window.location.href = 'https://link.wow', '_blank';

} }

你必须以不同的方式做到这一点:

const parameters = new URLSearchParams(window.location.search);

这将从实际站点获取所有参数。 现在您必须测试它是否具有您正在寻找的参数,例如:

if(parameters.get("utm_source") && parameters.get("utm_source") === "abc"){
 console.log("It has this parameter")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM