使用JavaScript将UTM参数传递到按钮URL



在这里,我有一个代码,该代码应该在URL中使用UTM参数,并将其传递给具有特定HREF域的所有按钮。但是,此代码在第12行字符27上运行了编译错误,这是与前循环的行,我不明白为什么。有人可以帮忙吗?谢谢你

<script type="text/javascript">
(function() {
var utmInheritingDomain = "https://www.motorstore.com/amr_en/checkout/", 
utmRegExp = /(&|?)utm_[A-Za-z]+=[A-Za-z0-9]+/gi,
links = document.getElementsByTagName("a"),
utms = [
"utm_medium={{utm_medium}}", 
"utm_source={{utm_source}}", 
"utm_campaign={{utm_campaign}}", 
];
for (var index = 0; index 0) { 
tempLink = tempLink.replace(utmRegExp, "");
tempParts = tempLink.split("#");
if (tempParts[0].indexOf("?") < 0 ) {
tempParts[0] += "?" + utms.join("&"); 
} else {
tempParts[0] += "&" + utms.join("&");
}
tempLink = tempParts.join("#");
}
links[index].href = tempLink;
}
}());
</script>

您希望它说

for (var index = 0; index < links.length; index++) { 

似乎您的 }太多了,而您的循环无效

(function() {
    var utmInheritingDomain = "https://www.motorstore.com/amr_en/checkout/", 
    utmRegExp = /(&|?)utm_[A-Za-z]+=[A-Za-z0-9]+/gi,
    links = document.getElementsByTagName("a"),
    utms = [
        "utm_medium={{utm_medium}}", 
        "utm_source={{utm_source}}", 
        "utm_campaign={{utm_campaign}}", 
    ];
    // Invalid for loop
    // for (var index = 0; index 0) {
    for (var index = 0; index < links.length; index++ 
        tempLink = tempLink.replace(utmRegExp, "");
        tempParts = tempLink.split("#");
        if (tempParts[0].indexOf("?") < 0 ) {
            tempParts[0] += "?" + utms.join("&"); 
        } else {
            tempParts[0] += "&" + utms.join("&");
        }
        tempLink = tempParts.join("#");
    }
    links[index].href = tempLink;
    // } <- this one
}());

您的tempLinktempParts变量尚未在任何地方定义。

相关内容

  • 没有找到相关文章

最新更新