Change all website links to affiliate links automatically

前端 未结 3 1745
情歌与酒
情歌与酒 2021-01-06 13:19

I would like to be able to automatically change links into affiliate links automatically on my MediaWiki installation. This would help to reduce the amount

3条回答
  •  逝去的感伤
    2021-01-06 13:47

    So you can also use jquery to bind any link click. This way you can do your link eval on the fly. This jsfiddle is a rough run through of what i think you're trying to accomplish. The alerts are just for your benefit and should be removed.

    $("a").click(function() {
        addAffiliate(this);
    });
    
    myCode = "?pp=708a77db476d737e54b8bf4663fc79b346d696d2";
    myAmazonCode = "?tag=shihac-20"
        function addAffiliate(link) {
            alert("enterting script: " + link.href);
            if ((link.href).indexOf("gog.com") > -1 && (link.href).indexOf(myCode) < 0) {
                    link.href = link.href + myCode;
            }else if((link.href).indexOf("amazon.com") > -1 && (link.href).indexOf(myAmazonCode) < 0){
                    link.href = link.href + myAmazonCode;   
            }
                alert(link.href);
                return true;
            }​
    

    http://jsfiddle.net/du47b/23/

    UPDATE: added code and fully qualified paths

    UPDATE: added 'else if' block for other codes. using 'else if' instead of just another if block will hopefully cut back on unnecessary processing.

提交回复
热议问题