Dig into what return does in javascript

  • 2020-03-30 01:08:05
  • OfStack

This return contains some details:

For example: onClick='return add_onclick()' versus onClick='add_onclick()'

When JAVASCRIPT calls a function in an event, it actually sets the window.event.returnvalue by returning the value.

This value determines whether the current operation continues.
When true is returned, the operation continues.
When false is returned, the operation is broken.

When executed directly (without return). Window.event.returnvalue will not be set
So it will continue to execute by default

Details are as follows:
Such as:
When in < A href = "ABC. HTM" onclick = "return add_onclick ()" > Open< / a> In the
If the function add_onclick() returns true, the page opens abc.htm
Otherwise, (returns false), the page will not jump to abc.htm, only execute your add_onclick() function

)
While < A href = "ABC. HTM" onclick = "add_onclick ()" > Open< / a>
Whatever value add_onclick() returns, the page abc.htm will open after the execution of add_onclick

In addition:
Onclick ="return true/false"
Ex. :


function check()
{
if(obj.value=="" )
   {
     window.alert(" Can't be empty! ");
     obj.focus();
     return false;
   }
     return true;
}

The form is not submitted until the method returns true. Otherwise, it is not submitted. This is the submit button
------------------------------------------------------------------------------------------

Calling a js function does not require a return, but the form cannot be submitted, so add a sentence to the js function
Ex. :


<script language="javascript">
function check()
{
if(obj.value=="" )
   {
     window.alert(" Can't be empty! ");
     obj.focus();
     return false;
   }
     document.myform.submit();
     return true;
}
</script>

Note: the document. Myform. Submit (); Before return true

 

Return false and return true in javascript
Return is the keyword in javascript that returns the value of a function. The result processed in a function can be returned by using return, so that the result can be received with a variable at the place where the function is called. Any type of variable data or expression in the return keyword can be returned, or even nothing can be returned, such as


function NullReturn(IsNull)
{
if(IsNull==true)
{
return;
}
}

It's also ok to write this way, which means to return null.
So sometimes what return does is it terminates a function.
Such as

<html>
<head>
<title>return Verification test </title>
<script language="javascript">
function Login_Click()
{
if(document.form1.UsName.value=="")
{
alert(' User name null ');
}
if(document.form1.UsPwd.value=="")
{
alert(' Password is empty ');
}
alert(' Log in successfully ');
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="UsName" > The user name 
<input type="password" name="UsPwd"> password 
<input type="button" name="Login" onClick="Login_Click();" > landing 
</form>
</body>
</html>

No return
With the return

<html>
<head>
<title>return Verification test </title>
<script language="javascript">
function Login_Click()
{
if(document.form1.UsName.value=="")
{
alert(' User name null ');
return;
}
if(document.form1.UsPwd.value=="")
{
alert(' Password is empty ');
return;
}
alert(' Log in successfully ');
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="UsName" > The user name 
<input type="password" name="UsPwd"> password 
<input type="button" name="Login" onClick="Login_Click();" > landing 
</form>
</body>
</html>

If you run it, you'll see the difference between return and no return,
The simplest test method, the above two examples nothing input directly login, building will understand.

The phenomenon without return is that the user name is not input, and then the password is not input; After adding return, it encounters an untyped message and then stops checking

Return false means to return a false value, which means that the commit is unsuccessful, or it will not be committed.
Return true table method returns a true value, that is, a commit, whether you enter the value or not, will be committed to the action page.


Related articles: