I am currently working on a blog project that allows visitors to customize the background color by simply clicking on one of five available buttons.
$(function(){
//Checking if a color scheme has already been selected
var chosenColor = parseInt(getCookie("background_color"));
switch(chosenColor){
case 1:
$('body').css('background-color','#0A0A0A');
break;
case 2:
$('body').css('background-color','#766777');
break;
case 3:
$('body').css('background-color','#EEE6EE');
break;
case 4:
$('body').css('background-color','#9F00A9');
break;
case 5:
$('body').css('background-color','#420668');
break;
default:
$('body').css('background-color','#9F00A9');
}
$('#cor_01').click(function(){
setCookie('background_color', 1);
$('body').css('background-color','#0A0A0A');
});
$('#cor_02').click(function(){
setCookie('background_color', 2);
$('body').css('background-color','#766777');
});
$('#cor_03').click(function(){
setCookie('background_color', 3);
$('body').css('background-color','#EEE6EE');
});
$('#cor_04').click(function(){
setCookie('background_color', 4);
$('body').css('background-color','#9F00A9');
});
$('#cor_05').click(function(){
setCookie('background_color', 5);
$('body').css('background-color','#420668');
});
});
The default color for the blog is purple.
background: #9F00A9
While the button event works perfectly when the user changes the background color, there is an issue when navigating between different pages on the blog. The previously selected color briefly reverts back to black before loading the correct color (for example, going from purple to black and then back to purple). To address this, how can I ensure that the user's selected color is loaded as the priority?