0

I have a .php file running that is generating a downloadable file; when the .php file runs it opens a tab in the browser. Occasionally the .php file takes up to 15 seconds depending on the conditions to create the file.

I would like to know when this tab is open generating the downloadable file and when it closes. This way I can have some sort of loading message displayed while the file is being generated. When the .php file is done creating the file it automatically closes the tab.

Code:

var win = window.open(download, '_blank'); //opens the php file which generates the file.
if (win)
{
    win.focus();
    //have some sort of message stating to wait for the file to download here and then close it when the php file finishes running.
}
else
{
    alert("Please allow popups.");
}

closePopup2();
vector
  • 199
  • 1
  • 4
  • 17

2 Answers2

0

Give your window a unique id so the script will know which one to check (maybe needs to be random if opened/closed multiple times). I don't know if you need to focus, and popup permission is for you to figure out, but I think following should work:

var win = window.open(download, 'windowID', 'width:300px;height:300px');
win.document.write("<p>One moment please!</p>");
win.focus();

var test = setInterval(function() {   
    if(win.closed) {  
        clearInterval(test);  
        // do you stuff here after window closed
    }  
}, 500); 
yezzz
  • 2,990
  • 1
  • 10
  • 19
0

I would suggest not opening a PHP file in a new tab, but using XMLHttpRequest(), you can find a guide on how to use it on MDN

You can use it like this:

function reqListener () {
  console.log(this.responseText); // Will log full output of page
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", download);
oReq.send();
Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
  • Rather than the file being output to the page, how do I output this as a downloadable file, like what was being returned when I did the var win = window.open(download, '_blank');? Using your code the file is just output to the console, but I want it to be a downloadable file. – vector Mar 17 '16 at 14:29
  • Mmmm... I am not sure how to do that. You could try `window.open(this.responseText, '_blank')`? – Kaspar Lee Mar 17 '16 at 14:41
  • I tried the solution that you proposed by replacing console.log(this.responseText); with window.open(this.responseText, '_blank');. This is causing a new tab to pop open but I am receiving a 403 Forbidden error, so I added a user name and password to the oReq.open() request, this did not solve the issue. I am doing more investigation, but I believe this may be close to the solution. Let me know if you have any further ideas. I will continue to research and if I can get it to work, I will accept your answer! Thank you. – vector Mar 17 '16 at 14:53
  • Just to check, you want a window to pop open, and then the default alert saying "Do you want to download this file / Save As?" – Kaspar Lee Mar 17 '16 at 15:04
  • Actually, check out [this answer](http://stackoverflow.com/a/1968116/5620297), and [this simpler answer](http://stackoverflow.com/a/985090/5620297). It sets the headers to basically state the data is a downloadable file. Try that and see if it works... – Kaspar Lee Mar 17 '16 at 15:11
  • Things seem to work using this method. I want a loading message while the file is being generated. I am able to get the download to pop up using, window.open(globalDownload, '_blank'). – vector Mar 17 '16 at 15:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106621/discussion-between-druzion-and-vector). – Kaspar Lee Mar 17 '16 at 15:36