50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
|
const container = document.querySelector(".container");
|
|
|
|
async function loadPage(url, push = true) {
|
|
showLoader();
|
|
|
|
try {
|
|
|
|
const res = await fetch(url, { headers: { "X-Requested-With": "fetch" }});
|
|
const html = await res.text();
|
|
|
|
const parser = new DOMParser();
|
|
const doc = parser.parseFromString(html, "text/html");
|
|
const newContent = doc.querySelector(".container").innerHTML;
|
|
|
|
container.innerHTML = newContent;
|
|
|
|
if (push) history.pushState({}, "", url);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
showError("Impossible de charger la page. Vérifiez votre connexion.");
|
|
}
|
|
|
|
finally {
|
|
hideLoader();
|
|
}
|
|
}
|
|
|
|
// Intercepter les clics sur les liens internes
|
|
document.body.addEventListener("click", (e) => {
|
|
const link = e.target.closest("a");
|
|
|
|
if (link && link.getAttribute("href").startsWith("/")) {
|
|
|
|
e.preventDefault();
|
|
loadPage(link.href);
|
|
|
|
}
|
|
});
|
|
|
|
// Gérer le bouton retour
|
|
window.addEventListener("popstate", () => {
|
|
|
|
loadPage(location.pathname, false);
|
|
|
|
});
|
|
});
|