0

I'm posting a form that causes a file to be downloaded. So my browser page does not change. My form is e.g. form action="/download/file.txt". I'm aware that I could use JQuery and download the file with AJAX and use the JQuery callback mechanisms. However, I don't want to use JQuery because this is a learning experience. Is there any way to call a Javascript function when my file has downloaded?

KyleM
  • 4,445
  • 9
  • 46
  • 78

3 Answers3

0

There is no way to do this without AJAX. So either you use JQuery's AJAX call, or just plain JS, and use the onreadystatechange event (http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp). There is no JS event you can trigger when the submit is "completed" by setting it as an attribute on the form (like onClick="" etc).

So plain JS is possible with AJAX, but you'd just be making it yourself harder

  • If you use XMLHttpRequest, the file won't be downloaded, it will be put into a Javascript variable. – Barmar Apr 21 '15 at 19:05
0

you can use

<form action="nextStep.php" onsubmit = "return function(){
                 //download the file here **download/file.txt**
                 //if(downloaded == successful)
                 // set the flag as true
                 //else
                 // set the flas as false

}" >

if it is good then go the nextStep.php file

Shelly
  • 355
  • 1
  • 7
0

The answer here uses an iframe to load the static file and get the notification in its onload event. You can check the same.

Community
  • 1
  • 1
sujit
  • 2,258
  • 1
  • 15
  • 24