- Jun 10, 2010
- 2,084
- 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.
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.
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.