Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
Display a message once (like a cookie consent) on first visit

I'm building a website and am trying to display a message at the top of the page (inside the header) so it only appears once on every visit/session. An example of this the 'Book an appointment' green bar at the top of this website: https://www.tiffany.co.uk

My website is here: https://vitrify.tempurl.host/

I've got as far as having a message appear (orange panel at top of page) but currently it appears every time a page is loaded. I just want it to appear once, just like a cookie consent.

I've spent hours looking for a solution but, as I'm not a programmer, I'm struggling. Any help would be much appreciated.

Here's the HTML:

function myFunction() {
  var x = document.getElementById("topDIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
.topDIV {
  color: #000000;
    font-weight: 300;
  font-size: 15px;
}

.topDIV a:link, .topDIV a:visited {
  color: #000000!important;
  font-weight: 500;
  letter-spacing: -0.3px;
    line-height: 1.2        ;
}

span.topDIV {

}

.topDIV a:hover {
        border-bottom: 1px solid rgba(0,0,0,0.50) !important;
    display: inline-block;
}

.button-x {
  position: relative;
  float: right;
  top: -5px;
    background:none; 
    border:none; 
    color:rgb(0,0,0) ;
    cursor: pointer; 
    vertical-align: 0px;
}
.button-x:before {
    font-family: 'Times';
    content: "X";
        font-size:30px;
        vertical-align:0px;
    opacity:0.5;
}
.button-x:hover {
    opacity:1!important;
}
<span class = "topDIV">Welcome to <em>Vitrify</em>. Following in the finest traditions of vitreous enamelled jewellery. <a href = "#">Find&nbsp;out&nbsp;more.</a></span><button class = "button-x" onclick="myFunction()"></button>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

As mentioned above you need to use localStorage to solve your problem.

  • Need to know when page is loaded
  • Get value from the localStorage
  • Add event to the button (i removed the onclick event from the html for a cleaner solution)
  • After click set value for localStorage and hide item
  • If localStorage have value, hide the element

working example

Javascript

document.addEventListener('DOMContentLoaded', function () {
  const topDiv = document.querySelector('.topDIV');
  const buttonX = document.querySelector('.button-x');

  // Get value from localStorage when open the page
  const lS = localStorage.getItem('first-visit');

  // Add button 'click' event
  buttonX.addEventListener('click', () => {
    // Set value to the localStorage
    localStorage.setItem('first-visit', false);
    // hide DOM element
    topDiv.style.display = 'none';
  });

  // This does not check on the first visit to the page
  // If localStorage have value, hide DOM element
  if (lS) topDiv.style.display = 'none';
});

Html

<span class="topDIV"
  >Welcome to <em>Vitrify</em>.Following in the finest traditions of
  vitreous enamelled jewellery.
  <a href="#">Find out more.</a>
</span>
<button class="button-x"></button>
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add a flag "showTopMessage:true" in your local storage or session storage based on one time msg to user or every time he visits. respectively.

On Cross/close icon click set the flag "showTopMessage:false".

       const showTopBar = "SHOW_TOP_BAR"; 
       const[showTopBar,setShowTopBar] = useState(true);

      useEffect(()=>{
        const saveState = localstorage.getItems(showTopBar);

        if(saveState) setShowTopBar(saveState);
        else localstorage.setItem(showTopBar, true);
      },[]);

      const handleTopBarClose = () => {
        setShowTopBar(false);
        localstorage.setItem(showTopBar, false);
      }
 

      return(
      <div>
      {
        showTopBar && <TopBarComponent onClose={handleTopBarClose}/>
      }
      </div>
      )

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda