0

//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"
}
Barmar
  • 741,623
  • 53
  • 500
  • 612

0 Answers0