Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A form's "action" and "onsubmit": Which executes first?

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 forms, I'm wondering what the order of execution is for the action and onsubmit.

like image 575
Not John Skeet Avatar asked Mar 12 '15 15:03

Not John Skeet


People also ask

What happens Onsubmit?

The onsubmit event is an event that occurs when you try to submit a form. You can put your form validation against this event type. The following example shows how to use onsubmit. Here we are calling a validate() function before submitting a form data to the web server.

What is the purpose of a form's action attribute?

The action attribute is used to specify where we want to send the form data when the form is submitted. So the value of the action is the page that will process the form.

What happens when Onsubmit returns false?

It means that do nothing on submit.

What does Onsubmit mean in JavaScript?

Definition and Usage The onsubmit event occurs when a form is submitted.


2 Answers

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 in onsubmit, so it isn't.

Event handlers run before default actions. They can cancel default actions.

like image 163
Quentin Avatar answered Sep 19 '22 07:09

Quentin


The onsubmit must execute first, as returning false from it stops the form being submitted, and thus the action ever being requested.

like image 33
Jamiec Avatar answered Sep 19 '22 07:09

Jamiec