How to change href if contains a link

I want to create a script that changes the href of a button only if it contains a link or a period, since URLs normally contain periods (if it contains a link, I want to replace it with another one, for example google.com). However, the script I have changes all hrefs regardless of their content.

window.onload = function() {
  var anchors = document.getElementsByClassName("elementor-button");
  for (var i = 0; i < anchors.length; i++) {
    anchors[i].href = "https://google.com"
  }
}

5

You can use string.includes() to see if the href includes a dot.
Below is the final code, however, as the href on an anchor in an SO snippet has the default domain of SO, it doesn’t work correctly in the snippet – demonstrational snippet is below.

window.onload = function() {
  var anchors = document.getElementsByClassName("elementor-button");
  for (var i = 0; i < anchors.length; i++) {
    if (anchors[i].href.includes(".")) {
      anchors[i].href = "https://google.com"
    }
  }
}
<a href="#internal-link" class="elementor-button">Internal link</a>
<a href="stackoverflow.com" class="elementor-button">External link</a>

Demonstrational snippet

window.onload = function() {
  var anchors = document.getElementsByClassName("elementor-button");
  for (var i = 0; i < anchors.length; i++) {
    if (anchors[i].getAttribute("data-href").includes(".")) {
      console.log(anchors[i].text + " has dot")
    }
  }
}
<a data-href="#internal-link" class="elementor-button">Internal link</a>
<a data-href="stackoverflow.com" class="elementor-button">External link</a>

Script that only changes the href if it already contains a dot, which is common in URLs

<script>
    window.onload = function() {
        var anchors = document.getElementsByClassName("elementor-button");
        for (var i = 0; i < anchors.length; i++) {
            var href = anchors[i].href;
            if (href.includes(".")) {
                anchors[i].href = "https://google.com";
            }
        }
    }
</script>
  • includes(“.”) method checks if the href contains a dot.
  • Only if the dot is present, the href is updated to “https://google.com”.

You want to check if the href contains an external website URL.

There are 2 solutions.

For the first one, you suggested checking if the href contains (.).
I added to your suggestion to check if it also contains (‘https’ OR ‘http’).

let’s see the code and explain it.

window.onload = function() {

  var anchors = document.querySelectorAll(".elementor-button");
  
  anchors.forEach(element=>{
  
    var href = element.getAttribute('href');
    
    var valid = href.includes('https://') || href.includes('http://') || href.includes('.') ?true:false;
    
    if(valid){
      element.href = 'https://google.com';
    }
    
  });
  
}
<a class="elementor-button" href="/">stackoverflow</a>
<a class="elementor-button" href="http://github.com">Github</a>
<a class="elementor-button" href="#section-ID">internal</a>
  1. I changed the document element selector from ‘getElementsByClassName’ to ‘querySelectorAll’.

querySelectorAll is more versatile and preferred due to its ability to handle more complex selectors and its static nature, which often aligns better with modern JavaScript development practices.

  1. Then I start to loop on the anchors using ForEach
  2. I preferred to get the ‘href’ as an attribute value

.getAttribute(‘href’) returns the URL as it was specified in the HTML, whether it’s absolute or relative. e.g. #section-ID

.href returns the URL resolved to its absolute form if it was relative. e.g. https://stacksnippets.net/js#section-ID

  1. I created a variable that will hold a boolean value. will be true if the href is valid to be replaced and false if not. and it checks if the href contains (‘.’ OR ‘http://’ OR ‘https://’).
  2. if it’s valid to be replaced then I will replace the href with the URl you prefer. for example ‘https://google.com’.

And that’s all with this method.

We can make something else

If we want to check if the href is external, then We can check if the hostname of the href is not equal to our window hostname.

let’s see the code.

window.onload = function() {
  var anchors = document.querySelectorAll(".elementor-button");
  anchors.forEach(element=>{
    if(element.hostname !== window.location.hostname){
      element.href = 'https://google.com';
    }
  })
  
}
<a class="elementor-button" href="https://stackoverflow.com">stackoverflow</a>
<a class="elementor-button" href="http://github.com">Github</a>
<a class="elementor-button" href="#section-ID">internal</a>

That’s all the solutions in my mind if I understand your question right.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật