Create Affiliate Links Using JavaScript

NulledCode

Elite Member
Jr. VIP
Joined
Jun 10, 2010
Messages
2,084
Reaction score
1,508
I created a JavaScript library that finds text on a website and replaces it with a link. It's simple, basic, and doesn't require any jQuery. I created it to use on a static HTML website where I wanted to swap out some text with an affiliate link.



Example:


Load the js-affiliate.js file


Put the following inside 'script' tags before the closing </body> tag.


JavaScript:
    JsAffiliate([

      {

        url: "*",

        selector: "article p",

        findText: "Amazon",

        linkTo: "https://www.amazon.com"

      }

    ]);



url: can be a regex or a string you want the script to execute on. Use '*' for every page.

selector: is a CSS selector where the text can be found.

findText: the text to search inside the matching selector.

linkTo: is the link it will create.



The example above will replace the text 'Amazon' with a link to the Amazon website, which can be an affiliate link.



The source code is available here.
 
Interesting. Does it only work on your own websites? What would be the use case for this? Maybe I'm missing something
 
Interesting. Does it work only on your own websites? What would be the use case for this? Maybe I'm missing something
Could be used to scrape content and automatically replace the affiliate links to your links
 
Nice little wrapper you made there. Liked it. :)
 
Could be used to scrape content and automatically replace the affiliate links to your links
I get that, but you already have access to the content. You can change the links.
 
I get that, but you already have access to the content. You can change the links.

Yes, you are correct I do have access to the content. I use a static site generator like Jekyll to create some sites with the pages written in HTML and the posts written in markdown. Sure, I could search and replace all occurrences of "findText" with "linkTo", rebuild the site, then deploy it, but this is tedious. It's easier for me to have a single JS file with all of my links in a single place that can work throughout the site. No more search and replace. A blind search and replace will replace everything and this isn't good because you might end up with h1-h6 tags replaced, menu items, footer, etc, and EVERYTHING will get replaced.

With this script, there are 2 additional config options:

`occurrences` and `position`

These allow you to replace the second occurrence of findText from the start (position:start).

Example:
occurrences: 2,
position: 'start'

Plus, with the selector option you can target your findText better.

I imagine this would work with WordPress as well. I'm sure there's a plugin that does this for you, but who needs a plugin when this can do the same thing?



Nice little wrapper you made there. Liked it. :)

Thank you.
 
Yes, you are correct I do have access to the content. I use a static site generator like Jekyll to create some sites with the pages written in HTML and the posts written in markdown. Sure, I could search and replace all occurrences of "findText" with "linkTo", rebuild the site, then deploy it, but this is tedious. It's easier for me to have a single JS file with all of my links in a single place that can work throughout the site. No more search and replace. A blind search and replace will replace everything and this isn't good because you might end up with h1-h6 tags replaced, menu items, footer, etc, and EVERYTHING will get replaced.
Hmm .. I guess you're right. It depends on how many links you have on a page
 
Back
Top