Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (46 loc) · 1.41 KB
/
Copy pathscript.js
File metadata and controls
52 lines (46 loc) · 1.41 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
const tabButtons = document.querySelectorAll('.tab-button');
const contentContainer = document.getElementById('tab-content');
const loadedResources = new Set();
function loadCSS(path) {
if (loadedResources.has(path)) return;
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = path;
document.head.appendChild(link);
loadedResources.add(path);
}
function loadJS(path) {
if (loadedResources.has(path)) return;
const script = document.createElement('script');
script.src = path;
script.defer = true;
document.body.appendChild(script);
loadedResources.add(path);
}
function loadTab(tabName) {
const htmlPath = `tabs/${tabName}/${tabName}.html`;
const cssPath = `tabs/${tabName}/${tabName}.css`;
const jsPath = `tabs/${tabName}/${tabName}.js`;
fetch(htmlPath)
.then(res => {
if (!res.ok) throw new Error(`Cannot load ${tabName}`);
return res.text();
})
.then(html => {
contentContainer.innerHTML = html;
loadCSS(cssPath);
loadJS(jsPath);
})
.catch(err => {
contentContainer.innerHTML = `<p>Error loading ${tabName} tab: ${err.message}</p>`;
});
}
tabButtons.forEach(button => {
button.addEventListener('click', () => {
const tabName = button.getAttribute('data-tab');
tabButtons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
loadTab(tabName);
});
});
loadTab('basic');