Identify all links on a site and change it with java script

Asked

Viewed 135 times

-1

There is how to 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 change the links. Would that be:

ANCIENT LINK: https://site.com/dwd8464

AUTOMATICALLY SWITCH TO: https://site.com/linkdireto/? a=dwd8464

Can you do ? like a link protector, but to change the link.

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

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

  • 1

    If you are going to have all this work on a site whose PHP you have access to, it would no longer be better to change in PHP itself?

  • You could even graft a Javascript to go through all the elements <a> and make this change, but it would hurt the user experience (not to mention it could open up a small gap to click on the old link)

  • the links are used in other sites, so I want to make available the javascript, to be able to put in their sites, to be able to change my linsks, to the new format, so without having to modify each post..

  • you want to change the link that third parties point to you?

  • Yes, they use my server. Then they want to change the url parameter to the new one without having to manually modify it.

  • 1

    I believe that changing the .htaccess is the best solution in this case, but sincerely a Ctrl + H in a half-mouth IDE is quite simple to do than a javascript that does this

  • @Guilhermecostamilam or use a sed for programmatic editing of text...

  • Ai e que ta personal, /linkdireto/ and a folder that I made myself, and n can touch . htacsess, why it will be on client sites, and almost equal those scripts that adfly provides.

  • if I understand, /linkdireto/ is a folder on your server where you have something that you make available to your clients, you can change YOUR .htacces to redirect links in the old format to the new one, but if you’re going to make js available, make available a regex that captures old links and swaps for new ones and tells your client to use it in netbeans, vs code, I think even in the sublime to do this

  • Ai e que ta, the old links will still work! the redirect I want to do only to those who use the script.

Show 5 more comments

1 answer

1


Well, I made a simple code with javascript. I’m getting all the elements "to", taking your links, cutting them and concatenating with the "/seulink/"

Although I did take the elements "to" recommend putting a class in these elements, so that not all of the page are taken.

<!DOCTYPE html>
    <html>
    
    <body>
      <a href="https://google.com">Google</a>
      <a href="/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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.