Created
January 14, 2020 18:41
-
-
Save ericzon/8256ba0fecc8a03f49a0b46348644843 to your computer and use it in GitHub Desktop.
Securize dynamically target blank links
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
function setRelAttribute() { | |
const elems = document.body.getElementsByTagName('a'); | |
for (let i = 0; i < elems.length; i++) { | |
const elem = elems[i]; | |
elem.setAttribute('rel', 'noopener noreferrer nofollow'); | |
} | |
} | |
const ready = (callback) => { | |
if (document.readyState !== 'loading') callback(); | |
else document.addEventListener('DOMContentLoaded', callback); | |
} | |
ready(() => { | |
document.body.addEventListener('DOMSubtreeModified', function () { | |
setRelAttribute(); | |
}, false); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment