Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

javascript - Download a file and redirect...or alternative

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";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...