Click to See Complete Forum and Search --> : How to replace text with link?


jackman
07-12-2008, 05:35 AM
I want to replace any keyword which displays on html page with keyword with link. I want to do this by javascript. This keyword can be already linked, if this keyword is already linked then I don't want to link this keyword with new link tag but if it is not linked then link with some url.

This keyword will also not linked when it is an alt text of an image or used in script or somewhere else which is not displayed on page.

I have to do this using javascript. Can somebody suggest me how to do this?

vwphillips
07-12-2008, 07:55 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
var Ary=[];
Ary[0]=['KeyWord','http://www.webdeveloper.com/'];
function zxcWords(zxcn,ary){
var zxcnu=zxcn.childNodes.length;
for(var zxc0=0;zxc0<zxcnu;zxc0++) {
var zxctxt=zxcn.firstChild.data;
var zxcrn=zxcn.removeChild(zxcn.firstChild);
if(zxcrn.nodeType==3&&zxcn.nodeName.toUpperCase()!='A'){
var zxcs=zxctxt.split(' ');
for(var zxc1=0;zxc1<zxcs.length; zxc1++) {
var zxcnn=document.createTextNode(zxcs[zxc1]+' ');
for (var zxc0a=0;zxc0a<ary.length;zxc0a++){
if (zxcs[zxc1]==ary[zxc0a][0]){
zxcnn=document.createElement('A');
zxcnn.href=ary[zxc0a][1];
zxcnn.appendChild(document.createTextNode(zxcs[zxc1]+' '));
}
}
zxcn.appendChild(zxcnn);
}
}
else {
zxcWords(zxcrn,ary);
zxcn.appendChild(zxcrn);
}
}
}


/*]]>*/
</script></head>

<body onload="zxcWords(document.body,Ary);">
Some Text KeyWord some <a href="http://www.codingforums.com/">text</a> <a href="http://www.codingforums.com/">KeyWord</a>
</body>

</html>

jackman
07-14-2008, 06:58 AM
Thanks vwphilips for your reply. Your code is really good but I have some problem with this.

You are splitting the text with spaces but keyword can have more than one word, so how can I match these keywords and linked those keywords.

vwphillips
07-14-2008, 11:05 AM
I cant look at this now.
You will need to 'bump' this every couple of days unless someone else can do this for you.