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

201
Vistas
beforeunload event and Content-Disposition=attachment

I recently add on my code something like this:

$(window).on('beforeunload', function () {
    $('.whirly-loader').show(); //SPINER
})

So anytime the user go to another side of my web the spinner show up. And it works most of the time. But, in some part of the app the client start going another side and the server response with this headers:

Cache-Control
    max-age=0, must-revalidate, private
Connection
    Keep-Alive
Content-Disposition
    attachment;filename=suministro.csv
Content-Type
    text/csv; charset=utf-8
[...]

This prevents the reload of the page and only show up the window to ask to download or open the document. My problem is the spinner still show up even if the page stop load

Which should be the event to hide my spinner even if the page don't reload because of the headers?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

  1. This jquery plugin uses the window object to bind it's beforeunload event to the body element making it easy to implement and offer a a generic solution.

;(function($, window, document){

  $.fn.plgn = function() {

    //Start the loader when the event beforeunload
    $(window).on('beforeunload', function(event){
      event.stopPropagation();
      console.log("whirly-loader show");

      //Hide the loader after 5 seconds if the page fails to load
      setTimeout(function(){
        console.log("whirly-loader hide");  
      }, 5000);
    });

  }

})(jQuery, window, document);

//Start the loader as soon as javascript loads
console.log("whirly-loader show");

$( document ).ready(function() {
  //Hide the loader when the page is completely loaded
  console.log("whirly-loader hide");
  $("body").plgn();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" value = "Refresh page" onclick="window.location.reload();" />

about 4 years ago · Juan Pablo Isaza Denunciar

0

We had a similar problem and came to the solution to do magic (in your case, show a spinner) manually, like:

$('.js-show-spinner').addEventListener('click', event => {
  event.preventDefault();
  $('.whirly-loader').show(); //SPINER
});
<a href="https://google.com">google.com</a>
<a class="js-show-spinner" href="/home">Home</a>
<a href="SOME_DOC" download>some doc</a>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add attribute target for download links.

Example:

<body onbeforeunload="document.body.style.backgroundColor = 'red';">
    <a href="output.zip" target="_blank">Download</a>
</body>

Attribute target="_blank" will cause, that onbeforeload event will not fire.

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