简体   繁体   中英

HTTP form submit callback

I'm posting a form that causes a file to be downloaded. So my browser page does not change. My form is eg 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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM