I know its really annoying to add target="_blank" to every external link in your website, so I wanted to share this code you can use to make every link open in a new tab. Just put this code right above the closing </body> tag.
document.addEventListener('DOMContentLoaded', function() {
var links = document.querySelectorAll('a[href]');
var host = window.location.hostname;
links.forEach(function(link) {
var href = link.getAttribute('href');
if (href && href.indexOf('http') === 0 && href.indexOf(host) === -1) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
}
});
});
This will automatically find every link on the page that points to an external domain and set it to open in a new tab. No need to manually add target="_blank" to each one.
