57

The question is pretty self-explanatory. I don't understand what the return is doing in the following code:

<form onsubmit="return somefunction()">
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zjmiller
  • 2,709
  • 4
  • 29
  • 30

6 Answers6

73

You need the return so the true/false gets passed up to the form's submit event (which looks for this and prevents submission if it gets a false).

Lets look at some standard JS:

function testReturn() { return false; }

If you just call that within any other code (be it an onclick handler or in JS elsewhere) it will get back false, but you need to do something with that value.

...
testReturn()
...

In that example the return value is coming back, but nothing is happening with it. You're basically saying execute this function, and I don't care what it returns. In contrast if you do this:

...
var wasSuccessful = testReturn();
...

then you've done something with the return value.

The same applies to onclick handlers. If you just call the function without the return in the onsubmit, then you're saying "execute this, but don't prevent the event if it return false." It's a way of saying execute this code when the form is submitted, but don't let it stop the event.

Once you add the return, you're saying that what you're calling should determine if the event (submit) should continue.

This logic applies to many of the onXXXX events in HTML (onclick, onsubmit, onfocus, etc).

Parrots
  • 26,658
  • 14
  • 59
  • 78
17

An extension to what GenericTypeTea says - Here is a concrete example:

<form onsubmit="return false">

The above form will not submit, whereas...

<form onsubmit="false">

...does nothing, i.e. the form will submit.

Without the return, onsubmit doesn't receive a value and the event is executed just like without any handler at all.

zb226
  • 9,586
  • 6
  • 49
  • 79
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
13

Returning false from the function will stop the event continuing. I.e. it will stop the form submitting.

i.e.

function someFunction()
{
    if (allow) // For example, checking that a field isn't empty
    {
       return true; // Allow the form to submit
    }
    else
    {
       return false; // Stop the form submitting
    }
}
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • 1
    But why won't the function return false if we just had onSubmit="someFunction()"? – zjmiller Mar 04 '11 at 15:46
  • For example, function test() {return "asdf"}; alert(test()); triggers an alert that reads "asdf", but I didn't have to say alert(return test()); – zjmiller Mar 04 '11 at 15:48
  • 1
    @zjmiller - Because `return` indicates that the value of the expression should be returned to the submit routine. If the expression evaluates to false, the submit routine is cancelled. – djdd87 Mar 04 '11 at 15:52
  • 3
    To amplify GenericTypeTea's excellent explanations, think of onsubmit as a Javascript variable and the stuff in the quotes as a Javascript function. So both "someFunction()" and "return somefunction()" will cause someFunction() to be executed, but "someFunction()" causes the return value from someFunction() to be discarded, whereas "return someFunction()" assigns the return value to the onsubmit variable. This is all due to the loosey-goosey nature of Javacript, where you're allowed to define functions any way you want, so the interpreter has to accept whatever you do... – Sheldon R. Jun 26 '14 at 15:09
9

When onsubmit (or any other event) is supplied as an HTML attribute, the string value of the attribute (e.g. "return validate();") is injected as the body of the actual onsubmit handler function when the DOM object is created for the element.

Here's a brief proof in the browser console:

var p = document.createElement('p');
p.innerHTML = '<form onsubmit="return validate(); // my statement"></form>';
var form = p.childNodes[0];

console.log(typeof form.onsubmit);
// => function

console.log(form.onsubmit.toString());
// => function onsubmit(event) {
//      return validate(); // my statement
//    }

So in case the return keyword is supplied in the injected statement; when the submit handler is triggered the return value received from validate function call will be passed over as the return value of the submit handler and thus take effect on controlling the submit behavior of the form.

Without the supplied return in the string, the generated onsubmit handler would not have an explicit return statement and when triggered it would return undefined (the default function return value) irrespective of whether validate() returns true or false and the form would be submitted in both cases.

mshalaby
  • 141
  • 1
  • 5
  • Yes.. that is what I want to read; `onsubmit` is actually a function body, works like `{...}` of a function, not an attribute. – WesternGun Mar 16 '18 at 08:11
  • Thank you for the helpful description (and evidence!) of what is actually happening behind the scenes - this really answers the question with a explanation that aids understanding. – Andrew Merrill Jan 10 '20 at 22:09
5

HTML event handler code behaves like the body of a JavaScript function. Many languages such as C or Perl implicitly return the value of the last expression evaluated in the function body. JavaScript doesn't, it discards it and returns undefined unless you write an explicit returnEXPR.

Denis Howe
  • 2,092
  • 1
  • 23
  • 25
-2
<script>
function check(){
    return false;
}
 </script>

<form name="form1" method="post" onsubmit="return check();" action="target">
<input type="text" />
<input type="submit" value="enviar" />

</form>
  • How does this answer the question? In addition, the question is two years old. Please try putting your effort in unanswered questions instead. – Mifeet Jun 01 '13 at 16:08