Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...