JS return false with return true

Source: Internet
Author: User
Tags tagname

In most cases, false is returned for the event handler to prevent the default event behavior. For example, by clicking on a <a> element by default, the page jumps to the page specified by the href attribute of the element.
Return False equals the Terminator, and return True is the equivalent of the executor.
The function of return false in JS is generally used to cancel the default action. For example, you click on a link to trigger your
The onclick time (if you specify it) to trigger a default event is to perform a jump on the page. So if
You can return FALSE if you want to cancel the object's default action. return false application of more than the occasion is:
<form name= "Form1" onsubmit= "return Youfunction ();" A. </form>
<a href= "www.***.com" mce_href= "www.***.com" onclick= "...; return false; " &GT;DDDD </a>.
<mce:script type= "Text/javascript" ><!--
function test ()
{
Alert ("Test");
return False ();
}
--></mce:script>
<body>
1, <a href= "a.jsp" mce_href= "a.jsp" onclick= ' Test (); ' > Hyperlinks </a>
2, <input type= "button" onclick= ' Test () ' value= "Submit" >
3, <form name= "Form1" onsubmit= "return Test ();" >
Content
<input type= "Submit" value= "Submission" >
</form>
</body>
<a href= "xxxx.do?....." mce_href= "xxxx.do?....." target=dolink> connect ABCD </a>
<iframe id=dolink src= "About:blank" mce_src= "About:blank" style= "Display:none" mce_style= "Display:none" > </ Iframe>

Click on the top of the connection after the Dolink frame to perform the page processing things (xxxx.do? ... Pass parameters to the server), the page will not jump.
A recently-made project submits a form through a function that is bound to the OnClick event of the button. It is also important to find that return is false. Like what:
<input type= "Submit" onclick= "submitaction (); return false; "/>
The Submitaction method has the action of submitting the form. If you do not add return False, the Submit button will continue to execute its default event after Submitaction executes, and the form will be submitted again. This may be the source of many mistakes.
Indeed, the meaning of return false is not to prevent the event from continuing to propagate to the top-level element, but to block the browser's default handling of events. You can experiment with this: first comment out all the JS script, in the Internet Explorer to try to drag a piece, you will find that the mouse will be a prohibited operation of the style, the picture is forbidden to drag, it is the browser for the MouseMove event provides the default behavior. Return false is to remove this behavior, or the interrupt event that you describe is executed consecutively.
In addition, the statement equivalent to return false is: Window.event.returnValue = False, you can replace the return false with this statement and verify.
Finally, this approach applies only to IE browsers.
<mce:script language= "JavaScript" ><!--
function Verifyform () {
var ass=document.getelementsbyname ("Assetname");
if (ass[0].value.length==0) {
Alert ("Name input is empty!") ");
return false;
}
if (ass[0].value.length>15) {
Alert ("Name input length cannot exceed 15 characters!") ");
return false;
}
}
--></mce:script>
<body>
<form name= "XX" >
<input name= "Assetname" type= "text"/>
<--determines whether to submit the form based on the return value of TRUE or false--->
<input type= "Submit" onclick= "return Verifyform ()"/>
</form>
</body>
Diving to see the great prawns of the wonderful post, this also contributed a little experience. The common appreciation of its smell, doubts and analysis. If there is anything wrong, please do not hesitate to shoot bricks.
As we all know, add onsubmit= "return false" to the form; You can prevent form submissions.
Here is a simple piece of code:
View Plaincopy to Clipboardprint?
<form action= "index.jsp" method= "post" onsubmit= "submittest ();;" >
<input value= "www" >
<input type= "Submit" value= "Submit" >
</form>

<script language= "JavaScript" >
<!--
function Submittest (); {
Some logical judgments
return false;
}
-
</SCRIPT>
<form action= "index.jsp" method= "post" onsubmit= "submittest ();;" >
<input value= "www" >
<input type= "Submit" value= "Submit" >
</form>
<script language= "JavaScript" >
<!--
function Submittest (); {
Some logical judgments
return false;
}
-
</SCRIPT>
Everyone judge like the above wording, click the Submit button is the form submitted?
If the answer is yes, you don't have to look down.
If the answer is no, it is wrong. The fact is that the form is normally submitted, if you want it not to commit, you should
View Plaincopy to Clipboardprint?
<form action= "index.jsp" method= "post" onsubmit= "submittest ();;" >
<form action= "index.jsp" method= "post" onsubmit= "submittest ();;" > instead
View Plaincopy to Clipboardprint?
<form action= "index.jsp" method= "POST" onsubmit= "return Submittest ();;" >
<form action= "index.jsp" method= "POST" onsubmit= "return Submittest ();;" >
Why?
The original OnSubmit property is like a method name for the <form> HTML object whose value (a string) is its method body, which returns true by default;
Like Java, you can write any number of statements, including built-in functions and custom functions, in the body of the method, such as
View Plaincopy to Clipboardprint?
Onsubmit= "
Alert (' haha ');; Built-in functions
Submittest (); Custom functions
alert (this.tagname);; Use this keyword
...... (any number of statements);
return false;
"
Onsubmit= "
Alert (' haha ');; Built-in functions
Submittest (); Custom functions
alert (this.tagname);; Use this keyword
...... (any number of statements);
return false;
"
is equivalent to
View Plaincopy to Clipboardprint?
Form.prototype.onsubmit = function (); {
Alert (' haha ');; Built-in functions
Submittest (); Custom functions
alert (this.tagname);; Use this keyword
...... (any number of statements);
return false;
};
Form.prototype.onsubmit = function (); {
Alert (' haha ');; Built-in functions
Submittest (); Custom functions
alert (this.tagname);; Use this keyword
...... (any number of statements);
return false;
};
In that case, you'll overwrite (override) Its default method (returns True by default)
It is noted that this keyword can be used in the method body, which represents the object instance of <form>.
After this analysis, the above situation is not difficult to understand:
View Plaincopy to Clipboardprint?
<form action= "index.jsp" method= "post" onsubmit= "submittest ();;" >
<form action= "index.jsp" method= "post" onsubmit= "submittest ();;" > This, the effect of the override method is:
View Plaincopy to Clipboardprint?
Form.prototype.onsubmit = function (); {
Submittest ();
};
Form.prototype.onsubmit = function (); {
Submittest ();
};
Here Submittest () returns false, but we only execute this function and do nothing with its result. and
View Plaincopy to Clipboardprint?
<form action= "index.jsp" method= "POST" onsubmit= "return Submittest ();;" >
<form action= "index.jsp" method= "POST" onsubmit= "return Submittest ();;" >
The effect of the override method is:
View Plaincopy to Clipboardprint?
Form.prototype.onsubmit = function (); {
return submittest ();;
};
Form.prototype.onsubmit = function (); {
return submittest ();;
};
In this way, we have used its return value to achieve the desired effect.
I think the impression will be much deeper, it's not easy to make mistakes.
Conclusion:
We can think in Java about similar scenarios in analog JavaScript (prototype-based object-oriented technology in JavaScript does), but they still have a fundamentally different nature, such as Java is strongly typed and has strict syntax limitations , while JavaScript is loosely shaped. Like the above method:
View Plaincopy to Clipboardprint?
Form.prototype.onsubmit = function (); {
};
Form.prototype.onsubmit = function (); {
};
There can be no return value, and no return value, in Java is not possible, after all, Java can not rely on the return value of the method to overload (overload) method, and JavaScript overload is much looser.

JS return false with return true

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.