Deep understanding of the role of return in JavaScript _ basics

Source: Internet
Author: User

The return here contains some details:

For example: The difference between onclick= ' return Add_onclick () ' and onclick= ' Add_onclick () '

When JavaScript calls a function in an event, returning the value with return is actually setting the Window.event.returnvalue.

This 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
Therefore, 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, the page opens abc.htm
Otherwise, (returns false), the page does not jump to abc.htm, only the contents of your Add_onclick () function are executed. (except for the control page in the Add_onclick function, go to abc.htm

)
and <a href= "abc.htm" onclick= "Add_onclick ()" >Open</a>
Regardless of what value Add_onclick () returns, the page opens after the Add_onclick is executed abc.htm

Additionally added:
The onclick event is equivalent to onclick= "return True/false"
Cases:

Copy Code code as follows:

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, but not submitted, which is the submit button
------------------------------------------------------------------------------------------

Call JS function does not need to return, but the form is not submitted, so in the JS function add a Word
Cases:

Copy Code code as follows:

<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 be before return true

About return false and return true in JavaScript
Return is the keyword in JavaScript that returns the value of a function, and the result of a function can be returned with return, so that the return result can be received with the variable at the point where the function is called. Any type of variable data or expression within the return keyword can be returned, or even nothing is returned.

Copy Code code as follows:

function Nullreturn (IsNull)
{
if (isnull==true)
{
Return
}
}

It is also possible to write, which means to return null (NULL)
So sometimes the return action is used to terminate function execution.
Like what
Copy Code code as follows:

<title>return Verification Test </title>
<script language= "JavaScript" >
function Login_click ()
{
if (document.form1.usname.value== "")
{
Alert (' User name is empty ');
}
if (document.form1.uspwd.value== "")
{
Alert (' Password is empty ');
}
Alert (' Landing success ');
}
</script>
<body>
<form name= "Form1" >
<input type= "text" name= "Usname" > Username
<input type= "Password" name= "uspwd" > Password
<input type= "button" name= "Login" onclick= "Login_click ();" > Login
</form>
</body>

The situation without return
Add return
Copy Code code as follows:

<title>return Verification Test </title>
<script language= "JavaScript" >
function Login_click ()
{
if (document.form1.usname.value== "")
{
Alert (' User name is empty ');
Return
}
if (document.form1.uspwd.value== "")
{
Alert (' Password is empty ');
Return
}
Alert (' Landing success ');
}
</script>
<body>
<form name= "Form1" >
<input type= "text" name= "Usname" > Username
<input type= "Password" name= "uspwd" > Password
<input type= "button" name= "Login" onclick= "Login_click ();" > Login
</form>
</body>

The difference between adding return and no return is found in the operation.
The simplest test method, the above two examples of nothing input direct landing, the building will understand.

The phenomenon of no return is to prompt the user name is not entered, and then prompted the password is not entered; After the return of a return after a no input and then no longer continue to detect

Return False indicates that a false value is returned, which means that the commit is unsuccessful or is not committed. The
return True table method returns a true value, which commits the submission to the action-specific page, regardless of whether you enter a value or not.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.