var links = document.querySelectorAll('a'); links.forEach((link) => { var a = new RegExp('/' + window.location.host + '/'); if(!a.test(link.href)) { link.addEventListener('click', (event) => { event.preventDefault(); event.stopPropagation(); window.open(link.href, '_blank'); }); } });