willy.club/public/static/js/jarring.js

39 lines
1.3 KiB
JavaScript

/**
* Reloading the page can feel jarring!
* Replace the content of the page without reloading the DOM
*/
onclick = (e) => {
let element = e.target;
if (element.tagName == 'A') {
navigate(element.href).then(function(url) {
window.history.pushState('', '', url);
});
return false; // prevent default action and stop event propagation
}
};
onpopstate = () => {
navigate(document.location);
};
function navigate(href) {
document.body.className = 'leaving';
return new Promise((resolve) => {
fetch(href)
.then(async (response) => {
html = await response.text();
//var parser = new DOMParser();
//var doc = parser.parseFromString(html, 'text/html');
//document.getElementById('pageContent').innerHTML = doc.getElementById('pageContent').innerHTML;
setTimeout(() => {
document.body.parentNode.innerHTML = html;
document.body.className = 'entering';
}, 250);
resolve(response.url);
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
}
);
}