Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!