1

I have over 100 articles. Each article has a different PDF file attached. There's a form where a user must filled and gets redirected to a thank you page. The code is below.

document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'https://example.com/thankyou';
}, false );

In the above code, I would like to add the code below so that it triggers the download. How can I combine this two codes?

$(document).ready(function(){
    $("#download a").trigger("click");
});

The article in HTML format:

<p>Content goes here</p>
<p id="download"><a href="https://example.com/path/file.pdf">Download</a></p>

If there's a better way, please suggest.

Elaine Byene
  • 3,868
  • 12
  • 50
  • 96
  • How does the form relate to the link to the article? Does the link become available after the form is submitted? Are the form and the link on the same page? The context for your question is not clear at all. We need more info i think, before we can give a proper answer – ADyson Nov 08 '19 at 00:52
  • Does this answer your question? [Download File Using Javascript/jQuery](https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) – chandu komati Nov 08 '19 at 03:29
  • @ADyson Yes, the form is a widget which is available in every article. The download PDF file is in the article. However, the file names are different. The ID will be constant. – Elaine Byene Nov 08 '19 at 09:25
  • so after the form is submitted you want to automatically initiate the download, and also redirect them to the thankyou page, is that correct? – ADyson Nov 08 '19 at 09:32
  • @ADyson Yes. Correct. – Elaine Byene Nov 08 '19 at 10:06
  • Ok. If the answer below doesn't work for you, let me know and I'll try and come up with another way – ADyson Nov 08 '19 at 12:17
  • @ADyson It's not working. – Elaine Byene Nov 08 '19 at 23:55

1 Answers1

1

Pass in a get parameter.

Since you're reloading a new page, you should pass along a get parameter to indicate the download should start.

document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'https://example.com/thankyou?download';
}, false );

Then on your page that does have the download file.

$(document).ready(function(){
  if (window.location.search.includes("download")){
    $("#download a").trigger("click");
  }
});

If you had a list on a single page, you could pass an argument in the download parameter ?download="1234.zip" and use that as your target. Please be careful if you use this method so you don't open yourself up to JS injection.

John Pavek
  • 2,595
  • 3
  • 15
  • 33