This commit is contained in:
William 2023-02-08 15:00:14 +01:00
parent 89fae23784
commit 59a42c2028

View File

@ -0,0 +1,37 @@
/**
* 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);
});
}
);
}