Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda