25

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,039
  • 14
  • 51
  • 105

3 Answers3

45
<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,808
  • 3
  • 31
  • 51
  • 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,284
  • 2
  • 36
  • 54
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,542
  • 11
  • 19
Royi Namir
  • 138,711
  • 129
  • 435
  • 755