I want to convert certain key words into affiliate links on my website. I do not want to hand-code each one of those links in every page. So I'm looking for a Javascript/Jquery based solution where using a given a list of keywords and corresponding URLs (a two dimensional array), on page load, these keywords are 'URLified' using their corresponding URLS.
I found this comment Find text string using jQuery? which has the code for a case insensitive search, but it URLifies the whole text node instead of just the word.
I could use something like the code below. But the problem is that it will URLify keywords inside <pre>
elements too. I want them URLified inside just <p>
elements.
<script type="text/javascript">
(function($) {
var thePage = $("body");
thePage.html(thePage.html().replace(/testing/ig, '<a href="http://testing.com">testing</a>'));
})(jQuery)
</script>
For eg:
<p>I'm going to test urlification</p>
<pre>
function test() {
alert(' test ');
}
</pre>
should change to
<p>I'm going to <a href="test123.com">test</a> urlification</p>
<pre>
function test() {
alert(' test ');
}
</pre>
` elements should be urlified
– freethinker Aug 06 '11 at 09:02