total newbie here, first try in getting a script to work for an a/b-test.
I have one element, ‘prodBanEle’, that I want to be seen only if up to three other URLs (topBanUrl1, topBanUrl2, topBanUrl3) in another element also present on the page is a part of the current URL.
So for example: if you visit "mywebsite.com/category-grey-pants/product-light-grey-shorts/", ‘prodBanEle’ should only be visible if "mywebsite.com/category-grey-pants/" is one of the three URLs.
If the current URL instead is "mywebsite.com/category-shorts/product-light-grey-shorts/", ‘prodBanEle’ should not be visible.
This is what I’ve tried to get to work, the thing I’m most uncertain of is indexOf actually works with variables, or only strings. But I’m sure there are a bunch of other mistakes in there…
$(function(){
var prodBanEle = document.getElementById("addToCardButtonDiv").getElementsByClassName("banner ")[0];
var topBanUrl1 = document.getElementById("top_banner_url_1");
var topBanUrl2 = document.getElementById("top_banner_url_2");
var topBanUrl3 = document.getElementById("top_banner_url_3");
if (window.location.href.indexOf(topBanUrl1)){
prodBanEle.style.display = "block";
}
else if (window.location.href.indexOf(topBanUrl2)){
prodBanEle.style.display = "block";
}
else if (window.location.href.indexOf(topBanUrl3)){
prodBanEle.style.display = "block";
}
else { prodBanEle.style.display = "none";
}
}
);
Are there any obvious mistakes the you recognize immediately, or should it work?
Thank you for your help!
2
Answers
I can answer better if I see some HTML
One possible mistake is
topBanUrl1, topBanUrl2, and topBanUrl3 should be strings not elements since you use indexOf.
I don’t know what the html is, but instead of
it would be this assuming that top_banner_url_1, top_banner_url_2, top_banner_url_3 are links