简体   繁体   中英

Session Storage Pop Up Not Using Session Storage

I am designing a website and have a pop up that appears. The pop up functions how I would like it, appearing when the page is visited and goes away when the 'x' is pressed in the top corner of the pop up. But, I want the pop up to go away (for that session) after being 'x'ed out. After some investigation, I have found the session storage is the way to do this. I have tried using session storage, but I can't get it to work. I am totally new to Java Script so please bare with my terrible code.

HTML:

<div id="pop-up"> 
    <i onclick="fcnx1(),setTimeout(fcnx2, 700)" class="fas fa-times fa-2x" style="padding: 4%; float:right;"></i>
    <p>Have your logo here! If you are interested in becoming a sponsor, feel free to email us at <a href="mailto:sponsor@beaverauv.org">sponsor@beaverauv.org</a>.</p>
</div>

CSS:

#pop-up {
  position: absolute;
  bottom: 7%; 
  right: 0;
  height: 50%;
  width: 30%;
  background: #F0F0F0;
  border-top: solid 5px rgb(25,28,31);
  border-bottom: solid 5px rgb(25,28,31);
  border-left: solid 5px rgb(25,28,31);
  transition:all 385ms linear;
}

#pop-up p {
  text-align: center;
  margin:5%;
  position: absolute;
  top:50%;
  transform: translateY(-50%);
}

.hide {
  opacity: 0;
}

.gone {
  display: none;
}

JavaScript:

<script type="text/javascript">
    if(sessionStorage.getItem('hidepopup') == true){
        document.getElementById("pop-up").classList.toggle("gone")
    } 

    function fcnx1() {
        document.getElementById("pop-up").classList.toggle("hide");
        sessionStorage.setItem('hidePop', true);
    }

    function fcnx2() {
        document.getElementById("pop-up").classList.toggle("gone");
    }
</script>

Data stored in sessionStorage is of type string , not boolean , so do eg like this and quote the value "true"

A note, you also spelled the key 'hidepopup' different in the get/set call.

if(sessionStorage.getItem('hidepopup') == "true"){
    document.getElementById("pop-up").classList.toggle("gone")
} 

function fcnx1() {
    document.getElementById("pop-up").classList.toggle("hide");
    sessionStorage.setItem('hidepopup', "true");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM