-2

I'm trying to replace all HREF with domain1.com with the URL described in his title (if it has a title).

var els = document.getElementsByTagName('a'),
  for (els in url) {
    if (url.includes(domain1)); {
      var titulo = url.attr('title');
      var enlace = url.attr('href');
      url.replace(enlace, titulo);
    }
  }
<p style="margin:40px;" line-height:2px;=""><img src="https://www.domain1.com/738956_1.jpg" data-width="1070" data-height="1387" data-image="738956-sanKH" alt="738956-sanKH.jpg">XS <a href="https://www.domian1.com/plants" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test1">texto</a><br>
  <img src="https://www.domain1.com/738956_1.jpg" data-width="1077" data-height="1693" data-image="738956-iMkWh" alt="738956-iMkWh.jpg">S <a href="https://www.domain1.com/flowera" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test2">texto</a></p>

I'm a beginner in javascript. Sure have a lot of format mistakes. Thanks in advance

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
Vicks
  • 21
  • 6
  • 1
    Don't delete [questions](https://stackoverflow.com/questions/71079386/how-do-i-replace-all-urls-of-a-target-domain-in-html-code) and ask them again to destroy the comment history. – Quentin Feb 15 '22 at 11:09
  • It's `for (let url in els)` for one. –  Feb 15 '22 at 11:12
  • @ChrisG — I counted half a dozen different problems and no debugging on the original version of this code. The OP seems to have made zero effort since then. – Quentin Feb 15 '22 at 11:13
  • If you're just going to blindly down-vote every answer posted to your question without any explanation or attempt to engage with the people trying to help you, then don't expect anyone to try to help you. – Richard Deeming Feb 15 '22 at 11:16
  • 1
    @RichardDeeming how do you know it's the OP doing the downvoting? – Andy Feb 15 '22 at 11:17
  • 2
    @Andy I don't, but the fact that every answer prior to my comment was down-voted within seconds of being posted seems highly suspicious. Unless someone else is sat refreshing this page waiting for new answers to down-vote? – Richard Deeming Feb 15 '22 at 11:21
  • thank you all for your time and effort, it's a shame that the staff doesn't allow newbies to help, I know the question had errors, but, as I said, I'm not a developer, I just try to learn with my tests – Vicks Feb 15 '22 at 12:17
  • You didn't get downvoted by staff. Also you're asking a pretty basic thing, and you're supposed to do lots of research *before* posting here. [Here](https://stackoverflow.com/questions/4365246/how-to-change-href-of-a-tag-on-button-click-through-javascript) for instance is the first result for a google search for "js change href", which would've helped a great deal with getting this right. Lots of people posting questions here expect others to just hand them the solution (and unfortunately they are right), but this is not what SO is about. –  Feb 15 '22 at 14:01
  • People who are frustrated that SO has completely abandoned what made it great and devolved into a newbie tutorial chat tend to (correctly) downvote questions and answers that they think (correctly) do not belong here. –  Feb 15 '22 at 14:02

3 Answers3

0

If your DOM is having all the links after document load, then you can do this:

window.onload = bindEvents;

function bindEvents() {
  // replace href with title
  bindAnchorTitleAsHref();
}

function bindAnchorTitleAsHref() {
  const els = document.getElementsByTagName('a');
  for (let i = 0; i < els.length; i += 1) {
      const title = els[i].getAttribute('title');
      const href = els[i].getAttribute('href')
      
      els[i].setAttribute('href', title);
      
      console.log('New Href set :', els[i].getAttribute('href'), 'instead of', href);
  }
}
<a 
  href="https://www.domian1.com/plants"
  title="https://www.newdomain.com/test1"
  target="_blank" 
  rel="nofollow noopener"
>
  texto
</a>
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • thank you all for your time and effort, it's a shame that the staff doesn't allow newbies to help, I know the question had errors, but, as I said, I'm not a developer, I just try to learn with my tests – Vicks Feb 15 '22 at 12:16
0

If you just want to update the domain of the link, then you can use the URL class:

document.addEventListener('DOMContentLoaded', () => {
  document.querySelectorAll('a[title]').forEach(el => {
    const href = new URL(el.href);
    if ((/[\^\.]domain1.com$/i).test(href.host)) {
      const title = new URL(el.title);
      href.host = title.host;
      el.href = href.toString();
    }
  });
});
<p style="margin:40px;" line-height:2px;=""><img src="https://www.domain1.com/738956_1.jpg" data-width="1070" data-height="1387" data-image="738956-sanKH" alt="738956-sanKH.jpg">XS <a href="https://www.domian1.com/plants" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test1">texto</a><br>
  <img src="https://www.domain1.com/738956_1.jpg" data-width="1077" data-height="1693" data-image="738956-iMkWh" alt="738956-iMkWh.jpg">S <a href="https://www.domain1.com/flowera" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test2">texto</a></p>
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • This doesn't check to see if there's `domain1.com` in the href attribute. – Andy Feb 15 '22 at 11:21
  • @Andy Fair point; it does now. – Richard Deeming Feb 15 '22 at 11:26
  • thank you all for your time and effort, it's a shame that the staff doesn't allow newbies to help, I know the question had errors, but, as I said, I'm not a developer, I just try to learn with my tests – Vicks Feb 15 '22 at 12:17
0

Here is a tested JavaScript code, replace the for..of with for..in if you want to support Internet Explorer browser.

  var domain1 = 'domain1.com';
  var els = document.getElementsByTagName('a');
    
  for (var url of els) { 
    var title = url.getAttribute('title');
  
    if (title && url.getAttribute('href').search(domain1) !== -1) {
       url.setAttribute('href', title);
    }
  }
Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24
  • thank you all for your time and effort, it's a shame that the staff doesn't allow newbies to help, I know the question had errors, but, as I said, I'm not a developer, I just try to learn with my tests – Vicks Feb 15 '22 at 12:17