website/assets/js/page.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-08-06 09:51:41 +02:00
import Cookies from 'js-cookie';
2021-08-05 13:51:44 +02:00
document.addEventListener("readystatechange", event => {
if(event.target.readyState === 'complete') {
initOverflow();
initExternalLinks();
2021-08-06 09:51:41 +02:00
initNinetiesToggle();
2021-08-05 13:51:44 +02:00
}
});
function initOverflow() {
let body = document.querySelector('body');
if (body.scrollHeight > window.outerHeight) {
body.style.height = 'auto';
}
}
2021-08-06 09:51:41 +02:00
function initNinetiesToggle() {
document.querySelector('.nineties-toggle').addEventListener('click', function() {
let body = document.querySelector('body');
if (body.classList.contains('nineties')) {
body.classList.remove('nineties');
Cookies.remove('nineties');
} else {
body.classList.add('nineties');
Cookies.set('nineties', true, { sameSite: 'strict' });
}
})
}
2021-08-05 13:51:44 +02:00
function initExternalLinks() {
document.querySelectorAll('a').forEach(elem => {
if(isExternalURL(elem.getAttribute('href'))) {
elem.setAttribute('target', '_blank');
}
})
}
const isExternalURL = (url) => new URL(url).origin !== location.origin;