Using Session storage to store whether a popup has been shown

I have a website with a popup I built using plain HTML, CSS, and JS. Nothing fancy. I choose to display my popup after 15 seconds. Because I don't want to show the popup to anyone who's already seen it, I want to use session storage to store the fact I've displayed the popup.

I came up with this code:

setTimeout(function() {
    var today = new Date();
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    if (sessionStorage[mm+"_displayed"] != 'y'){
      var modal = document.getElementById("myModal"); = "block";
      sessionStorage[mm+"_displayed"] = 'y';
  }, 15000);

The popup shows up after 15 seconds as expected. I can also see the session storage value in Chrome's Developer Tools. However I sometimes notice that the popup comes back up again.

  • Is my comparison wrong?
  • Is session storage specific to each tab? So far that's what I found but it seems counterintuitive. Sessions are shared across tabs so why wouldn't session storage be?
  • Is local storage still "live forever"? I found a great post on the topic and a workaround.


Read more here:

Content Attribution

This content was originally published by David Brossard at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: