首页 > 解决方案 > 如何在 window.open(url, _blank) 之后开始下载文件

问题描述

我需要的是:

现在,这是我的代码

<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 ...但是如何使用?

谢谢

标签: javascript

解决方案


你没有setTimeout正确使用。您需要传递thanksyou页面 url 并将秒数设置2000为等于2秒数。

与目标一起setTimeout使用,以便它以新的.window.open_blanktab

现场演示:(经过代码测试并在本地主机上运行)

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


推荐阅读