-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
41 lines (34 loc) · 1.06 KB
/
Copy pathmain.js
File metadata and controls
41 lines (34 loc) · 1.06 KB
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
// Get the buttons
const homeButton = document.getElementById('homeButton');
const projectButton = document.getElementById('projectButton');
// Add click event listeners to the buttons
addPageLoadListener(homeButton, 'home');
addPageLoadListener(projectButton, 'projects');
// Load page content using Ajax
function loadPage(pathname) {
fetch(`pages/${pathname}.html`)
.then(response => {
if (response.ok) {
return response.text();
} else {
throw new Error("Page not found.");
}
})
.then(html => {
content.innerHTML = html;
// change the URL only if the cite isent locally hosted (say if it's up on https://Luramoth.Github.io :3)
if (window.location.protocol === 'http:' && window.location.hostname === 'localhost' !== -1) {
return;
} else {
history.pushState(null, "", pathname);
}
})
.catch(error => console.log(error));
}
// Helper function to add a click event listener to a button to load a page
function addPageLoadListener(button, url) {
button.addEventListener('click', () => {
loadPage(url);
});
}
loadPage('home');