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

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-02-08 14:00:14 +00:00
/**
* 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 () {
window.history.pushState('', '', element.href);
});
return false; // prevent default action and stop event propagation
}
};
onpopstate = () => {
navigate(document.location);
};
function navigate(href) {
return new Promise((resolve) => {
fetch(href)
.then((response) => {
return response.text();
}).then(function (html) {
//var parser = new DOMParser();
//var doc = parser.parseFromString(html, 'text/html');
//document.getElementById('pageContent').innerHTML = doc.getElementById('pageContent').innerHTML;
document.body.parentNode.innerHTML = html;
resolve();
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
}
);
}