I'm using jQuery("#content").on('click', 'a', function() {});
to add an Amazon Affiliate tag to my content. My content is generated by an ajax call.
The click is being caught by my script; but the link is not modified.
jQuery(document).ready(function() {
jQuery("#content").on('click', 'a', function() {
addAffiliate(this);
});
amazonFRTag = "tag=XXXXXXX-21";
function addAffiliate(link) {
if ((link.href).indexOf("amazon.fr") > -1 && (link.href).indexOf(amazonFRTag) < 0){
(link.href) = (link.href).replace("?tag=", "?old-tag=");
(link.href) = (link.href).replace("&tag=", "&old-tag=");
if ((link.href).indexOf("?") > -1 ) {
link.href = link.href + "&" + amazonFRTag;
}
else {
link.href = link.href + "?" + amazonFRTag;
}
}
return true;
}
});
How can I make it work?
EDIT :
Barmar is right, his example works.
But, another script (Skimlinks) seems to modify the links afterwards.
See here
How can I avoid the process of this other script when my function succesfully made the append ? Thanks...
Skimlinks has its own click handler that modifies the link in much the same way that you do. It's running before your code, not after. Here's what it changes the link to:
<a href="http://redirect.kadolog.com/?id=75284X1526071&site=kadolog.be&xs=1&isjs=1&url=http%3A%2F%2Fwww.amazon.fr%2Ffoo1&xguid=560420e8d59460f59daf72824e859763&xuuid=3df2f1da98b6a4bda13909d043bb3f0d&xsessid=141dd024a3348df74d7e1743db5f6891&xcreo=0&xed=0&sref=http%3A%2F%2Ffiddle.jshell.net%2Fgkp7dg1f%2Fshow%2F&pref=http%3A%2F%2Fjsfiddle.net%2Fgkp7dg1f%2F&xtz=240"
sl-processed="1"
data-skimlinks-orig-link="http://www.amazon.fr/foo1">Link1</a>
You can check whether the data-skimlinks-orig-link
attribute is set, and then use that instead of link.href
. Also, the original URL is in the url=
parameter of the replacement link, you can try modifying that part of the URL.
Another option would be to rewrite the links to add your affiliate tags as soon as you load the content using AJAX, rather than in the click handler. The Skimlinks FAQ says they don't modify links that already have affiliate tags.