Home / Admin / Open external links in new tab
Duplicate Snippet

Embed Snippet on Your Site

Open external links in new tab

Code Preview
js
document.addEventListener("DOMContentLoaded", function() {
  var links = document.querySelectorAll("a[href]");
  links.forEach(function(link) {
    if (link.hostname !== window.location.hostname) {
      link.setAttribute("target", "_blank");
      link.setAttribute("rel", "noopener noreferrer");
    }
  });
});

Comments

Add a Comment