Reputation: 437
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.
Upvotes: 1
Views: 7789
Reputation: 24590
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:
Upvotes: 3
Reputation: 16726
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>
Upvotes: 3
Reputation: 316
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
Upvotes: 0