javascriptjquery

"javascript:void(0);" vs "return false" vs "preventDefault()"


When I want some link to not do anything but only respond to JavaScript actions what is the best way to avoid the link scrolling to the top edge of the page?

I know several ways of doing it, they all seem to work fine:

<a href="javascript:void(0)">Hello</a>

or:

<a id="hello" href="#">Hello</a>
<script type="text/javascript>
  $(document).ready(function() {
    $("#toto").click(function(){
      //...
      return false;
    });
  });
</script>

and even:

<a id="hello" href="#">Hello</a>
<script type="text/javascript>
  $(document).ready(function() {
    $("#toto").click(function(event){
      event.preventDefault();          
      //...
    });
  });
</script>

What is the difference between the above 3 examples?

PS: The above examples assume you are using jQuery but there are equivalents for mootools or prototype.


Solution

  • Binding:

    Responses:

    However: