views:

90

answers:

4

How do I remove links from a webpage with Javascript. I am using Google Chrome. The code I tried is:

function removehyperlinks() {
    try {
        alert(document.anchors.length);
        alert(document.getElementsByTagName('a'));
        for(i=0;i=document.anchors.length;i++) {
            var a = document.anchors[i];
            a.outerHTML = a.innerHTML;
            var b = document.getElementsByTagName('a');
            b[i].outerHTML = b[i].innerHTML;
        }
    } catch(e) { alert (e);}
    alert('done');
}

Of course, this is test code, which is why I have the alerts and 2 things trying at the same time. The first alert returns "0" the second [Object NodeList] and the third returns "done".

My html body looks like this:

<body onload="removehyperlinks()">
<ol style="text-align:left;" class="messagelist">
    <li class="accesscode"><a href="#">General information, Updates, &amp;   Meetings<span class="extnumber">141133#</span></a>
        <ol>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li start="77"><a href="#"">...</a></li>
            <li start="88"><a href="#">...</a></li>
            <li start="99"><a href="#">...</a></li>
        </ol>
    </li>
  </ol>
</body>
A: 

Try

var ary = document.getElementsByTagName("a");

to get the anchors.

Then you can remove them like this

for (var i=0;i<ary.length;i++) {
  // brain cramp: document.removeElement(ary[i]);
  ary[i].parentNode.removeChild(ary[i]);
}
Robusto
I did(--------)
Arlen Beiler
@Arien Beller: D'oh! Sorry, just saw you trying to use document.anchors, which is what you're using in your loop anyway. This should do what you want about finding and removing them, because what you have is not iterating through the elements and even if it were it would just be removing the text contents of the anchor tag, not the element itself.
Robusto
I used both alert(document.anchors.length); (0) alert(document.getElementsByTagName('a')); ([Object NodeList])
Arlen Beiler
+2  A: 

If you can include jquery, you can do it simply with

$('document').ready(function (){
    $('a').contents().unwrap();
});​​​​​​​​​​​​​​​​​
AdmSteck
How can I get JQuery and how do I use it?
Arlen Beiler
@Arlen -- http://jquery.com/ Download and follow the instructions there
T. Stone
You can also reference the Google Hosted jQuery file. http://code.google.com/apis/ajaxlibs/documentation/#jquery example: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>The benefit is that if your user already visited another site referencing the shared file, it is likely in the user's cache.
Christopher Altman
+2  A: 

Here's some vanilla JS that does the trick. All it does is replace a tags with span's and copies over class and id attributes (if they exist).

var anchors = document.getElementsByTagName("A");

for ( var i=0; i < anchors.length; i++ ) {
    var span = document.createElement("SPAN");
    if ( anchors[i].className ) {
        span.className = anchors[i].className;
    }

    if ( anchors[i].id ) {
        span.id = anchors[i].id;
    }

    span.innerHTML = anchors[i].innerHTML;

    anchors[i].parentNode.replaceChild(span, anchors[i]);
}
Justin Johnson
A: 
function removehyperlinks() {
    try {
        for(i=0;i<document.anchors.length;i++) {
            document.anchors[i].outerHTML = document.anchors[i].innerHTML
        }
    } catch(e) { alert ("try2:" + e);}
}
function runner() {
    for(i=1;document.anchors.length > 0;i++) {
        //alert('run ' + i + ':' + document.anchors.length);
        removehyperlinks();
    }
}

This works. As I am in control of the content, I named all the anchors "link" using a simple search and replace. If you run it once, it takes out every other one. So I just had it repeat, as you can see, till they are all out.

Arlen Beiler