1

I wanted to replace match text to html link in content area in each post on WordPress site.

I implemented that by JavaScript as follows.


jQuery(document).ready(function(){

  var selector = `article p` ;

  for (i = 0; i < document.querySelectorAll( selector ).length; i++){
    if ( document.querySelectorAll( selector )[i].innerText.match(/(vpn|VPN)/) !== null ){
      var term = document.querySelectorAll( selector )[i].innerText.match(/(vpn|VPN)/)[1]
      document.querySelectorAll( selector )[i].innerHTML = document.querySelectorAll( selector )[i].innerHTML.replace( term ,`<a href="https://yuis-programming.com/vpn">${term}</a>`)
      break
    }
  }

});

As you can see the one replace plain HTML text with p tag for hyperlink with dofollow.

So, this is my question. Does such JavaScript work correctly for SEO?

2

1 Answer 1

1

Google can't see some JavaScript links, so you need to be careful.

Which links does Google index?

<a href=”/good-link”>Will be crawled</a> 

This is a regular a href link that will be crawled by Google.

<span onclick=”changePage(‘bad-link’)”>Not crawled</span> 

Google will not crawl this link because it is not an anchor.

<a onclick=”changePage(‘bad-link’)”>Not crawled</a> 

This link is in an anchor, but it does not use the href attribute. That’s why Google won’t crawl it.

<a href=”/good-link” onclick=”changePage(‘good-link’)”>Will be crawled</a> 

This link will be crawled by Google because it is an anchor with a href attribute.

Source

2
  • 1
    Google can crawl dynamic links powered by JavaScript as long as they are written into the DOM when it loads. You are correct that links written on click won't get crawled, but not all JavaScript powered links without static href backup are invisible to Googlebot these days. Jun 18, 2020 at 16:58
  • No but the recommendations above come straight from Google, so while other implementations can and do work, it's no guaranteed. Jun 18, 2020 at 17:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.