So, I want to read a value from sessionStorage. I know how to do this, but then I want to do something like this:
function whiteSet() {
if (// condition: what the heck would I put here?) {
const color = localStorage.getItem('theme');
//if the value 'theme' is equal to 'aluminum' then:
document.getElementById("headerbar").style.backgroundImage = "url(images/themes/aluminum.png)";
} else {
document.getElementById("headerbar").style.backgroundImage = "url(images/themes/galaxy.png)";
};
I don't really know what to do :/ If an answer already exists for this question, I have already tried finding it and failed.
CodePudding user response:
Just take it step by step. First you're reading the value out of local storage and putting it in the variable 'color', then you need to decide what to do based on what value you read out. So instead of starting out with an if
statement, get color
first and then the conditional tests against its content:
function whiteSet() {
const theme = localStorage.getItem('theme');
if (theme === "aluminum") {
document.getElementById("headerbar").style.backgroundImage = "url(images/themes/aluminum.png)";
} else {
document.getElementById("headerbar").style.backgroundImage = "url(images/themes/galaxy.png)";
};
}
(Personally, since you're storing it in a localStorage key named "theme", I'd name the variable "theme" too instead of "color" just to avoid confusion; but that's more of a coding style thing than a requirement for it to work.)