Home > Web Front-end > JS Tutorial > body text

In-depth understanding of the role of return in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 17:06:09
Original
1039 people have browsed it

The return here contains some detailed knowledge:

For example: the difference between onClick='return add_onclick()' and onClick='add_onclick()'

JAVASCRIPT uses return when calling a function in an event to actually set the window.event.returnvalue.

This value determines whether the current operation continues.
When true is returned, the operation will continue.
When the return is false, the operation will be interrupted.

When executed directly (without return). window.event.returnvalue will not be set
so the operation will continue by default

Details are as follows:
For example:
When in Open
If the function add_onclick( ) Return true, then the page will open abc.htm
Otherwise, (return false), then the page will not jump to abc.htm, and will only execute the content in your add_onclick() function. (In the add_onclick function Except for controlling the page to go to abc.htm

)
AndOpen
No matter what value add_onclick() returns, it will be opened after add_onclick is executed. Page abc.htm

In addition:
onclick event is equivalent to onclick="return true/false"
Example:

Copy code The code is as follows:

function check()
{
if(obj.value=="" )
{
window.alert("Cannot Empty! ");
obj.focus();
return false;
}
return true;
}

when the calling method returns true Submit the form, otherwise do not submit, this is the submit button
---------------------------------- -------------------------------------------------- -----

Return is not required to call the js function, but the form cannot be submitted, so add a sentence in the js function
Example:

Copy code The code is as follows:



Note: document.myform.submit(); must be before return true

About return false and return true in javascript
return is the keyword for function return value in javascript. The result processed within a function can be returned using return, so that where the function is called Variables can be used to receive the returned results. Any type of variable data or expression within the return keyword can be returned, or even nothing can be returned, such as

Copy code The code is as follows:
function NullReturn(IsNull)
{
if(IsNull==true)
{
return;
}
}

It is also possible to write like this. What it means here is to return empty (null)
So sometimes the function of return is to terminate function execution.
For example

Copy code The code is as follows:

< ;head>
return validation test




Username
Password
Login




Without return
With return

Copy code The code is as follows:



return validation test




Username
Password
Login




When you run it, you will find whether to add return or not The difference between return and
is the simplest way to test. In the two examples above, you can log in directly without inputting anything. You will understand.

The phenomenon of not adding return is that the user name is not entered first, and then the password is not entered; after adding return, it will no longer continue to detect after encountering a failure to enter

Return false means returning a false value, which means that the submission is unsuccessful, that is, it will not be submitted.
The return true table method returns a true value, which means it is submitted. Regardless of whether you enter a value or not, it will be submitted to the action specified page.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template