2

I am currently working on an affiliate website. I have made a script in jQuery that shows the sales page (without affiliate link) when hovering, but when you click on it, it adds my affiliate link to the beginning of the link. Then my network redirects them to the desired page.

EDIT: To clarify, I want to say that I am talking about hovering over links

Now, if you right click on it, a menu opens, and you can click "Copy link". This copies the "xxx" in:

<a href="xxx">Example</a>

Which is without my affiliate link. How do I make the "Copy link" button copy another link than the href in the a tag? Is it even possible?

Right now I avoid it completely by having this in the beginning of my code:

.on("click contextmenu", function(){};

To give you an example of the current code, it works by listening to clicks on a tags with a specific class, then it checks if the href in the a tag contains some text, if it contains that text, it does this:

window.location.href = affiliatelink + link;

If it doesn't, it does this (if I don't have an affiliate link for that product):

window.location.href = link;

So, is my problem possible to solve - or do I have to do it, like I am doing it?

EDIT 2: I have to provide my code

$(document).ready(function(){

$(".class").on("click contextmenu", function(){
    var link = this.href;
    var partner = "https://affiliatelink.com/?url=";

    if(link.indexOf("partnername") != -1){
        window.location.href = partner + link;
    } else {
        window.location.href = link;
    }

});});
Eddy
  • 21
  • 5
  • Possible duplicate of [Click button copy to clipboard using jQuery](http://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery) – ProEvilz Oct 03 '16 at 14:25
  • 1
    `To give you an example of the current code[...]` . No, NO, NO!!! Give us your code, a working example, something. Don't explain your code without showing us. this is offtopic on stackoverflow. Read: http://stackoverflow.com/help/how-to-ask || http://stackoverflow.com/help/mcve – Marcos Pérez Gude Oct 03 '16 at 14:30
  • @MarcosPérezGude - Sorry, I have added the code now. – Eddy Oct 03 '16 at 14:35
  • 1
    Use mousedown and keydown event instead for changing href attribute. And btw, reset it on mouseleave and keyup events – A. Wolff Oct 03 '16 at 14:35
  • @Eddy now is better :) – Marcos Pérez Gude Oct 03 '16 at 14:37

1 Answers1

1

Assuming I'm understanding your question correctly, it might be possible to solve if you change when you're appending your affiliate code to links.

The "copy link address" context menu shortcut is going to copy whatever is in the href attribute of a link tag. So, what if you set that attribute's value to your affiliate link + the original link's text? You would no longer need to do it in a click handler; it would "just work" and would support both cases.

Something like this might do the trick:

var links = [].slice.call(document.querySelector('.your-class'));
links.forEach(function(link){
    link.href = affiliateLink + link.href;
});

You'd no longer need your click handler, and it supports both cases of interacting with links.

EDIT: Updated with your code sample.

$(document).ready(function(){

$(".class").each(function(){
    var link = this.href;
    var partner = "https://affiliatelink.com/?url=";

    if(link.indexOf("partnername") != -1){
        this.href = partner + link;
    }    
});});
ajm
  • 19,795
  • 3
  • 32
  • 37
  • But OP doesn't want the affiliate link to appear on hovering it – A. Wolff Oct 03 '16 at 14:40
  • If I understand your edit correct, it does this: Takes all elements with X class and then if the if condition matches, it replaces the links with my affiliate link (or appends my affiliate link)? Right? When hovering over something, it shows the href - and this won't help me, I assume. Or am I wrong? – Eddy Oct 03 '16 at 14:43
  • @Eddy You are more looking for something like that: https://jsfiddle.net/ksxb8uLq/ – A. Wolff Oct 03 '16 at 14:53
  • @A.Wolff - Makes good sense. I saw your previous comment, but couldn't really image how it would work. Thanks a lot, I'll look into it! If people have other solutions, I'm happy to learn them. – Eddy Oct 03 '16 at 14:57
  • @A.Wolff - Works good, but the links that are not affiliate links become broken somehow. (Example: domain.com/undefined) – Eddy Oct 03 '16 at 15:18