Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

I have noticed one of my friend using 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 have feel no changed 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 any one explain what is the difference in both?

Just curious to know why he always use return.

Thanks

like image 829
deerox Avatar asked Jan 16 '23 06:01

deerox


1 Answers

In the first example a value will be returned from the onclick handler. This could be used to return false which would 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 run, the event will propagate and the default behavior will execute 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>
like image 155
Kevin Boucher Avatar answered Jan 25 '23 23:01

Kevin Boucher