Gilang Rizkie
Gilang Rizkie

Reputation: 111

sessionStorage doesn't work correctly

this problem look easy yet unsolved by me.

i have javascript code like this to stores data for one session :

function colorRed() {
document.getElementById("dashboard").style.backgroundColor = "#D64541";
document.getElementById("picked").style.backgroundColor = "#D64541";
document.getElementById("logout").style.color = "#D64541";
sessionStorage.setItem('colRed', "#D64541");
}
colorRed(sessionStorage.getItem('colRed'));

function colorGreen() {
document.getElementById("dashboard").style.backgroundColor = "#2ECC71";
document.getElementById("picked").style.backgroundColor = "#2ECC71";
document.getElementById("logout").style.color = "#2ECC71";
sessionStorage.setItem('colGreen', "#2ECC71");
}
colorGreen(sessionStorage.getItem('colGreen'));

function colorBlue() {
document.getElementById("dashboard").style.backgroundColor = "#6BB9F0";
document.getElementById("picked").style.backgroundColor = "#6BB9F0";
document.getElementById("logout").style.color = "#6BB9F0";
sessionStorage.setItem('colBlue', "#6BB9F0");
}
colorBlue(sessionStorage.getItem('colBlue'));

Those function will change the background and font color by button. And thats work perfectly what i want, but.. The problem is when i try to refresh the page, the color change back to default color i use in css.

This is the html code :

<div id="dashboard" class="dashboard">
 <img src="assets/img/svg/029-crown.svg" class="rank"/>
 <h2><?php echo $userRow['username']; ?></h2>
 <a id="logout" href="logout" class="logout">Logout</a>
 <div id="picked" class="color">
  <div id="choose" class="color-container">
   <div id="red" onclick="colorRed()" class="red color-pick"></div>
   <div id="green" onclick="colorGreen()" class="green color-pick"></div>
   <div id="blue" onclick="colorBlue()" class="blue color-pick"></div>
  </div>
 </div>
</div>

Question is :
1. Why sessionStorage not working
2. Does css transition affected to sessionStorage ? Why am i asking this, because before i'm using css transition the sessionStorage work perfectly.

Thanks..

Upvotes: 0

Views: 1967

Answers (1)

Chris Thorsvik
Chris Thorsvik

Reputation: 470

I would use one function to set all colors, and pass a color variable to it. To initiate a color, you need to get the color value you stored in the sessionStorage and set the background color before the page loads. E.g.

// Generic function to set background color
function setColor(color) {
  document.getElementById("dashboard").style.backgroundColor = color;
  document.getElementById("picked").style.backgroundColor = color;
  document.getElementById("logout").style.color = color;
  sessionStorage.setItem('bgColor', color);
}

// Set color to initial color if it exists
var storedColor = sessionStorage.getItem('bgColor');
if (storedColor != undefined) setColor(storedColor);

And then, change your button function calls to something like:

<div id="red" onclick="setColor('#D64541')" class="red color-pick"></div>
<div id="green" onclick="setColor('#2ECC71')" class="green color-pick"></div>
<div id="blue" onclick="setColor('#6BB9F0')" class="blue color-pick"></div>

Upvotes: 1

Related Questions