gpt4 book ai didi

javascript - 使用 JS window.open _self 下载 zip 文件,不带 "useless window"

转载 作者:行者123 更新时间:2023-12-02 16:54:28 25 4
gpt4 key购买 nike

以下信息来自this page 在讨论“无用的空窗口”问题时,我正在尝试创建一个启动 zip 文件下载的脚本:

<script>window.open('archive.zip','_self')</script>

上面的内容正确启动下载,但页面完全空白,没有内容可见。我可以看到整个源代码已加载但未显示。

如何使用js在加载页面内容的同时异步开始下载,而不出现“无用的窗口”?

最佳答案

您可以使用隐藏的 iframe。使用_self时,window.open将清除所有页面内容。

这是一个简单的示例

<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>

关于javascript - 使用 JS window.open _self 下载 zip 文件,不带 "useless window",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com