8

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>

7 Answers 7

8

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.querySelectorAll("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]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Just a small comment - i < anchors.length needs to be i <= anchors.length in order to not break.
if you got 3 anchors childs, your loop will run only twice
I do not see how. If anchors.length == 3, then i = 0, 1, 2.
This sometimes works, but not always. When I ran this script on the Stack Overflow home page, many of the links were not removed.
Using querySelectorAll fixes it.
7

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

$('document').ready(function (){
    $('a').contents().unwrap();
});​​​​​​​​​​​​​​​​​

2 Comments

How can I get JQuery and how do I use it?
You can also reference the Google Hosted jQuery file. code.google.com/apis/ajaxlibs/documentation/#jquery example: <script src="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.
3
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.

1 Comment

As an explanation, it wouldn't show up in the anchors list unless it had the name attribute set. Also, the reason it takes out every other one is because when you delete the first one, the second one becomes the first one, and you move to the second one, delete that, and the third... You get the idea. I didn't understand that back then, I guess. What would work fine is a while loop. While (document.anchors.length > 0) doc.anc[0].outHTML = doc.anc[0].inHTML;
2

You can use removeAttribute:

var allImages = document.querySelectorAll('.imageLinks');

function removehyperlinks()(){  
    for (var i = 0; i < allImages.length; i++) {
    allImages[i].removeAttribute("href");
  }
}

removehyperlinks()()

Comments

1

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]);
}

3 Comments

@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.
I used both alert(document.anchors.length); (0) alert(document.getElementsByTagName('a')); ([Object NodeList])
This doesn't seem to work correctly because ary.length is dynamic and changes with every removal.
1

in one line

document.querySelectorAll(".description a").forEach(a => a.outerHTML = a.innerHTML);

Comments

1

The shortest one that works for me from the browser console I copied from this answer:

[...document.getElementsByTagName('a')].map(a => a.removeAttribute("href"));
a {
  color: red;
  text-decoration: underline;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Example Links</title>
</head>

<body>
  <h1>Example Links</h1>
  <p>Here are some useful links:</p>
  <ul>
    <li><a href="https://www.google.com" target="_blank">Google</a></li>
    <li><a href="https://www.wikipedia.org" target="_blank">Wikipedia</a></li>
    <li><a href="https://www.github.com" target="_blank">GitHub</a></li>
  </ul>
  <p><a href="#footer">Jump to Footer</a></p>

  <h2 id="footer">Footer Section</h2>
  <p>Back to <a href="#top">Top</a></p>
</body>

</html>

But, as you can see from the snippet, it only stops links from behaving like links, but? if they styled in CSS, visually they still going to look like links.

In this case you may replace links with span while keeping the content, you can modify the JavaScript like this:

[...document.getElementsByTagName('a')].forEach(a => {
  let text = a.textContent;
  let span = document.createElement('span');
  span.textContent = text;
  a.replaceWith(span);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.