Giulio Gualtieri
Giulio Gualtieri

Reputation: 23

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

what i need is:

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?

Thanks

Upvotes: 2

Views: 1089

Answers (1)

Always Helping
Always Helping

Reputation: 14570

You are not using the setTimeout correctly. You need to pass the thanksyou page url and set the seconds to 2000 which are equivalent to 2 seconds.

In setTimeout use window.open with a target _blank so that it opens in a new 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()

Upvotes: 1

Related Questions