27

I can see a lot of <a href="javascript:void(0);"> on html pages. From I've read it does nothing by returning undefined. How is this different with <href="#">

abiieez
  • 3,139
  • 14
  • 57
  • 110

3 Answers3

47
<a href="#">link</a>

adds # to the browser url and jumps to the top of the page.

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

simply "ignores" the link click.

<a href="#" onclick="return false;">link</a>

also ignores the href.

Don't forget that in some cases javascript might be disabled (very uncommon).

MaGnetas
  • 4,918
  • 4
  • 32
  • 52
  • Adding to that: `void 0` is essentially `undefined` - that's why browser will do nothing. – kamituel Dec 19 '13 at 08:42
  • probably something like `href="#_"` should also avoid the need for `onclick="return false;"`, which IMHO is obtrusive! – Fr0zenFyr Jul 26 '17 at 07:39
4

# might jump to a different location in the page. Plus, it modifies the URL

Leo
  • 14,625
  • 2
  • 37
  • 55
4

href="javascript:void(0); is for the case that you want it to do nothing , but still look as a link. ( blue and underline).

it's just like :javascript:return undefined;

Why ?

Because someone might do : undefined=function (){}

The # - is an anchor which sends you to the top of the page.

Akshay Khandelwal
  • 1,570
  • 11
  • 19
Royi Namir
  • 144,742
  • 138
  • 468
  • 792