skip to Main Content

I’m trying to make a program that when you visit the site, it shows the div, a button is pressed to hide it, and it isn’t shown again until chrome is closed and reopened.

function check() {
  if (sessionStorage.alreadyClicked) {
    document.getElementsByClassName('disclaimer-container')[0].style.display = "none";
    sessionStorage.alreadyClicked = 1;
}

I’ve tried adding sessionStorage.alreadyClicked to by code as seen here to no avail.

No matter what I do the div will always show. I’m looking a cross browser solution in the long term but as of now just chrome.

2

Answers


  1. this should work , although I recommend to get the div by id not class because the following function will hide only the first one because of the [0] so you need to make sure that it will always be first at this case. -thank you VLAZ-

    function check() {
      if (sessionStorage.getItem('alreadyClicked') != 1) {
        document.getElementsByClassName('disclaimer-container')[0].style.display = "none";
         sessionStorage.setItem('alreadyClicked',1);
    }
    
    Login or Signup to reply.
  2. From that same page:

    “Be careful with sessionStorage because it can only store string values.”

    Right now your if statement is essentially checking if that storage exists, which will always return true.

    Try this instead:

    
    function check() {
      if (!sessionStorage.alreadyClicked = “y”) {
        document.getElementsByClassName('disclaimer-container')[0].style.display = "none";
        sessionStorage.alreadyClicked = “y”;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search