-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (27 loc) · 1022 Bytes
/
Copy pathscript.js
File metadata and controls
29 lines (27 loc) · 1022 Bytes
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
document.addEventListener("DOMContentLoaded", () => {
const placeholder = document.getElementById('nav-placeholder') || document.getElementById('navbar');
if (placeholder) {
fetch('./navbar.html')
.then(res => {
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
return res.text();
})
.then(data => {
if (placeholder.tagName.toLowerCase() === 'nav') {
placeholder.outerHTML = data;
} else {
placeholder.innerHTML = data;
}
// Highlight active link based on current page
const currentPath = window.location.pathname.split('/').pop() || 'index.html';
const links = document.querySelectorAll('.nav-link');
links.forEach(link => {
const href = link.getAttribute('href').replace(/^\.\//, '');
if (href === currentPath) {
link.classList.add('active');
}
});
})
.catch(err => console.error("Failed to load navbar:", err));
}
});