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

234
Visualizações
How to detect refresh action(by clicking refresh icon in browser)?

I want to restrict the page refresh only when we doing this by clicking the refresh icon at the top left side of the browser.

I have tried more available options like,

'beforeunload' event, which can fire on each time of unloading(refresh, navigation, close) the browser. Inside the event, I have tried 'window.performance.navigation.type' and window. performance.getEntrieaByType('navigation').map(nav=>nav.type) options. But both aren't giving the correct result. On each actions like "refresh", "navigation" and "close" getting the same result. Also sometimes getting different result. So I amn't trusting the options.

So can anyone help me to get the correct option to detect the refresh action done by browser refresh icon?

I am using react application.

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

0

window.onbeforeunload = ()=>{
    localStorage.setItem("unload",Date.now());
}
useEffect(){
    let last_unload = localStorage.getItem('unload');
    let decided_time = 1000;
    if(last_unload){
        // means the user have closed the page 
        if(Date.now() - parseInt(last_unload,10) <= 1000)
        {
            // this is a refresh
            console.log('refresh handling code')
        }else{
            // It's a new session, means user came to page, closed the browser, now came back after a long time
        }
    }
}

Generally the answer from here

should work but because you are stating that window.performance is ambiguous in your case try something like this. Let me know if this helps!

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