[英]Javascript Download file link is closing popup window before download popup
我正在創建一個簡單的功能,我需要創建一個下載文件鏈接,該鏈接提供一個彈出窗口供下載,並且一旦用戶保存或取消下載,該窗口就會關閉。 這是我使用的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();
}
如果我刪除window.close()語句,它可以順利運行,但使用window.close(),新窗口將關閉,然后再拋出彈出窗口以供下載。 請問有人可以幫忙嗎?
我能夠解決這個問題,最終添加了HTML塊
<body onload="closeWindow()"></body>
<script>
function closeWindow() {
self.close();
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.