Matt Lambert
Matt Lambert

Reputation: 3665

Download a file via jquery and redirect

I'd like to figure out a simple way using jQuery to trigger a download when a text link is clicked. Also, after click the user is redirected to success page. Can anyone help?

Upvotes: 4

Views: 24336

Answers (1)

fsong
fsong

Reputation: 666

Let's say you have a link to your download page

<a class="downloadLink" href="www.example.com/foo.pdf">Download</a>

In your Javascript:

$(".downloadLink").click(
    function(e) {   
        e.preventDefault();

        //open download link in new page
        window.open( $(this).attr("href") );

        //redirect current page to success page
        window.location="www.example.com/success.html";
        window.focus();
    }
);

Upvotes: 16

Related Questions