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 <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), then the page will not jump to abc.htm, but will only execute the content in your add_onclick() function. (Except for the add_onclick function to control the page to go to abc.htm
)
And <a href="abc.htm" onclick="add_onclick()">Open</a>
No matter what value add_onclick() returns, the page abc.htm will be opened after add_onclick is executed.
In addition:
Onclick event is equivalent to onclick="return true/false"
example:
Copy the code code as follows:
function check()
{
if(obj.value=="" )
{
window.alert("Cannot be empty!");
obj.focus();
return false;
}
return true;
}
The form will be submitted only when the calling method returns true, otherwise it will not be submitted. This is the submit button.
-------------------------------------------------- ----------------------------------------
There is no need to return when calling the js function, but the form cannot be submitted, so add a sentence to the js function
example:
Copy the code code as follows:
<script language="javascript">
function check()
{
if(obj.value=="" )
{
window.alert("Cannot be empty!");
obj.focus();
return false;
}
document.myform.submit();
return true;
}
</script>
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 a variable can be used to receive the return result where the function is called. Any type of variable data or expression within the return keyword can be returned, or even nothing can be returned, for example
Copy the code code as follows:
function NullReturn(IsNull)
{
if(IsNull==true)
{
return;
}
}
It is also possible to write like this, what it means here is to return null (null)
So sometimes the role of return is to terminate function execution.
for example
Copy the code code as follows:
<html>
<head>
<title>return verification test</title>
<script language="javascript">
function Login_Click()
{
if(document.form1.UsName.value=="")
{
alert('Username is empty');
}
if(document.form1.UsPwd.value=="")
{
alert('Password is empty');
}
alert('Login successful');
}
</script>
</head>
<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>
</html>
Without return
Add return
Copy the code code as follows:
<html>
<head>
<title>return verification test</title>
<script language="javascript">
function Login_Click()
{
if(document.form1.UsName.value=="")
{
alert('Username is empty');
return;
}
if(document.form1.UsPwd.value=="")
{
alert('Password is empty');
return;
}
alert('Login successful');
}
</script>
</head>
<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>
</html>
When you run it, you will find the difference between adding return and not adding return.
The simplest test method is to log in directly without inputting anything in the two examples above, and you will understand.
The phenomenon without 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 when a user is not entered.
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.