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?
            Asked
            
        
        
            Active
            
        
            Viewed 1,294 times
        
    0
            
            
        
        KyleM
        
- 4,445
 - 9
 - 46
 - 78
 
3 Answers
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
        Brent Roose
        
- 39
 - 5
 
- 
                    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