HREF link that targets nothing, does not want to use hash or void(0)

Posted by Mattis on Stack Overflow See other posts from Stack Overflow or by Mattis
Published on 2011-06-29T00:16:23Z Indexed on 2011/06/29 8:22 UTC
Read the original article Hit count: 221

Filed under:
|
|

I have a link that I want to be able to click to trigger a piece of jQuery code.

Currently I have

<a href="#" id="foo">Link</a>

and

$('#foo').click(function(){
  // Do stuff
});

which works well. But, I have always hated using hash in this way. The page flickers and the hash is added to the page url.

One alternative is to use

<a href="javascript:void(0);" id="foo">Link</a>

but I also dislike seeing that piece of code in the browser status bar. It looks tacky.


What I'd rather have is an explanatory javascript placeholder that does nothing, like

<a href="javascript:zoom();" id="foo">Link</a>

which actually works, but throws an ReferenceError in the javascript console since there are no such function. What's the minimum definition of a function that does nothing?

Are there any other alternatives?

Should I just skip the link and use something like

<span id="foo" style="cursor:pointer;cursor:hand;">Link</span>

instead?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery