Created
March 6, 2023 11:47
-
-
Save iamrobert/936647385e20322adfe083532865a2ff to your computer and use it in GitHub Desktop.
Open LInk in new window via JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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