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

95
Vistas
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 Respuestas
Responde la pregunta

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 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