csauve
csauve

Reputation: 6263

How does form.submit timing work?

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.

Upvotes: 3

Views: 303

Answers (1)

SLaks
SLaks

Reputation: 887459

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.

Upvotes: 3

Related Questions