Skip to content

Instantly share code, notes, and snippets.

@iamrobert
Created March 6, 2023 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamrobert/936647385e20322adfe083532865a2ff to your computer and use it in GitHub Desktop.
Save iamrobert/936647385e20322adfe083532865a2ff to your computer and use it in GitHub Desktop.
Open LInk in new window via JS
//4. SET EXTERNAL LINKS
function setRelAttribute() {
var elems = document.body.querySelectorAll('.mainbody a:not([class^="glightbox"])');
var l = window.location.hostname;
var regex = new RegExp('^https?:\/\/' + l.replace(/\./g, '\\.') + '(\/.*)*$|^\/.*$');
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
var isInternal = regex.test(elem.href);
if (!isInternal) {
if (elem.rel === '') {
elem.rel = 'noopener';
}
elem.setAttribute('target', '_blank');
elem.classList.add('no-barba');
}
}
}
setRelAttribute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment