0

The following Javascript will bring up a pdf file in a new browser page:

var url = "http://company.com/myfile.pdf";
window.location.href = url;

How can I change this so that the script initiates a download of the file, given the url? While the HTML5 attribute 'download' doesn't work in IE, I'd like to use it, but I'm not sure how.

The download attribute can be used like this:

<a href="./directory/yourfile.pdf" download>Download the pdf</a>

But I don't have a link like that on my page. Can I create a dummy tag and assign an href? But I'm not sure how to initiate the download programmatically.

George Hernando
  • 2,550
  • 7
  • 41
  • 61
  • So, you don't want a download link, but instead a script that forces a download of a file..? The question is a bit unclear. – Daniel B Feb 13 '15 at 22:33
  • It's a button that will do some processing via Ajax and ultimately create a PDF file that needs to be downloaded. – George Hernando Feb 13 '15 at 23:27

1 Answers1

0

I don't think you're allowed to trigger a download using javascript due to security reasons.

See: Force download through js or query

Community
  • 1
  • 1