Auto-closing a popup with a file to download

扶醉桌前 提交于 2019-12-04 12:42:59

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.

svvac

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!