Original: http://blog.csdn.net/gchonghavefun/article/details/8112830
The return in this includes some knowledge of the details:
For example: The difference between onclick= ' return Add_onclick () ' and onclick= ' Add_onclick () '
When JavaScript calls a function in an event, the return value is actually set to Window.event.returnvalue.
The value determines whether the current operation continues.
When True is returned, the operation continues.
When the return is false, the operation is interrupted.
While executing directly (without return). The window.event.returnvalue will not be set
As a result, the operation will continue by default
Detailed description is as follows:
For example:
When in <a href= "abc.htm" onclick= "return Add_onclick ()" >Open</a>
If the function Add_onclick () returns True, then the page will open abc.htm
Otherwise, (return false), the page will not jump to abc.htm, only the contents of your Add_onclick () function will be executed. (except for the Add_onclick function in the control page go to abc.htm
)
and <a href= "abc.htm" onclick= "Add_onclick ()" >Open</a>
Regardless of the value returned by Add_onclick (), the page will open after Add_onclick is executed abc.htm
Additionally add:
The onclick event is equivalent to onclick= "return True/false"
Cases:
function Check ()
{
if (obj.value== "")
{
Window.alert ("Can't be empty!") ");
Obj.focus ();
return false;
}
return true;
}
When the calling method returns True, the form is submitted, and the reverse is not committed, which is the submit button
------------------------------------------------------------------------------------------
Calling the JS function does not require return, but the form cannot be submitted, so add a sentence to the JS function
Cases:
<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: Document.myform.submit (); To return true before
About return false and return true in JavaScript
Return is the keyword for the function return value in JavaScript.
The result of a function's processing can be returned using return,
This allows you to receive a return from a variable where the function is called.
Results. Variable data or expression of any type within the return keyword
Can be returned without even returning anything, such as
function Nullreturn (IsNull)
{
if (isnull==true)
{
Return
}
}
It's also possible to write this, which means return null (NULL)
So sometimes the function of return is to terminate the execution of functions.
Like what
<title>return Verification Test </title>
<script language= "JavaScript" >
function Login_click ()
{
if (document.form1.usname.value== "")
{
Alert (' User name is null ');
}
if (document.form1.uspwd.value== "")
{
Alert (' Password is null ');
}
Alert (' Landing success ');
}
</script>
<body>
<form name= "Form1" >
<input type= "text" name= "usname" > User name
<input type= "Password" name= "uspwd" > Password
<input type= "button" name= "Login" onclick= "Login_click ();" > Login
</form>
</body>
The case of not adding return
Add return
<title>return Verification Test </title>
<script language= "JavaScript" >
function Login_click ()
{
if (document.form1.usname.value== "")
{
Alert (' User name is null ');
Return
}
if (document.form1.uspwd.value== "")
{
Alert (' Password is null ');
Return
}
Alert (' Landing success ');
}
</script>
<body>
<form name= "Form1" >
<input type= "text" name= "usname" > User name
<input type= "Password" name= "uspwd" > Password
<input type= "button" name= "Login" onclick= "Login_click ();" > Login
</form>
</body>
Running will find the difference between adding return and not adding return,
The simplest test method, the above two examples do not enter any direct login
The phenomenon of no return is to first prompt the user name is not entered, and then prompts the password is not entered; add return after encountering a no input after no longer
Return false means that a false value is returned, which means that the commit is unsuccessful or not committed.
The return True table method returns a value of True, which is submitted to the action specified page, regardless of the input value you entered.
function of return in JS event handler function