Can anyone help me to figure out what's actually wrong with my night mode JavaScript, When night mode is switch on and then i refresh my page or navigate to another page my night mode script switches back off, what might possibly be the issue with local storage or maybe it's just my code, honestly am a no-voice in JS and these is really getting my head to explode. since i have spend almost a month trying to hunt down the bug in my code. I also used the google chrome debug tool but it doesn't show where the bug is thought! I will really celebrate the person who helps me solve this issue for life time. Thank you !
<script>
(function (window, document, undefined) {
'use strict';
if (!('localStorage' in window)) return;
var nightMode = localStorage.getItem('gmtNightMode');
if (nightMode) {
document.documentElement.className += ' night-mode';
}
})(window, document);
(function (window, document, undefined) {
'use strict';
// Feature test
if (!('localStorage' in window)) return;
// Get our newly insert toggle
var nightMode = document.querySelector("#night-mode");
// When clicked, toggle night mode on or off
nightMode.addEventListener('click', function (event) {
event.preventDefault();
document.documentElement.classList.toggle('dark');
if (document.documentElement.classList.contains('dark')) {
localStorage.setItem('gmtNightMode', true);
return;
}
localStorage.setItem('gmtNightMode');
}, true);
})(window, document);
</script>