Fix: Add null check for theme toggle button in script.js
- Prevents errors when theme toggle button is not present on page - Ensures theme toggle works reliably on all pages that load script.js
This commit is contained in:
@@ -35,12 +35,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
// Helper function to update the theme toggle button text
|
||||
function updateThemeToggleButtonText() {
|
||||
if (themeToggleButton) {
|
||||
if (body.classList.contains('dark-mode')) {
|
||||
themeToggleButton.textContent = 'Change to Light Mode';
|
||||
} else {
|
||||
themeToggleButton.textContent = 'Change to Dark Mode';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check and apply the saved theme from localStorage
|
||||
const savedTheme = safeStorage.getItem('theme');
|
||||
@@ -52,11 +54,13 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
updateThemeToggleButtonText();
|
||||
|
||||
// Toggle the theme on button click
|
||||
if (themeToggleButton) {
|
||||
themeToggleButton.addEventListener('click', () => {
|
||||
const isDarkMode = body.classList.toggle('dark-mode');
|
||||
safeStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
|
||||
updateThemeToggleButtonText(); // Update the button text after toggling
|
||||
});
|
||||
}
|
||||
|
||||
// Date formatting is now handled consistently on the backend
|
||||
|
||||
|
||||
Reference in New Issue
Block a user