How to add affiliate links automatically to blog post using javascript

0 votes

Hi there, I want to know if there is any way to automatically add affiliate links to blog post each time the page finish loading.

For instance, below is a blog post
What is computer?
Computer is an electronic+ device that can accept Input, process inputs, and + output result.

How can I automatically add affiliate link to where + is on the post?
Thanks in advance!

Aug 9, 2020 in Java-Script by Okugbe
• 280 points

recategorized Aug 10, 2020 by Niroj 1,458 views

1 answer to this question.

0 votes

Hello @ Okugbe ,

 You need few lines of code, and use regexp: 

  $string = "<p>Please <a href='http://example.com'>click here</a> to go to <a href='http://example.com'>the site</a></p>"; 

$newurl = "http://myotherexample.com"; 

$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/"; 

$newstring = preg_replace($pattern,$newurl,$string); 

Second way 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;
    }

Here you can see my demo jsfiddle working code:http://jsfiddle.net/du47b/23/

Hope you understand the code and this help you!!

Thank You!!

    answered Aug 10, 2020 by Niroj
    • 82,840 points
    Thanks for your response. Am no fan of jQuery and don't even understand it either.

    Please can you translate this to pure javascript. Thanks

    Hello @Okugbe,

    If you want javascript then you can go with method-1 it is the optimal solution but jquery is also javascript lirary so it uses javascrpt only so you also go with method-2 as well.

    However you can refer this for better uderstading of javascript.

    Hope this is helpfull!!
    Thank you!!

    Related Questions In Java-Script

    0 votes
    1 answer

    How to add a custom HTTP header to ajax request with javascript?

    Hello @kartik, There is different way based on ...READ MORE

    answered Aug 25, 2020 in Java-Script by Niroj
    • 82,840 points
    103,684 views
    0 votes
    1 answer

    How to open a URL in a new Tab using JavaScript or jQuery?

    Hello @kartik, Use window.open(): var win = window.open('http://edureka.co/', '_blank'); if (win) ...READ MORE

    answered Aug 25, 2020 in Java-Script by Niroj
    • 82,840 points
    6,287 views
    0 votes
    1 answer

    How to get the file name from a full path using JavaScript?

    Hello @kartik, Use this: var filename = fullPath.replace(/^.*[\\\/]/, '') This ...READ MORE

    answered Sep 2, 2020 in Java-Script by Niroj
    • 82,840 points
    1,390 views
    0 votes
    1 answer

    How to detect a 'touch screen' device using JavaScript?

    Hello @kartik, Use this: function is_touch_device() { ...READ MORE

    answered Sep 18, 2020 in Java-Script by Niroj
    • 82,840 points
    2,690 views
    0 votes
    1 answer
    0 votes
    0 answers

    Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

    Hello guys, Can Someone helps me to find ...READ MORE

    Jan 17, 2020 in Web Development by anonymous
    • 37,520 points
    955 views
    +1 vote
    1 answer

    What is the relationship between angularjs Scope with controller/view?

    Let us consider the below block: <div ng-controller="emp"> ...READ MORE

    answered Jan 20, 2020 in Web Development by Niroj
    • 82,840 points

    edited Jan 21, 2020 by Niroj 1,191 views
    +1 vote
    1 answer

    What are pseudo class in css??

    Hey, The state of an element is controlled  by ...READ MORE

    answered Jan 20, 2020 in Web Development by Niroj
    • 82,840 points

    edited Jan 21, 2020 by Niroj 928 views
    0 votes
    1 answer

    How to Scroll to the top of the page using JavaScript?

    Hii @kartik, Using javascript <script> $("a[href='#top']").click(function() { ...READ MORE

    answered Apr 2, 2020 in Java-Script by Niroj
    • 82,840 points
    1,153 views
    0 votes
    3 answers
    webinar REGISTER FOR FREE WEBINAR X
    REGISTER NOW
    webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP