简体   繁体   中英

Passing UTM Parameters using Javascript to Button URL

Here I have a code that is supposed to take the utm parameters in the url and pass it to all buttons who have a specific href domain. However this code runs a compile error on Line 12 Character 27 which is the line with the for-loop and I do not understand why. Can someone please help. Thank you

<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>

Seems like you have a } too much and your for loop is invalid

(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
}());

Also your tempLink and tempParts variables aren't defined anywhere.

你想让它说一些

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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