【发布时间】:2021-09-21 12:49:12
【问题描述】:
我想自动为我网站上的 URL 添加会员标识符。 例如,我有外部 URL,例如:
https://www.adairs.com.au/
https://bedthreads.com.au/products/caitlin-robson-footed-bowl
我的会员标识符因零售商而异,例如:
adairs.com.au: https://t.example.com/12345/t/54321?Url=
bedthreads.com.au: https://t.example.com/12121/t/21212?Url=
我需要在点击形成时将这些自动添加到外部 URL 的开头:
https://t.example.com/12345/t/54321?Url=https://www.adairs.com.au/
https://t.example.com/12121/t/21212?Url=https://bedthreads.com.au/products/caitlin-robson-footed-bowl
我找到的最接近的解决方案是here,但是我在 java 方面不是很有经验并且无法使其工作。理想情况下,我还想在未来为其他零售店添加其他自动会员标识符。
另外,是否可以在 PHP 中添加它? 任何帮助将不胜感激。谢谢。
// attach a click even to all <a> elements
$("a").click(function() {
addAffiliate(this);
});
// adairs affiliate URL for redirect
var adairsaffURL = "https://t.example.com/12345/t/54321?Url=";
// bedthreads affiliate URL for redirect
var bedthreadsaffURL = "https://t.example.com/12121/t/21212?Url=";
// function called when link is clicked
function addAffiliate(link) {
// make sure this link is not to the current site and does not contain the affiliateURL
if ((link.href).indexOf(adairs.com.au) < 0 && (link.href).indexOf(adairsaffURL) < 0){
// update the link with the affiliateURL, the url encoded link, and the additional query string
link.href = affiliateURL + escape(link.href);
}else if((link.href).indexOf(bedthreads.com.au) < 0 && (link.href).indexOf(bedthreadsaffURL) < 0){
link.href = bedthreadsaffURL + escape(link.href);
}
alert(link.href);
// return true to follow the link
return true;
}
【问题讨论】:
标签: javascript php prepend affiliate