-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-nav.js
More file actions
61 lines (54 loc) · 2.18 KB
/
load-nav.js
File metadata and controls
61 lines (54 loc) · 2.18 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Load modern navigation
(function() {
const placeholder = document.getElementById('nav-placeholder');
if (placeholder) {
// Determine the correct path based on current location
const depth = (window.location.pathname.match(/\//g) || []).length - 1;
const prefix = depth > 0 ? '../'.repeat(depth) : '';
fetch(prefix + 'includes/nav.html')
.then(response => response.text())
.then(html => {
placeholder.innerHTML = html;
// Fix relative paths in the loaded content based on depth
if (depth > 0) {
const navLinks = placeholder.querySelectorAll('a[href^="/"]');
navLinks.forEach(link => {
const href = link.getAttribute('href');
if (href.startsWith('/')) {
link.setAttribute('href', prefix + href.substring(1));
}
});
const navImages = placeholder.querySelectorAll('img[src^="/"]');
navImages.forEach(img => {
const src = img.getAttribute('src');
if (src.startsWith('/')) {
img.setAttribute('src', prefix + src.substring(1));
}
});
}
// Initialize mobile menu toggle after navigation is loaded
const toggle = document.getElementById('navToggle');
const menu = document.getElementById('navMenu');
if (toggle && menu) {
toggle.addEventListener('click', function(e) {
e.stopPropagation();
menu.classList.toggle('active');
});
// Close menu when clicking outside
document.addEventListener('click', function(event) {
if (!toggle.contains(event.target) && !menu.contains(event.target)) {
menu.classList.remove('active');
}
});
// Close menu when clicking on a link
const menuLinks = menu.querySelectorAll('a');
menuLinks.forEach(link => {
link.addEventListener('click', function() {
menu.classList.remove('active');
});
});
}
})
.catch(error => console.error('Error loading navigation:', error));
}
})();