سؤال

I have a file, named download.php, which contains a file to be downloaded, so it has the following headers (declared with PHP):

header("Content-Type: ".pathinfo($_GET['file'], PATHINFO_EXTENSION));
header("Content-Length: ". filesize($_GET['file']));
header("Content-Disposition: attachment; filename=". $_GET['file']);

download.php is opened as a popup with jQuery by an other page.

Now I want download.php to self-close (using JavaScript) after some seconds (so I'm secure that download started) but I didn't manage to write working code. Here are the codes I tried (I placed them after headers):

window.setTimeout('self.close();', 3000);
window.setTimeout('function(){self.close();}', 3000);
window.setTimeout(self.close();, 3000);

I also tried simply:

self.close();

but it does not work anyway.

I tried to put these codes both in <head> and in <body>.

What could be the problem?

هل كانت مفيدة؟

المحلول 2

Can I ask what it says in the browser url bar in this opened window. It might be the case that the browser see's the headers letting the browser know it is to be treated as a download and doesn't run the window as a true page. and instead opens something like 'about:blank'. If that's the case the on the page javascript would never get run.

I can suggest the following however. I'm assuming this window is being opened by another page. In that case have the other page open the window programatically through javascript and control the close from there.

var popout = window.open("http://example.com/download.php");
window.setTimeout(function(){
    popout.close();
}, 1000);

نصائح أخرى

I have a somewhat different proposal, which worked fine in my case and does not have an arbitrary timeout:

var newwindow = window.open("http://example.com/download.php");
newwindow.focus();
newwindow.onblur = function() {newwindow.close(); };

When finishing the download the new window will eventually unfocus and close.

You may use the following snippet to close the current window (credits to this SO answer) :

window.open(window.location, '_self').close();

To run this after a given interval, simply use setTimeout

setTimeout(function() { window.open(window.location, '_self').close(); }, 3000);

If you don't need extra user actions on the download page, you can do a "trick".... Put an hidden iframe somewhere in the page which contains the link to the download page

<iframe id="dwn_frame" src="" height="0" width="0"></iframe>

So, in your page you should have something like this:

<script>
  function download_page() {
    $('#dwn_frame').attr('src', 'my_dwn_page.php');
}
</script>

 <a href="javascript: download_page()">Download File</a>

<iframe id="dwn_frame" src="" height="0" width="0"></iframe>

This way, the user clicks on "Download File", the link triggers the function "download_page", this function sets the source attribute of the iframe, the iframe loads your download page which prepares the file and the download starts without popups or similar.

I have used Onblure but it didn't help at all!

I used below code and it is working for me.

var popupWindow = window.open("endpoint","","setting");
var att = document.createAttribute("onblure");  // Create a "onblure" attribute
att.value = "window.close()";                  // Set the value of the class attribute
popupWindow.setAttributeNode(att); 

This will allow closing the window when download window appears

Note: endpoint: is your new window endpoint setting: is your window setting for example width, height,top, left .. etc

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top