37 lines
1.1 KiB
JavaScript
37 lines
1.1 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 () {
|
||
|
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);
|
||
|
});
|
||
|
}
|
||
|
);
|
||
|
}
|