Downloading and Redirecting From the Same Link

Yesterday, I was asked if it would be easy to make a script that starts a download and then redirects the browser to a different web page.
What I came up with is in two parts: An HTML <meta...> page refresh tag to do the download. And JavaScript to do the redirect.
To implement, simply link to a web page of this type:

   content="2; URL=/">
The number of seconds can be changed to any integer, 
     including zero, by changing the number between 
     the quotation mark and the semi-colon following 
The URL can be changed to any URL that initiates the 
     downloading process.


Two seconds after the page finishes loading, will begin downloading. 
(This is done with the <meta...> tag in the head 
area. See notes in the source code about changing URL 
and timing.)
Nine seconds after the page finishes loading, the page 
will redirect to � the 
download may or may not be finished by the time the page 
is redirected. (This is done with the JavaScript located 
below this point in the source code, although it could 
be located pretty much anywhere. See notes in the source 
code about changing URL and timing.)

<script type="text/javascript" language="JavaScript"><!--

function Redirect() {
// The URL between the quotation marks may be changed.
window.location = "";

// The timing may be changed by changing the number in the 
//   setTimeout function call parameters. The number represents 
//   thousandths of a second. Therefore, 9000 is 9 seconds.
window.onload = function() { setTimeout("Redirect()",9000); }



The above is an entire web page that can be loaded into a browser as is.
Once the above example web page is completely loaded, it waits 2 seconds then initiates a file download. This is caused by the HTML <meta...> page refresh tag in the head area of the web page.
At 9 seconds after the example web page is completely loaded (7 seconds after the download kicks in), the browser window is redirected to another URL. This is caused by some JavaScript.
If the page refresh URL is a 404 or causes a web page or other file to load into the browser window, rather than an automatic download to begin, then the JavaScript redirect won't happen at all.
The page refresh download URL should be to an automatically downloadable file only. That would be a file with an extension like .zip or .exe. If unsure. test. Don't specify files with extension .pdf — some browsers automatically download .pdf files, but others load the PDF document into the current browser window.
The example web page contains some instructions.
Note that the download might or might not be finished by the time the redirect occurs. If not, the download continues even thought the browser window loads a page at a different URL.
The URLs and timing can be changed. See the notes in the example web page.
The download must be initiated before the web page is redirected. The redirection should occur one or more seconds after the download is initiated.
If the redirect happens before the download is initiated, the download won't happen.
Will Bontrager

Post a Comment