diff --git a/public/static/js/jarring.js b/public/static/js/jarring.js new file mode 100644 index 0000000..841c386 --- /dev/null +++ b/public/static/js/jarring.js @@ -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); + }); + } + ); +} \ No newline at end of file