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

84
Vistas
Document ready event of another location opened using window.location in javascript

I am using below code to open a link on button click. The link is pointing to a Controller method responsible for downloading some Excel file.

        // Button to download table data
        $("#btnDownloadCIRResults").click(function (e) {
            var All_Recs = $("#cbShowAllRecords").prop("checked") ? "YES" : "NO";

            DisplayStatusMessageWind("Downloading report, please wait...", MessageType.Info, true, false);

            // DownloadCIRemediationTable(string AllRecords)
            window.location = '/AskNow/DownloadCIRemediationTable?AllRecords=' + All_Recs;

            DisplayStatusMessageWind("Report downloaded successfully.", MessageType.Success, false, true, 1000);
            e.preventDefault();
        });

The Controller method queries a DB table, converts it to an Excel workbook and returns a file as download result. All is working fine and as expected, except, since this is a time consuming process, I just want to improve on user experience and update this code to show some wait message while the file is being downloaded.

The DisplayStatusMessageWind() method shows a wait message. However, it doesn't know or care about the load complete event of the window.location = '/AskNow/DownloadCIRemediationTable?AllRecords=' + All_Recs; code.

How can I make the completion message appear only after the file download is completed:

DisplayStatusMessageWind("Report downloaded successfully.", MessageType.Success, false, true, 1000);

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

By assigning a new location with window.location = "<NEWURL>"; you're requesting asynchronously to replace the current page. What will happen, is that the next line is immediately executed (DisplayStatusMessage()). When all events are handled, the page will finally be replaced. The new page (URL) will load and you'll have no control whatsoever about how or what will happen next.

What you should do is use window.open("<NEWURL>", '_blank') MOZ and then on the new page send a signal via localStorage, which can be read and written by all pages of the same domain. These are some hints, to write the actual code is your job.

On this page, in on("click") event:

// local scope
var ukey;

// polling function
function waitOtherIsReady()
{
  if (localStorage.getItem(ukey) === true)
  {
    // other page experienced ready event
    localStorage.removeItem(ukey); // clean-up

    // TODO: do your stuff

  } else {
    setTimeout(waitOtherIsReady, 500);
  }
}

// create unique key and deposit it in localStorage
ukey = "report_" + Math.random().toString(16);
localStorage.setItem(ukey, false);

// pass key to other page
window.open("URL?ukey=" + ukey, "_blank");

// start polling until flag is flipped to true
setTimeout(waitOtherIsReady, 500);

On the other page:

$(() => {
  // get ukey from URL
  var ukey = new URL(window.location.href).searchParams.get("ukey");

  // page is now ready, flip flag to signal ready event
  localStorage.setItem(ukey, true);
});
about 4 years ago · Santiago Gelvez 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