0

Is there a way to stop form submission AFTER it has already been started.
Here is what I mean. I have a code like this:

<form method="POST"
      action="/hell">
    <input type="text" name="filename">
    <input type="submit">
</form>

<script type="text/javascript">
    $('form').on('submit', function (event) {

        setTimeout(function () {
            // And here i need to stop the form submission process
        }, 1000);

    });
</script>

To simplify the example let's say that the form target page never answers faster than in couple seconds.
What I really want to achieve here is something like non-ajax submission abortion. Is it even possible?

ADDED:
I'm trying to make a jQuery plugin (for some specific needs) and in order to send files with a form I use iframe to fake synchronous ajax request. But can't find a way to abort the submission on i timeout.
Yes, I'm aware of FormData (which is not suitable for me, I can sweep aside IE < 9, but IE 9 support is required), as well as about a bunch of plugins, which still use iframe under the hood (none of them can serve my needs).

1 Answers1

0

No, there isn't. By the time the timeout function runs, the browser will have already submitted the form and be loading the new page.

If you want to cancel the submission conditionally than the closest you could come would be to always prevent the default form behaviour and then conditionally restart it (by calling submit()) in the timeout.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Hm... But it still won't abort submission in 1 second, it will start it in 1 second. – Spaghetti.Coder Jan 10 '15 at 11:07
  • @Spaghetti.Coder — Yes, I said that. – Quentin Jan 10 '15 at 11:07
  • And there is no way to stop loading the new page, right? – Spaghetti.Coder Jan 10 '15 at 11:12
  • @Teemu That's actually the point. As I said I'm trying to do it without AJAX, which has some unpleasant limitations (most annoying is it can't send files). Yes, I'm aware of FormData (which is not suitable for me, I can sweep aside IE < 9, but IE 9 support is required), as well as about a bunch of plugins, which still use iframe under the hood. I'm trying to make a jQuery plugin (for some specific needs) and in order to send files I use iframe to fake synchronous ajax request. But can't find a way to abort the submission on i timeout. I guess this explanation deserves a place in the head post – Spaghetti.Coder Jan 10 '15 at 12:46