Uh
This commit is contained in:
parent
89fae23784
commit
59a42c2028
37
public/static/js/jarring.js
Normal file
37
public/static/js/jarring.js
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user