My website features two stylesheets, one for day mode and one for night mode. There is an image on the site that triggers a function with an onclick
event to switch between the two stylesheets. However, when new visitors click the button for the first time, there is a brief moment where the page appears completely unstyled before the new stylesheet is applied. I have identified that this occurs because I remove the rel="stylesheet"
attribute from the current stylesheet before adding it to the new one. Is there a way to modify the function so that there is no delay in styling, even for new visitors? Your assistance with this issue is greatly appreciated.
function changeStyleSheet() {
var a = document.getElementById('stylesheet1');
var b = document.getElementById('stylesheet2');
var now1 = $(a).attr('rel');
var now2 = $(b).attr('rel');
if (now1 == 'stylesheet') {
a.setAttribute('rel', 'alt-stylesheet');
b.setAttribute('rel', 'stylesheet');
} else {
b.setAttribute('rel', 'alt-stylesheet');
a.setAttribute('rel', 'stylesheet');
}
};