13

I am just wondering if the document.myForm.submit() is a synchronous call, that will block until finished... or if it is async and will continue to execute without waiting for the submit to return. Thanks for any help.

frosty
  • 21,036
  • 7
  • 52
  • 74

3 Answers3

17

It's an asynchronous call.

However, at some point, the new page will load, and your page will be destroyed.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Does this mean that a `form.submit(); window.close();` sequence in a popup is wrong because the window may get destroyed before the submission is finished? I'd expect browsers to ensure the request to finish, regardless of the attempt to close the context, but I'm experiencing sporadic update failures on the server side, that's why I'm asking (after half an hour of fruitless googling). – Sz. Mar 04 '18 at 20:08
4

The browser seems to continue to execute javascript immediately after submitting a form. In this jsFiddle example, the log statement is printed before the form is submitted.

Markup

<form action="foobar"></form>
<button id="submitBtn">Submit</button>

Javascript

var button = document.getElementById('submitBtn');
button.onclick = function() {
    document.forms[0].submit();
    console.log('after submitting');
};
Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
1

I had a jsp page that a page-reloading function follows right after a submit method. Then I faced 'unexpected end of part' error immediately. Submit() must be asynchronous.

meringue
  • 33
  • 7