I've seen such
hrefs many times, but I don't know what exactly that means.
Verified Answer (1016 Votes) ✓
voidoperator evaluates the given expression and then returns
voidoperator is often used merely to obtain the
undefinedprimitive value, usually using “
void(0)” (which is equivalent to “
void 0”). In these cases, the global variable
undefinedcan be used instead (assuming it has not been assigned to a non-default value).
An explanation is provided here:
The reason you’d want to do this with the
href of a link is that normally, a
undefined, then the browser stays on the same page.
void(0) is just a short and simple script that evaluates to
Answer #2 (427 Votes)
In addition to the technical answer,
There is no good reason to use a
<a href="#"> is a common alternative which might arguably be less bad. However you must remember to
return false from your
onclick event handler to prevent the link being followed and scrolling up to the top of the page.
In some cases there may be an actual useful place to point the link to. For example if you have a control you can click on that opens up a previously-hidden
<div id="foo">, it makes some sense to use
Otherwise, if a link points only to some script, it is not really a link and should not be marked up as such. The usual approach would be to add the
onclick to a
<div>, or an
<a> without an
href and style it in some way to make it clear you can click on it. This is what StackOverflow [did at the time of writing; now it uses
The disadvantage of this is that you lose keyboard control, since you can't tab onto a span/div/bare-a or activate it with space. Whether this is actually a disadvantage depends on what sort of action the element is intended to take. You can, with some effort, attempt to mimic the keyboard interactability by adding a
tabIndex to the element, and listening for a Space keypress. But it's never going to 100% reproduce the real browser behaviour, not least because different browsers can respond to the keyboard differently (not to mention non-visual browsers).
If you really want an element that isn't a link but which can be activated as normal by mouse or keyboard, what you want is a
<button type="button"> (or
<input type="button"> is just as good, for simple textual contents). You can always use CSS to restyle it so it looks more like a link than a button, if you want. But since it behaves like a button, that's how really you should mark it up.
(*: in site authoring, anyway. Obviously they are useful for bookmarklets.
Answer #3 (122 Votes)
It means it’ll do nothing. It’s an attempt to have the link not ‘navigate’ anywhere. But it’s not the right way.
You should actually just
return false in the
onclick event, like so:
<a href="#" onclick="return false;">hello</a>
<a href="backup_page_displaying_image.aspx" onclick="return coolImageDisplayFunction();">hello</a>