I have a slight understanding of this works, however, not enough to understand what's going on here. I believe it's creating a conflict and rendering one useless.
I'm trying to replace {{aff_id}} with a URL parameter.
This is in ClickFunnels FYI and it's being used in a email submit form.
Here is the code:
<script>
$(document).ready(function () {
var replaceString = "organic";
var replaceString2 = "null";
var urlTerm2 = getURLParameter("aff_id");
var urlTerm = getURLParameter("tid");
if (urlTerm.trim().length > 0 && urlTerm != "null") {
replaceString = decodeURIComponent(urlTerm).replace(/\+/g,' ');
}
$("body").html($("body").html().replace(/\{\{tid\}\}/g,replaceString));
if (urlTerm2.trim().length > 0 && urlTerm2 != "null") {
replaceString2 = decodeURIComponent(urlTerm2).replace(/\+/g,' ');
}
$("body").html($("body").html().replace(/\{\{aff_id\}\}/g,replaceString2));
});
</script>
I'm sure this is something super simple that I'm missing. NAy help would be greatly appreciated!!!
Thank you.
My only real critique here is the use of non-descriptive variable names. Like replaceString and replaceString2 are not very helpful. For the do not repeat yourself principle, it might be better to extract the logic as a function. Also, it seems like the regular expressions are more cryptic than actually helpful here. I might write something like this instead.
const replaceTemplate = function (tag, replacment) {
$("body")
.html()
.replaceAll("{{" + tag + "}}", replacment.replaceAll("+", " "));
};
$(document).ready(function () {
var affId = getURLParameter("aff_id");
var tid = getURLParameter("tid");
replaceTemplate("aff_id", affId ? affId : "null");
replaceTemplate("tid", tid ? tid : "organic");
});