I'm trying to debug a webpage and I see a form element whose opening is
<form name="aspnetForm" method="post" action="default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm">
Having only a base knowledge of web form
s, I'm wondering what the order of execution is for the action
and onsubmit
.
If
action
was resolved first, then the browser would leave the page, the JS execution environment would go away, and there would be nowhere to run the JS inonsubmit
, so it isn't.Event handlers run before default actions. They can cancel default actions.
This is explained in the HTML5 spec:
Therefore, at step 5, the
submit
is fired, and can be canceled to prevent form submission. The action is resolved after that.The
onsubmit
must execute first, as returning false from it stops the form being submitted, and thus theaction
ever being requested.onsubmit
is executed first in order to check the format etc. Thenaction
is executed to get/post the data to the backend.