Identify all links in a website and change it with java script

-1

You can create a script, to identify all the links of a site and modify them later, ie add the code in footer.php and so it changes the links. It would look like this:

ANTIQUE LINK : link

CHANGE AUTOMATICALLY FOR : link

Can you do it? as if it were a link protector, but to change the link.

With the code in the footer of the site, it identifies everyone, always needing to modify the posts of my site.

It would be like those scripts that adfly uses to identify links, and add the link protector for them.

    
asked by anonymous 25.06.2018 / 04:01

1 answer

1

Well, I made a simple code with javascript. I'm getting all the "a" elements, grabbing your links, hacking them and concatenating / seulink / strong>

Although I've done picking up the "a" elements I recommend placing a class do not get all of the page.

<!DOCTYPE html>
    <html>
    
    <body>
      <a href="https://google.com">Google</a>
      <a href="https://pt.stackoverflow.com/questions/309522/identificar-todos-os-links-de-um-site-e-alterar-ele-com-java-script">Sua Pergunta</a>
      <p>Clique no botão para alterar os links</p>
    
      <button onclick="mudar()">Clique!</button>
      <script>
        function mudar() {
          var elemento = document.getElementsByTagName("a");
          var qntElementos = elemento.length;
          for (i = 0; i < qntElementos; i++) {
            var link = elemento[i].href;
            var cortaLink = link.split(".com/");
            var novoLink = cortaLink[0] + '.com/SEULINK/' + cortaLink[1];
            elemento[i].href = novoLink;
          }
        }
      </script>
    
    </body>
    
    </html>
    
25.06.2018 / 16:56