Pop up open once a day by javascript user?

Asked

Viewed 441 times

1

I’m creating my website, and I created a popup basic with html, css3, javascript and would like to know how I do to catch the cookie user and display the popup only once on my site, follow the syntax below.

window.onload = function() {
  function fechar() {
    var popup = document.getElementById('popup');
    var cor = document.getElementById('cor');
    popup.style.display = "none";
    cor.style.display = "none";
  }
}
.popup {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 500px;
  height: 300px;
  padding: 15px;
  border: solid 1px black;
  background: url(https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png) no-repeat;
  z-index: 2;
  display: none;
}
.popup p {
  position: relative;
  top: -30px;
  left: 0;
  color: white;
}
.popup p a {
  position: relative;
  top: 68px;
  left: 178px;
  text-decoration: none;
  color: black;
}
img[alt="fechar"] {
  height: 26px;
  width: 28px;
  position: relative;
  left: 454px;
  top: -14px;
}
<body>
  <div class="popup" id="popup">
    <a href="javascript:fechar()">
      <img src="img/fechar.png" alt="fechar">
    </a>
    <p>Inscreva-se em meu canal do youtube Leonardo Santos link abaixo</p>
    <p><a href="#" target="_blank">clique aqui</a>
    </p>
  </div>

1 answer

1

You can create a date object:

d = new Date()

Then increase the date by one day:

d.setDate(d.getDate() + 1*24*60*60*1000)

And finally create a cookie that expires after one day:

document.cookie = "visto=true; expires=" + d.toUTCString() + ";path=/"

To find out if a day has passed: document.cookie == null

For more see this example here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.