Preventing a Link or Button from moving to the next URL (or triggering a post or postback in the case of ASP.Net) is easily accomplished using the very common method of calling event.preventDefault() in the event handler for the element’s click event. However, it may be tempting to simply use onclick="return false;".

event.preventDefault() is the preferred method to stop a link’s default behaviour, but of course it requires that you write the JS/jQuery to bind to the event to call preventDefault() on the event.

Using onclick=”return false;” (or OnClientClick in the case of ASP.Net) seems appealing as it will prevent the link or button’s default action without having to bind to the element’s click event in JavaScript. However, onclick=”return false;” also prevents the element’s click event from firing at all, which is an issue if you plan on binding to the click event. One exception to this is the case where you bind to the document’s click event with a selector, which will still work as you’re not relying on the button’s click event.

A pretty simple guideline: If the link/button should not change the URL or cause a post, and you don’t plan on binding to the element’s click event, then you can simply use onclick=”return false;” (or OnClientClick in the case of ASP.Net) to save a few lines of JavaScript. An example that comes
to mind is a button where the only purpose is to do something client-side like open a Bootstrap Popover, where the Popover will open without the onclick event having to fire.

Example of impact of using onclick="return false;":

HTML/ASPX:

<a href="#" id="myBtn" onclick="return false;">Click Me</a>
<!-- OR for ASP.Net LinkButton: -->
<asp:LinkButton ID="myBtn" runat="server" OnClientClick="return false;" ClientIDMode="Static">Click Me</asp:LinkButton>
<!-- OR for ASP.Net Button: -->
<asp:Button ID="myBtn" runat="server" OnClientClick="return false;" ClientIDMode="Static">Click Me</asp:Button>

JS:

<script>
  $(function() {
    $('#myBtn').on('click', function { 
      // This will NOT execute because the button click event is never fired
    });
    $(document).on('click', '#myBtn', function {
      // This WILL execute because the document click event still fires
    });
  });
</script>