What is the difference in using return before any ex function: "return Buy ()" or "Buy ()"

I noticed that one of my friends used this.

<a href="#" onclick="return Buy();">» <b class="font_bigger"><span id="buy_title">Buy</span> for <span class="points_in" id="buy_value">$1,691</span></b></a> 

And I will not change if I use this.

 <a href="#" onclick="Buy();">» <b class="font_bigger"><span id="buy_title">Buy</span> for <span class="points_in" id="buy_value">$1,691</span></b></a> 

So can anyone explain what the difference is in both?

Just curious to find out why he always uses return.

thanks

+4
source share
2 answers

In the first example, the value will be returned from the onclick handler. This can be used to return false , which will prevent the default behavior.

 <a href="#" onclick="return Buy();">» <b class="font_bigger"><span id="buy_title">Buy</span> for <span class="points_in" id="buy_value">$1,691</span></b></a> 

In the second example, the event handler will simply fire, the event will propagate, and the default behavior will be executed regardless of any value returned by the event handler.

 <a href="#" onclick="Buy();">» <b class="font_bigger"><span id="buy_title">Buy</span> for <span class="points_in" id="buy_value">$1,691</span></b></a> 
+8
source

There are two actions here.

  • Default Behavior: HREF Execution
  • OnClick event execution

Events are executed first by the browser. This behavior is called Early event handling . If the event action returns false , it prevents the default action ie HREF .

+1
source

Source: https://habr.com/ru/post/1440914/


All Articles