website/assets/js/page.js

21 lines
519 B
JavaScript
Raw Normal View History

import '/assets/scss/page.scss';
2021-08-06 09:51:41 +02:00
2021-08-05 13:51:44 +02:00
document.addEventListener("readystatechange", event => {
if(event.target.readyState === 'complete') {
initExternalLinks();
}
});
2021-08-06 09:51:41 +02:00
2021-08-05 13:51:44 +02:00
function initExternalLinks() {
document.querySelectorAll('a').forEach(elem => {
if(isExternalURL(elem.getAttribute('href'))) {
elem.setAttribute('target', '_blank');
}
})
}
2021-12-31 18:22:31 +01:00
const isExternalURL = (url) => {
if(url.startsWith('/')) return false;
return new URL(url).origin !== location.origin;
}