3 javascript php popup download
我有一个名为的文件,download.php其中包含要下载的文件,因此它具有以下标头(用PHP声明):
header("Content-Type: ".pathinfo($_GET['file'], PATHINFO_EXTENSION));
header("Content-Length: ". filesize($_GET['file']));
header("Content-Disposition: attachment; filename=". $_GET['file']);
Run Code Online (Sandbox Code Playgroud)
download.php 用另一个页面的jQuery作为弹出窗口打开.
现在我想download.php在几秒钟后自我关闭(使用JavaScript)(所以我确保下载开始安全)但我没有设法编写工作代码.这是我尝试的代码(我把它们放在标题之后):
window.setTimeout('self.close();', 3000);
window.setTimeout('function(){self.close();}', 3000);
window.setTimeout(self.close();, 3000);
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
self.close();
Run Code Online (Sandbox Code Playgroud)
但无论如何它都不起作用.
我试着将这些代码放入<head>和放入<body>.
可能是什么问题呢?
我可以在这个打开的窗口中询问它在浏览器URL栏中的内容.可能是浏览器看到的标题让浏览器知道它被视为下载并且不会将窗口作为真实页面运行.而是打开像'about:blank'这样的东西.如果是这种情况,页面上的javascript永远不会运行.
不过我可以建议如下.我假设这个窗口正被另一个页面打开.在这种情况下,让另一个页面通过javascript以编程方式打开窗口并从那里控制关闭.
var popout = window.open("http://example.com/download.php");
window.setTimeout(function(){
popout.close();
}, 1000);
Run Code Online (Sandbox Code Playgroud)
我有一个有点不同的建议,在我的情况下效果很好,并且没有任意超时:
var newwindow = window.open("http://example.com/download.php");
newwindow.focus();
newwindow.onblur = function() {newwindow.close(); };
Run Code Online (Sandbox Code Playgroud)
完成下载后,新窗口最终将失去焦点并关闭。