Page change without reloading page

This commit is contained in:
Crom (Thibaut CHARLES) 2018-05-16 01:46:53 +02:00
parent 4cd3402f9d
commit 9887857fc9
Signed by: tcharles
GPG Key ID: 45A3D5F880B9E6D0
1 changed files with 20 additions and 7 deletions

View File

@ -15,13 +15,20 @@ $(document).ready(() => {
let path = document.location.pathname;
console.log("pathname: ", path);
if(path === "/" || path === "" || path === "/index.html"){
path = "/home";
}
setPage(path);
$("nav > a").each((i, elmt) => {
const href = $(elmt).attr("href");
if(href[0] === "/"){
$(elmt).bind("click", () => {
// window.location.pathname = href;
history.pushState(null, null, href);
setPage(href);
return false;
})
}
})
})
@ -37,6 +44,10 @@ function setPage(path){
sanitize: false,
};
if(path === "/" || path === "" || path === "/index.html"){
path = "/home";
}
$.ajax("/pages"+path+".md")
.done((res) => {
$("#article-container").html(res);
@ -54,8 +65,10 @@ function setPage(path){
return res;
}));
});
})
.fail((err) => {
$("#article-container").html(`<article><h1>Error ${err.status} :'(</h1><p>${err.statusText}<br/>${err.responseText}</p>`);
console.error(err);
});
}