[英]Javascript Download file link is closing popup window before download popup
I'm creating a simple feature where I need to create a download file link which gives a popup window to download and as soon as user saves or cancel the download, the window should close. 我正在创建一个简单的功能,我需要创建一个下载文件链接,该链接提供一个弹出窗口供下载,并且一旦用户保存或取消下载,该窗口就会关闭。 This is the JS code I used.
这是我使用的JS代码。
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
window.close();
}
If I remove the window.close() statement, it works smooth but with window.close(), the new window closes before it could throw a popup window for download. 如果我删除window.close()语句,它可以顺利运行,但使用window.close(),新窗口将关闭,然后再抛出弹出窗口以供下载。 Can anybody here please help?
请问有人可以帮忙吗?
I was able to fix this eventually adding the HTML block 我能够解决这个问题,最终添加了HTML块
<body onload="closeWindow()"></body>
<script>
function closeWindow() {
self.close();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.