使用JS window.open _self下载zip文件而不使用“无用窗口” [英] Downloading zip file using JS window.open _self without "useless window"
问题描述
以下来自 此页 讨论了无用的空窗口的问题,我正在尝试创建一个开始下载zip文件的脚本:
Following information from this page where the issue of the "useless empty window" is discussed, I'm trying to create a script that starts the download of a zip file:
<script>window.open('archive.zip','_self')</script>
以上内容正确启动,但该页面完全空白,无可见内容。我可以看到整个源被加载但不显示。
The above starts the download correctly but the page is then completely blank, no content visible. I can see the entire source is loaded but not displayed.
如何在加载页面内容的同时不使用无用的窗口来使用js异步启动下载? / p>
How can I use js to start the download asynchronously while loading page content, without a "useless window"?
推荐答案
您可以使用隐藏的iframe。使用_self时,window.open将清除您的所有页面内容。
You can use a hidden iframe instead. The window.open will clear all your page content when using _self.
这是一个简单的例子
<a href="archive.zip" target="download_frame">Initiate download from link</a>
<iframe id="download_frame" name="download_frame" src="about:Blank" style="width:0px; height:0px; overflow:hidden;" frameborder="0" scrolling="no"></iframe>
<script type="text/javascript">
// initiate download by script
// add this in onload event or after the iframe
document.getElementById('download_frame').src="archive.zip";
</script>
这篇关于使用JS window.open _self下载zip文件而不使用“无用窗口”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!