Automatically Download PDFs via jQuery

Have you ever wanted to have a link to a resource, such as a PDF file, and have that resource be automatically downloaded when the link is clicked? All you need to do is add a download attribute to your link HTML.

But wait, the download attribute isn’t supported across all the current browsers! It won’t work in Internet Explorer or Safari. So, the next best thing is to make sure that the link opens in a new tab when it can’t be downloaded.

Since you probably can’t count on your content editors to know HTML, much less reliably set these attributes on every link they create, the best thing would be to add this jQuery code snippet to your site:

Now you have a simple, reliable way to handle download links on your site. If you need to force download other file types, just add some code for the file extensions of your choice.

2 thoughts on “Automatically Download PDFs via jQuery”

  1. Hi, Thanks for the code!
    Now it’s easy to make ‘downloadable’ files.
    I tested the download in Chrome and works seamlessly with .pdf, .jpg and .png. But in Firefox (47.0.1) does not work.
    Can you help with this?

Leave a Reply

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