-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.js
More file actions
22 lines (19 loc) · 893 Bytes
/
init.js
File metadata and controls
22 lines (19 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async function includeHeaderFooter() {
// Calcula el path base segons la profunditat actual
const pathDepth = window.location.pathname.split('/').filter(p => p).length;
const basePath = pathDepth > 1 ? '../' : './';
async function fetchAndInsert(path, selector) {
try {
const res = await fetch(path);
if (!res.ok) throw new Error("No s'ha pogut carregar " + path);
const html = await res.text();
document.querySelector(selector).innerHTML = html;
} catch (err) {
console.error('Error incloent fragment:', err);
}
}
await fetchAndInsert(basePath + 'includes/header.html', '#site-header');
await fetchAndInsert(basePath + 'includes/footer.html', '#site-footer');
}
// Crida la funció quan el DOM està llest
document.addEventListener('DOMContentLoaded', includeHeaderFooter);