Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does form.submit timing work?

Tags:

javascript

I've come across some javascript code in our codebase that stands out as dangerous to me.

function SaveAndClose()
{
  //etc etc some validation
  TheForm.submit();
  window.close();
}

Perhaps I am misunderstanding how form submission timing works. I was hoping someone could enlighten me: 1) When does the form submit happen?
2) How does the close affect the form submission? Is it possible that there is a race condition here?

I originally expected that the close would not execute (execution would terminate at the submit), but the behavior I am seeing is that both the submit and close are executing (in FF) - the data is being persisted to the DB (via the page of the form's post action), and the window is indeed being closed.

like image 734
csauve Avatar asked May 19 '26 05:05

csauve


1 Answers

submit() is an asynchronous call.
It kicks off an HTTP request in the background, then continues running the code on the page.

I don't know whether closing the browser window will abort the HTTP request.
However, if this is in a popup window, the form is likely to be submitting to the parent window (using the target attribute), so it wouldn't matter.

like image 90
SLaks Avatar answered May 21 '26 21:05

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!