JS. Function of return in "Turn" JS event handler

Source: Internet
Author: User

Tagged with: GPO href = = Val tde blog meaning mit ack

1. Function of return in JS event handler-answercard-Blog Park. HTML (https://www.cnblogs.com/answercard/p/5255230.html)

2, the content of the website to save:

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

Returns false to prevent the default action from being supported by all browsers this is the basic component of an event handler. Today's event handler model also adds new ways to block default actions:

    • The Preventdefalut () method was added to the event. If you refer to this method, the default action will be blocked.
    • Microsoft added the ReturnValue attribute to the event. If you set his value to False then the default action will also be blocked.

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

<HTML><Head><title>Return validation Test</title><Scriptlanguage= "JavaScript">functionLogin_click () {if(Document.form1.UsName.value=="") {alert ('user name is empty');}if(Document.form1.UsPwd.value=="") {alert ('password is empty');} Alert ('Landing Success');}</Script></Head><Body><formname= "Form1"><inputtype= "text"name= "Usname" >User name<inputtype= "Password"name= "Uspwd">Password<inputtype= "button"name= "Login"OnClick= "Login_click ();" >Login</form></Body></HTML>

The case of not adding return

Add return

<HTML><Head><title>Return validation Test</title><Scriptlanguage= "JavaScript">functionLogin_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></Head><Body><formname= "Form1"><inputtype= "text"name= "Usname" >User name<inputtype= "Password"name= "Uspwd">Password<inputtype= "button"name= "Login"OnClick= "Login_click ();" >Login</form></Body></HTML>

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.

3.

4.

5.

JS. Function of return in "Turn" JS event handler

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.