Here are some of the benefits I find in using this approach.
Single source of truth.
I have a single place where I can see everything. I can view all of my links in a single file and change them in one place.
Better than search and replace.
Instead of blindly doing a search and replace all, I have more control over how many times I want my links to appear and where they should appear.
Simple to Use
Use CSS selectors to tell it where you want to look for text. Then specify the text to find. Then tell it the link you want that text to go to. Done.
Here’s example code where I replace the last instance of the word
Amazon with a link to Amazon’s website.
First, you need to load the
js-affiliate.js file on your site. It’s best to place it right before the closing
</body> tag because, this will make sure all of the content is likely to be ready. If this code loads before the text exists on the page then, it will not work.
JsAffiliate is a global object created that accepts an array of configs. It accepts an array because, it can support more than one.
url is the page you want the code to execute on.
selector is a CSS selector to the Element that has your text.
findText is the text to search in the matching
linkTo is the link
findText should point to.
position accepts either
end and tells to search from the start or end of the selector.
occurrences is how many times we should do the substitution.
As you can see, there isn’t much to it and the end result should save you time and keep you a little more organized.