javascript - 如何在 window.open(url, _blank) 之后开始下载文件
问题描述
我需要的是:
- 用户点击链接
- 下载自动开始
- 2 秒后将打开一个新选项卡,其中包含感谢页面的 URL
现在,这是我的代码
<script type="text/javascript">
function DownloadAndRedirect()
{
var DownloadURL = "url-of-the-file";
var RedirectURL = "url-of-thank-you-page";
var RedirectPauseSeconds = 2;
location.href = DownloadURL;
setTimeout(DoTheRedirect(+RedirectURL+),parseInt(RedirectPauseSeconds*1000));
}
function DoTheRedirect(url) { window.location=url; }
</script>
你能提供正确的代码吗?我知道我可以使用 window.open ...但是如何使用?
谢谢
解决方案
你没有setTimeout
正确使用。您需要传递thanksyou
页面 url 并将秒数设置2000
为等于2
秒数。
与目标一起setTimeout
使用,以便它以新的.window.open
_blank
tab
现场演示:(经过代码测试并在本地主机上运行)
function DownloadAndRedirect() {
var DownloadURL = "https://www.google.com/";
var RedirectURL = "https://www.google.com/";
location.href = DownloadURL;
setTimeout(function() {
window.open(RedirectURL, '_blank')
}, 2000)
}
DownloadAndRedirect()