JavaScript return false detailed

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.

<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.

As we all know, add onsubmit= "return false" to the form; You can prevent form submissions.

<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>
The above code is the fact that the form is normally submitted, if you want it not to commit, you should

<form action= "index.jsp" method= "post" onsubmit= "Submittest ();" >

Switch

<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;

In this method body you can write any number of statements, including built-in functions and custom functions, such as:

onsubmit= "alert (' haha '); Built-in functions

Submittest (); Custom functions

alert (this.tagname); Use this keyword

...... (any more than one statement)

return false; "


is equivalent to

Form.prototype.onsubmit = function () {

Alert (' haha '); Built-in functions

Submittest (); Custom functions

alert (this.tagname); Use this keyword

...... (any more than one statement)

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:

Code <form action= "index.jsp" method= "post" onsubmit= "Submittest ();" >

In this case, the override method has the following effects:

Code Form.prototype.onsubmit = function () {submittest ();};


Here Submittest () returns false, but we only execute this function and do nothing with its result.

And the code <form action= "index.jsp" method= "POST" onsubmit= "return Submittest ();" >
The effect of the override method is:

Code Form.prototype.onsubmit = function () {return submittest ();};


In this way, we have used its return value to achieve the desired effect.


Conclusion: We can think in Java about similar scenarios in analog JavaScript (prototype-based object-oriented technology in JavaScript does), but they are fundamentally different, like Java is strongly typed, There are strict syntax restrictions, and JavaScript is loosely shaped. Like the above method:
Form.prototype.onsubmit = function () {


};

Can have a return value, and can not return the value, in Java is not possible, after all, Java can not rely on the return value of the method to overload (overload) method, and the overload in JavaScript is much looser

JavaScript return false detailed

Related Article

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.