-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline_script_19.js
More file actions
139 lines (61 loc) · 3.26 KB
/
inline_script_19.js
File metadata and controls
139 lines (61 loc) · 3.26 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
62
63
64
65
66
67
68
69
70
document.addEventListener("DOMContentLoaded", function () {
const menuToggle = document.getElementById("menuToggle");
const sidebar = document.getElementById("sidebar");
// Toggle sidebar in mobile mode
menuToggle.addEventListener("click", function () {
sidebar.classList.toggle("show");
});
// Switch tabs and hide sidebar in mobile mode
const tabLinks = document.querySelectorAll(".sidebar a");
tabLinks.forEach(link => {
link.addEventListener("click", function (e) {
e.preventDefault();
const parentLi = this.parentElement; // Get the parent <li> element
// Remove active class from all <li> elements
document.querySelectorAll(".sidebar ul li").forEach(li => {
li.classList.remove("active");
});
// Add active class to the parent <li> of the clicked link
parentLi.classList.add("active");
const tabId = this.getAttribute("data-tab");
document.querySelectorAll(".tab-content").forEach(tab => {
tab.classList.remove("active");
});
document.getElementById(tabId).classList.add("active");
// Update URL hash without scrolling - works for file protocol
try {
// Method that works for both file and http protocols without scroll
const newUrl = window.location.href.split('#')[0] + '#' + tabId;
window.history.replaceState(null, null, newUrl);
} catch (e) {
// Fallback for browsers that don't support replaceState with file protocol
console.log("Hash update failed:", e);
}
// Hide sidebar in mobile mode after clicking a tab
if (window.innerWidth <= 767.98) {
sidebar.classList.remove("show");
}
});
});
// Handle URL hash on page load - ONLY if hash exists
if (window.location.hash) {
const hash = window.location.hash.substring(1);
const tabExists = document.getElementById(hash);
if (tabExists && tabExists.classList.contains('tab-content')) {
// Switch to the tab but don't update hash again
const activeLink = document.querySelector(`.sidebar a[data-tab="${hash}"]`);
if (activeLink) {
const parentLi = activeLink.parentElement;
document.querySelectorAll(".sidebar ul li").forEach(li => {
li.classList.remove("active");
});
parentLi.classList.add("active");
document.querySelectorAll(".tab-content").forEach(tab => {
tab.classList.remove("active");
});
document.getElementById(hash).classList.add("active");
}
}
}
// If no hash, stay on homepage - no automatic tab activation
});