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

117
Visualizações
I am trying to close the window after executing some code but the window is not closing, tried window.close(); and close(); still not working

//close window section is the problem, it just won't close the current tab. I am jus running some code to download a file and then I require the tab to close automatically as if nothing happened to make the UX better. But window.close(); and close(); is not working. Tried a lot of stuff found on Internet still not working. What's wrong with my code?

var url_string = window.location; //window.location.href
var url = new URL(url_string);
var file = url.searchParams.get("file");
var dFile = file;

function onLoad() {
  if (dFile !== null) {
    var hiddenElement = document.createElement('a');

    // Download
    hiddenElement.href = `${dFile}`;
    hiddenElement.target = '_blank';
    hiddenElement.download = `${dFile}`;
    hiddenElement.click();

    //Close Window
    var win = window.open("about:blank", "_self");
    win.close();
  } else {

  }
}

//Set The value of the File being Downloaded
if (dFile !== null) {
  var SetFile = document.getElementById('xdFile')
  SetFile.innerHTML = "Downloading" + " " + dFile + " " + "now"
}

about 4 years ago · Juan Pablo Isaza
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