-4

my name is Andrea can you help me please? I have a banner that I show with some text and if the user clicks it it closes but the problem is:

if I reload the page the banner reappears and does not remain closed as clicked on the X button

window.onload = function(){
    document.getElementById('close').onclick = function(){
        this.parentNode.parentNode.parentNode
        .removeChild(this.parentNode.parentNode);
        return false;
    };
};
<div class="card-body">
  <button id="close"><h4>X</h4></button>
  <p class="card-title text-white">configurazione di Seo Tools Manager</p>
</div>
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
  • 1
    Please don't spam tag. – Funk Forty Niner Apr 29 '20 at 18:49
  • 1
    Hello Andrea, welcome to Stack Overflow! Before asking a question, please make sure to do some basic research and try to generalize your question a bit. I think your question is how to make a web page memorize things across reloads. Please look up "cookies": https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – Bernhard Stadler Apr 29 '20 at 19:05

1 Answers1

0

The browser doesn't automatically remember if a user closed the banner. You have to use, for example, localStorage to save that action in the user's browser so, when the page is reloaded, the browser "remembers" that that user closed the banner in the past.

Read more here

window.onload = function() {

  var popup = document.querySelector('.card-body');
  // Check if the user has previously closed the banner and display/hide it accordingly
  if (!localStorage.getItem('closedBanner')) {
    popup.style.display = "block";
  } else {
    popup.style.display = "none";
  }

  document.getElementById('close').onclick = function() {
    // Save into the user's browser that the popup was closed
    localStorage.setItem('closedBanner', "true");

    this.parentNode.parentNode.parentNode
      .removeChild(this.parentNode.parentNode);
    return false;
  };
};
<div class="card-body">
  <button id="close"><h4>X</h4></button>
  <p class="card-title text-white">configurazione di Seo Tools Manager</p>
</div>
ajmnz
  • 742
  • 7
  • 19