Javascript 如何在window.open(url,\u blank)之后开始下载文件

Javascript 如何在window.open(url,\u blank)之后开始下载文件,javascript,Javascript,我需要的是: 用户单击链接 下载将自动启动 2秒后,将打开一个新选项卡,其中包含感谢页面的URL 现在,这是我的代码 <script type="text/javascript"> function DownloadAndRedirect() { var DownloadURL = "url-of-the-file"; var RedirectURL = "url-of-thank-you-page";

我需要的是:

  • 用户单击链接
  • 下载将自动启动
  • 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>

函数下载andredirect()
{
var DownloadURL=“文件的url”;
var RedirectURL=“感谢页面的url”;
var RedirectPauseSeconds=2;
location.href=下载URL;
setTimeout(dotheredDirect(+RedirectURL+),parseInt(RedirectPauseSeconds*1000));
}
函数dotherDirect(url){window.location=url;}
你能提供正确的代码吗? 我知道我可以打开窗户,但怎么打开呢


谢谢

您没有正确使用
设置超时
。您需要传递
thanksyou
页面url,并将秒数设置为
2000
,这相当于
2

设置超时
中,使用
窗口。使用目标
打开
\u空白
,以便在新的
选项卡
中打开

实时演示:(已测试代码并在本地主机上运行)

函数下载andredirect(){
var DownloadURL=”https://www.google.com/";
var重定向URL=”https://www.google.com/";
location.href=下载URL;
setTimeout(函数(){
window.open(重定向URL“\u blank”)
}, 2000)
}

下载andredirect()
@GiulioGualtieri很高兴听到这个消息。快乐编码:)