Users fill out a form. With a valid form, they click to download a file. I would like to be able to redirect them to another page (e.g., page2.html
) AND initiate a download.
I can't initiate download on page2.html
because most browsers will flag security warnings if a download link isn't directly clicked on.
How is something like this accomplished using javascript or jquery? Obviously the following doesn't work but it's meant for illustrative purposes:
document.getElementById("foo").onclick = function(){
window.location = "/download.exe";
window.location = "/page2.html";
}
You could use a link to the file and onclick
to trigger "go to the next page shortly".
<script>
function thanks() {
setTimeout(function () {
document.location.pathname = "page2.html";
}, 1000);
}
</script>
<a href="file.exe" onclick="thanks()">Download now!</a>
You can initiate the download on page 2 by having page2.html
"change" its location (to your download URL) once the page is loaded:
$(function ()
{
setTimeout(function ()
{
window.location.pathname = '/download.exe';
}, 5000); // download the file in 5 seconds
});
I'm pretty sure (though not 100%) that won't trigger a security warning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With