-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
42 lines (41 loc) · 1.46 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
window.onload = () => {
let uri = location.hash.replace("#", "");
if(document.querySelectorAll("nav a").length < 2) {
document.querySelector("nav").classList.add("hidden");
}
if (uri == "") {
uri = document.querySelector("nav a").getAttribute("href").replace("#", "");
location.hash = uri;
} else {
loadPage(uri);
}
document.addEventListener("click", (e) => {
if(e.target.className.indexOf("clickable") >= 0) {
if(e.target.className.indexOf("active") >= 0) {
e.target.classList.remove("active");
} else {
document.querySelectorAll(".clickable.active").forEach(e => {
e.classList.remove("active");
})
e.target.classList.add("active");
}
} else {
document.querySelectorAll(".clickable.active").forEach(e => {
e.classList.remove("active");
})
}
});
}
let loadPage = (uri) => {
fetch("./pages/" + uri + "?" + new Date().getTime()).then((res) => {
res.text().then((data) => {
document.getElementById("content").innerHTML = data;
});
});
document.querySelectorAll("nav a").forEach(f=>{f.classList.remove("active")});
document.querySelector("a[href='#"+uri+"']").classList.add("active");
}
window.addEventListener('hashchange', () => {
let uri = location.hash.replace("#", "");
loadPage(uri);
});