Force download a pdf link using javascript/ajax/jquery

suppose we have a pdf link “http://manuals.info.apple.com/en/iphone_user_guide.pdf“(just for example and to let u know that file is not on my server, i only have the link)…now i have to provide a button on my site that will download the file.

i have tried various things like window.open, href etc. methods but it open the link on other window. i know thats because now all browser comes with a adobe plugin which opens it in another window, but still isnt there any way i give the user the option of download rather than opening it, through client side scripting ..

plz help.. thanks

Answer

Use the HTML5 “download” attribute

<a href="iphone_user_guide.pdf" download="iPhone User's Guide.PDF">click me</a>

2021 Update: Now supported in all major browsers! see: caniuse.com/#search=download

If you need to support older browsers, or you’re looking for an actual javascript solution (for some reason) please see lajarre’s answer

Leave a Reply

Your email address will not be published. Required fields are marked *