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

624
Visualizações
How to clear localStorage only on browser close

I have a localStorage key (mykey) and want to remove this key only when the browser is closed. It should not be cleared when the page is refreshed.

I have this Javascript code to clear the key on browser close.

window.onbeforeunload = function (event) {
    if (event && event.type == "beforeunload") {
        localStorage.removeItem("mykey");        
    }
};

This code is clearing the localStorage key on browser close and page refresh also. If I comment the code, then it works fine. The key is not cleared on page refresh but only on browser close.

I have observed that when the page is refreshed, the key will be cleared and then the debugger is hit for the window.onbeforeunload method.

There is no other place where I'm removing the key.

UPDATE: I have replaced localStorage with sessionStorage at all places of get set for this key, but it's still the same.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What you basically try to achieve is simply done by sessionStorage object. You can use sessionStorage instead.

localStorage is for data persistance with a different purpose to make sure data stays in the Browser Storage for long.

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