简体   繁体   English

如何在 window.open(url, _blank) 之后开始下载文件

[英]How to start download a file after window.open(url, _blank)

what i need is:我需要的是:

  • user click a link用户点击链接
  • the download start automatically下载自动开始
  • after 2 sec a new tab will open whit the URL of the Thank You page 2 秒后,将打开一个新选项卡,其中显示感谢页面的 URL

Now, this is my code现在,这是我的代码

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

Can you provide the correct code?你能提供正确的代码吗? i know i can use window.open...but how?我知道我可以使用 window.open...但是怎么做呢?

Thanks谢谢

You are not using the setTimeout correctly.您没有正确使用setTimeout You need to pass the thanksyou page url and set the seconds to 2000 which are equivalent to 2 seconds.您需要通过thanksyou page url 并将秒数设置为2000 ,相当于2秒。

In setTimeout use window.open with a target _blank so that it opens in a new tab .setTimeout中,将window.open与目标_blank一起使用,以便它在新tab中打开。

Live Demo: (Code tested and its working on localhost)现场演示:(代码测试并在本地主机上运行)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM