website/assets/js/page.js

21 lines
519 B
JavaScript

import '/assets/scss/page.scss';
document.addEventListener("readystatechange", event => {
if(event.target.readyState === 'complete') {
initExternalLinks();
}
});
function initExternalLinks() {
document.querySelectorAll('a').forEach(elem => {
if(isExternalURL(elem.getAttribute('href'))) {
elem.setAttribute('target', '_blank');
}
})
}
const isExternalURL = (url) => {
if(url.startsWith('/')) return false;
return new URL(url).origin !== location.origin;
}