Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Return false;" in onclick event for JavaScript mean?

Tags:

javascript

What does "Return false;" in onclick event for JavaScript mean?

    <input 
      type="button" 
      runat="server" 
      id="btnCancel" 
      value=" Cancel " 
      style="width:70px;"
      onclick="document.location.href = 'ReportPanel.aspx'; return false;" 
    />

in the onclick event. It has return false; What does it mean? What does it mean if return true;?

like image 862
Ybbest Avatar asked Dec 30 '09 21:12

Ybbest


People also ask

What does return false do in an onClick event?

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

What happens when a JavaScript function returns false?

Any code after return statement in a function will never be executed. It stops executing of function and make this function return value passed ( false in this case). Your function is "submit" event callback. If this callback returns false, form will not be submitted actually. Otherwise, it will be submitted as it would do without JavaScript.

What does return false mean in a pop-up?

Those pop-ups have two choices yes or no. If there is a return false statement then there in an event listener for that alert message, it may correspond to not executing the default set of instructions . The return value of the event handler is used to determine if the default (desired) browser behavior should take place or not.

What is onClick event in JavaScript?

The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element. It supports all HTML elements ...


1 Answers

It says "the event never happened" to the browser. If you had a submit button instead of a simple button and didn't have "return false", the form would get submitted when you click it (after executing the javascript).

like image 59
naivists Avatar answered Sep 21 '22 22:09

naivists