Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove link tags

Status
Not open for further replies.

Pichdude

Programmer
Aug 24, 2005
66
SE
Does anybody know a solution, that works both in IE and Mozilla, on how to remove link tags in a webpage?

Everywhere it is e.g. <a href="xxx.html">webpage</a> only want to keep the text between the start and end tag, i.e. "webpage".
 
Certainly!

You need to use document.getElementsByTagName('a') to get a collection of all the 'a' nodes in the DOM. Then you would iterate through each one... querying it's childNode for the 'text' node that contains the text you want to keep ("webpage" in your example above). Then you would clone that 'text' node, remove the 'a' node and add in the 'text' node you cloned.

Repeat until you have finished iterating the collection.

Some very well written posts in this forum discuss how you can do pretty much everything I have described above.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Thanks!

However, it is the part where I will query, clone, remove and add where I can't get it to work in both IE and Mozilla. Do you by any chance have any tips on a working solution in both browsers?

Best regards,

Pichdude
 
The simplest approach is to dis-function the href hyperlink. If there is only one hyperlink to xxx.html, it can be done this way.
[tt]
function handler() {
var canchor=document.getElementsByTagName("a");
for (var i=0;i<canchor.length;i++) {
if (canchor.href.indexOf("xxx.html")!=-1) {
canchor.href="javascript:void(0)";
}
}
}
[/tt]
 
-> tsuji

But in you case the link still remains and the text is underlined, right? I want it to be transformed from a link to ordinary text both in IE and Mozilla.
 
>I want it to be transformed from a link to ordinary text both in IE and Mozilla.
I can give you a quick solution for ie.
[tt] canchor.outerHTML=canchor.innerHTML;[/tt]

 
tsuji - he wants to remove the <a href> and the </a> part completely.

pichdude - do some searching on this forum using key words from the post I made... you will find plenty of examples of this kind of thing. You can solve the problem yourself if you take it in small chunks. The process I outlined is totally cross-browser and cross-platform. You just need to research the syntax.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Thanks Jeff, I appreciate the interpretation. I gave one "simplest" thing to do. If the op is not completely happy with the reduced performance, it is fair enough.

The second .outerHTML=.innerHTML is another solution for ie only, as I said.

Here is now the cross-browser solution also taking always the simplest path.
[tt]
canchor.removeAttribute("href");
[/tt]
That make no difference in display with what required.
 
Amendment

The last solution is effective only on ff/nn. By combining both, the functional solution is this.
[tt]
if (document.all) { //simplistic ie identifier
canchor.outerHTML=canchor.innerHTML;
} else {
canchor.removeAttribute("href");
}
[/tt]
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top