Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the difference between returning or not? [duplicate]

Tags:

javascript

Possible Duplicates:
What's the effect of adding 'return false' to an onclick event?
When and why to 'return false' in javascript?

I mean, if I have this function :

 function openMask() {
     alert("enter");
 }

is it better call it as :

 <a href="javascript:void(0);" onclick="openMask()">Link</a>

or :

 <a href="javascript:void(0);" onclick="openMask(); return false;">Link</a>

??? I'm really curious about it! Never understood the difference...

like image 500
markzzz Avatar asked Apr 27 '26 11:04

markzzz


1 Answers

In a DOM event handler (such as onclick), returning true means "continue with the event", and returning false means "don't".

In this case, it's not entirely meaningful because your href is already javascript:void(0) (which won't do anything).

Note, though, that javascript: URIs should really be avoided, and that this isn't very helpful for non-Javascript users, so you should provide a fallback page, and then the return false becomes more meaningful:

JS:

function openMask() {
   alert("enter");
   return false; 
}

HTML:

<a href="fallbackpage.html" onclick="return openMask();">Link</a>
like image 179
Lightness Races in Orbit Avatar answered Apr 28 '26 23:04

Lightness Races in Orbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!