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

94
Visualizações
Easly catch page reload in JavaScript

I need to do something when the browser is closing, so first I need to know that it closes. What I found was:

<script>
var closing = true;
$(function () {
  $("a,button[type=submit]").click(function()
    {
      closing=false;
  });
  $(window).on("unload",function(){
      if (closing){...}
    });
  });
</script>

It works good/nice/etc but there is one "catch": page reload... When the page is reloaded, it's not any click, so it's treated as "closing"... Is there such an easy way to catch the reload (as for the "click" written above)?

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

0

Try to use navigation object property, there you can find the information page is reloaded(value is equal to 1) or not:

<script>
var closing = true;
var performance = window.performance;
$(function () {
  if((performance.navigation && performance.navigation.type === 1) ||
    performance
      .getEntriesByType('navigation')
      .map((nav) => nav.type)
      .includes('reload')){
        closing = false;
        console.log("reloaded");
  }
  $("a,button[type=submit]").click(function()
    {
      closing = false;
  });
  $(window).on("unload",function(){
      if (closing){
       console.log("closing");
      }
  });
});
</script>
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