frosty
frosty

Reputation: 21762

Is form submit synchronous or async?

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.

Upvotes: 14

Views: 10439

Answers (3)

meringue
meringue

Reputation: 33

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.

Upvotes: 1

Sahil Muthoo
Sahil Muthoo

Reputation: 12496

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');
};

Upvotes: 5

SLaks
SLaks

Reputation: 887459

It's an asynchronous call.

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

Upvotes: 17

Related Questions