What's the effect of adding 'return false' to a click event listener?

前端 未结 16 1750
南旧
南旧 2020-11-21 22:59

Many times I\'ve seen links like these in HTML pages:

Click here !
相关标签:
16条回答
  • 2020-11-21 23:32

    By default, when you click on the button, the form would be sent to server no matter what value you have input.

    However, this behavior is not quite appropriate for most cases because we may want to do some checking before sending it to server.

    So, when the listener received "false", the submitting would be cancelled. Basically, it is for the purpose to do some checking on front end.

    0 讨论(0)
  • 2020-11-21 23:33

    The return value of an event handler determines whether or not the default browser behaviour should take place as well. In the case of clicking on links, this would be following the link, but the difference is most noticeable in form submit handlers, where you can cancel a form submission if the user has made a mistake entering the information.

    I don't believe there is a W3C specification for this. All the ancient JavaScript interfaces like this have been given the nickname "DOM 0", and are mostly unspecified. You may have some luck reading old Netscape 2 documentation.

    The modern way of achieving this effect is to call event.preventDefault(), and this is specified in the DOM 2 Events specification.

    0 讨论(0)
  • 2020-11-21 23:34

    I have this link on my HTML-page:

    <a href = "" 
    onclick = "setBodyHtml ('new content'); return false; "
    > click here </a>
    

    The function setBodyHtml() is defined as:

    function setBodyHtml (s)
    { document.body.innerHTML = s;
    }
    

    When I click the link the link disappears and the text shown in the browser changes to "new content".

    But if I remove the "false" from my link, clicking the link does (seemingly) nothing. Why is that?

    It is because if I don't return false the default behavior of clicking the link and displaying its target-page happens, is not canceled. BUT, here the href of the hyperlink is "" so it links back to the SAME current page. So the page is effectively just refreshed and seemingly nothing happens.

    In the background the function setBodyHtml() still does get executed. It assigns its argument to body.innerHTML. But because the page is immediately refreshed/reloaded the modified body-content does not stay visible for more than a few milliseconds perhaps, so I will not see it.

    This example shows why it is sometimes USEFUL to use "return false".

    I do want to assign SOME href to the link, so that it shows as a link, as underlined text. But I don't want the click to the link to effectively just reload the page. I want that default navigation=behavior to be canceled and whatever side-effects are caused by calling my function to take and stay in effect. Therefore I must "return false".

    The example above is something you would quickly try out during development. For production you would more likely assign a click-handler in JavaScript and call preventDefault() instead. But for a quick try-it-out the "return false" above does the trick.

    0 讨论(0)
  • 2020-11-21 23:36

    I am surprised that no one mentioned onmousedown instead of onclick. The

    onclick='return false'

    does not catch the browser's default behaviour resulting in (sometimes unwanted) text selection occurring for mousedown but

    onmousedown='return false'

    does.

    In other words, when I click on a button, its text sometimes becomes accidentally selected changing the look of the button, that may be unwanted. That is the default behaviour that we are trying to prevent here. However, the mousedown event is registered before click, so if you only prevent that behaviour inside your click handler, it will not affect the unwanted selection arising from the mousedown event. So the text still gets selected. However, preventing default for the mousedown event will do the job.

    See also event.preventDefault() vs. return false

    0 讨论(0)
提交回复
热议问题