JavaScript return False

Source: Internet
Author: User

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.

<a class= "Testreturn" href= "http://www.baidu.com" > Baidu </a>$ (". Testreturn"). On (' click ',function  (e) {        alert ("test");         return false ;    }) // links do not jump
<form action= "form_action.asp" method= "POST"  id= "Subform" >        <p>first Name: <input type= "Text" Name= "FName"/></p>        <p>last Name: <input type= "text" name= "lname"/></p>        < Input type= "Submit" value= "Submit"/>    </form>function  submitaction () {        alert ();         return false ;    } $ ("#subForm"). On ("Submit", submitaction);

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.

JavaScript return False

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.