javascript 自动关闭带有要下载的文件的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23638252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:16:04  来源:igfitidea点击:

Auto-closing a popup with a file to download

javascriptphpdownloadpopup

提问by Kaye

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

我有一个名为 的文件,download.php其中包含要下载的文件,因此它具有以下标头(用 PHP 声明):

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

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

download.php被其他页面作为带有 jQ​​uery 的弹出窗口打开。

Now I want download.phpto 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 afterheaders):

现在我想download.php在几秒钟后自动关闭(使用 JavaScript)(所以我确信下载开始了)但我没有设法编写工作代码。这是我尝试过的代码(我将它们放在标题之后):

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>.

我试图将这些代码同时<head>放入<body>.

What could be the problem?

可能是什么问题呢?

回答by David Beech

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.

我可以问一下这个打开的窗口中浏览器网址栏中的内容吗?浏览器可能会看到标题,让浏览器知道它将被视为下载而不是将窗口作为真实页面运行。而是打开类似“about:blank”的内容。如果是这种情况,页面上的 javascript 将永远不会运行。

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.

但是,我可以提出以下建议。我假设这个窗口是由另一个页面打开的。在这种情况下,让另一个页面通过 javascript 以编程方式打开窗口并从那里控制关闭。

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

回答by Kaye

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.

完成下载后,新窗口最终将失去焦点并关闭。

回答by svvac

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

您可以使用以下代码段关闭当前窗口(归功于此 SO 答案):

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

To run this after a given interval, simply use setTimeout

要在给定的时间间隔后运行它,只需使用 setTimeout

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