I have a div with a message, which I want to be displayed only when a visitor enters the page for the first time, if he enters the second time it is not displayed. I have this code that I found but it does not work
var mensaje = document.cookie.split('mensaje=')[1]; // obtenemos la cookie "mensaje"
if(mensaje != null) // si la cookie está definida
document.getElementById('mensaje').style.display = 'none'; // ocultamos el div#mensaje
else // si no está definida la cookie
document.cookie = 'mensaje=visto;path=/'; // la agregamos
#mensaje {
padding:20px;text-align:center;font-family:sans-serif;background-color:#FFD7D7;
}
<div id="mensaje">
Prueba. Entraste por primera vez
</div>
Use localStorage as follows.
Just use the code in the way that seems best to you and then you can have the
you can use this code only lasts as long as the session using kookie .