Skip to content

Commit 2988476

Browse files
committed
Reimplemented and fixed Dark Mode and Section States to keep them across the whole session (multiple tabs), only to be resetted when the whole window is closed.
1 parent 1c9c343 commit 2988476

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

WebApplication2/Customer/Pages/CustomerComponent.aspx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -669,16 +669,16 @@
669669
const icon = document.getElementById('modeIcon');
670670
if (body.classList.contains('dark-mode')) {
671671
icon.src = "https://img.icons8.com/?size=100&id=83221&format=png&color=FAB005"; // Sun for light mode
672-
sessionStorage.setItem('darkMode', 'true'); // Save dark mode state
672+
localStorage.setItem('darkMode', 'true'); // Save dark mode state
673673
} else {
674674
icon.src = "https://img.icons8.com/?size=100&id=59841&format=png&color=FFFFFF"; // Half Moon for dark mode
675-
sessionStorage.removeItem('darkMode'); // Remove dark mode state
675+
localStorage.removeItem('darkMode'); // Remove dark mode state
676676
}
677677
}
678678
679679
document.addEventListener("DOMContentLoaded", function () {
680-
// Check if dark mode is enabled in sessionStorage
681-
if (sessionStorage.getItem("darkMode") === "true") {
680+
// Check if dark mode is enabled in localStorage
681+
if (localStorage.getItem("darkMode") === "true") {
682682
document.body.classList.add("dark-mode");
683683
document.body.classList.remove("light-mode");
684684
document.getElementById('modeIcon').src = "https://img.icons8.com/?size=100&id=83221&format=png&color=FAB005"; // Sun for light mode
@@ -691,15 +691,20 @@
691691
692692
// Function to clear dark-mode state
693693
function clearDarkModeState() {
694-
sessionStorage.removeItem('darkMode');
694+
localStorage.removeItem('darkMode');
695695
}
696696
697697
window.addEventListener('load', function () {
698-
if (sessionStorage.getItem('darkMode') === 'enabled') {
698+
if (localStorage.getItem('darkMode') === 'enabled') {
699699
document.body.classList.add('dark-mode');
700700
}
701701
});
702702
703+
// Clear localStorage on window close
704+
window.addEventListener("beforeunload", function () {
705+
localStorage.clear();
706+
});
707+
703708
// Sortable Table Functionality
704709
document.addEventListener('DOMContentLoaded', function () {
705710
document.querySelectorAll('.styled-table th').forEach(function (th) {
@@ -738,7 +743,7 @@
738743
function logout() {
739744
// Clear the session variable
740745
localStorage.removeItem('activeSection');
741-
sessionStorage.clear();
746+
localStorage.clear();
742747
// Send a POST request to the logout endpoint
743748
fetch('logout.aspx', { method: 'POST' })
744749
.then(response => {

0 commit comments

Comments
 (0)