Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is form submit synchronous or async?

Tags:

javascript

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.

like image 575
frosty Avatar asked Nov 02 '11 19:11

frosty


People also ask

What is submit () method used for?

The form submit() Method in HTML DOM is used to send the form data to the web-server.

What is form submit in JavaScript?

The method form. submit() is used for dynamic creation and sending the details to the server. The JavaScript form submission can be used for object creation and various attributes can also be used. The attributes can be class, id, tag, etc.

Is async await asynchronous or synchronous?

As the following two examples show, the async and await keywords result in asynchronous code that looks a lot like synchronous code.

What event triggers when a form is submitted?

The onsubmit event occurs when a form is submitted.


2 Answers

It's an asynchronous call.

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

like image 122
SLaks Avatar answered Sep 20 '22 13:09

SLaks


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');
};
like image 43
Sahil Muthoo Avatar answered Sep 20 '22 13:09

Sahil Muthoo