1

Actually this is what I'm trying to do. I have a form and when a user fills that form it goes to a thank you page where thank you message is displayed. Now what I'm trying to do is as soon as the thank you page opens a pdf file should start downloading without the user clicking anywhere on the page. I tried to find a solution by searching but almost all of them have given a solution where one link is provided and user have to click that link to download her pdf. But that is not what I want. And I want to achieve that using Javascript or jQuery only, no server side language.

A clarification:- PDF download on clicking a link is already working. What I want is PDF to download as soon as thank you page opens. I know logically this should not happen because by doing this anyone can set any number of files to be downloaded as soon as their page opens and fill your local drive. But my client wants only this thing to happen.

user3224207
  • 457
  • 4
  • 14
  • just call `elm.click()` on the link that downloads it... – dandavis Sep 17 '15 at 04:41
  • @dandavis:- You seem to have not read my question carefully. I don't want any link to be clicked. Pdf should download as soon as thank you page opens. Download by clicking a link is already working. – user3224207 Sep 17 '15 at 04:43
  • _"Is it possible to force download a file (like pdf) as soon as a page loads"_ Not certain about meaning of "force download" ? – guest271314 Sep 17 '15 at 04:43
  • 2
    i read it. if the human clicking works, then just click() for the user. the file will download when the command is run, and the user won't have to click. simple, easy, effective... – dandavis Sep 17 '15 at 04:44
  • possible duplicate of [Force download a pdf link using javascript/ajax/jquery](http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery) – Imab Asghar Sep 17 '15 at 05:00
  • @Imab Asghar:- My question is different from the link you provide. In that question the asker wants to provide a download button. I don't want to provide any button or link. – user3224207 Sep 17 '15 at 05:07

3 Answers3

3

Calling click() for the user on an other-wise working link should do the trick:

<html>

<a href="/mypdf.pdf" download="mypdf.pdf" id="link">download pdf</a>

<script>
  var link = document.getElementById("link");
  link.click()
</script>
</html>
guest271314
  • 1
  • 15
  • 104
  • 177
dandavis
  • 16,370
  • 5
  • 40
  • 36
3

You can do it, in the HEAD tag:

<meta http-equiv="refresh" content="0; url=yourfile.pdf">

This will work also with browsers without JavaScript, and as soon as the the head tags loaded.

a working example:

See an example on github

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
0

You can use ajax to hit the url when the page loads.

EDIT: You might want to check this out https://stackoverflow.com/a/29266135/4549494

Community
  • 1
  • 1
Imab Asghar
  • 316
  • 1
  • 7