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

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

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

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

    WHAT "return false" IS REALLY DOING?

    return false is actually doing three very separate things when you call it:

    1. event.preventDefault();
    2. event.stopPropagation();
    3. Stops callback execution and returns immediately when called.

    See jquery-events-stop-misusing-return-false for more information.

    For example :

    while clicking this link, return false will cancel the default behaviour of the browser.

    <a href='#' onclick='someFunc(3.1415926); return false;'>Click here !</a>
    
    0 讨论(0)
  • 2020-11-21 23:14

    Retuning false from a JavaScript event usually cancels the "default" behavior - in the case of links, it tells the browser to not follow the link.

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

    using return false in an onclick event stops the browser from processing the rest of the execution stack, which includes following the link in the href attribute.

    In other words, adding return false stops the href from working. In your example, this is exactly what you want.

    In buttons, it's not necessary because onclick is all it will ever execute -- there is no href to process and go to.

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

    The return false prevents the page from being navigated and unwanted scrolling of a window to the top or bottom.

    onclick="return false"
    
    0 讨论(0)
  • 2020-11-21 23:16

    Browser hack: http://jszen.blogspot.com/2007/03/return-false-to-prevent-jumping.html

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

    Return false will prevent navigation. Otherwise, the location would become the return value of someFunc

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