В раздел
head
:
+ function applyTheme() {
+ const isDark = (localStorage.getItem('is-dark-theme') ?? 'true') === 'true';
+ if (isDark) {
+ document.body.classList.remove('light-mode');
+ document.body.classList.add('dark-mode');
+ } else {
+ document.body.classList.remove('dark-mode');
+ document.body.classList.add('light-mode');
+ }
+ }
- var dark = true;
function themeChange() {
- var element = document.body;
-
- if (dark == true) {
- element.classList.remove("dark-mode");
- element.classList.add("light-mode");
- dark = false;
- }
-
- else {
- element.classList.remove("light-mode");
- element.classList.add("dark-mode");
- dark = true;
- }
+ localStorage.setItem('is-dark-theme', !((localStorage.getItem('is-dark-theme') ?? 'true') === 'true'));
+ applyTheme();
}
+ window.addEventListener('DOMContentLoaded', function () {
+ applyTheme();
+ });