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="#">

3
  • 7
    # jumps to the top of the page. Commented Dec 19, 2013 at 8:39
  • 1
    #links in Modern browsers like Firefox and chrome. This causes the page to look for the available link (and jump to top if unavailable) in the page and scroll to the region. But Void does not do any action Commented Dec 19, 2013 at 8:40
  • Another question related to this: How about not include href attribute in <a>? For example <a href="javascript:void(0);" onclick="doSomething();"> vs <a onclick="doSomething();" Commented Jun 20, 2018 at 18:23

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Adding to that: void 0 is essentially undefined - that's why browser will do nothing.
probably something like href="#_" should also avoid the need for onclick="return false;", which IMHO is obtrusive!
4

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

4 Comments

modifies the url is fine but jump is not. and here if it's just "#" then the jump is to the top of the page
huh? I'm confused, there's a bit of discrepancy in your comment. You say that jump is not fine and then that the jump is to the top of the page. I don't get it
which is why we use javascript:void(0) that does not make the jump
Mate, you lost the plot...looks like you are on a different conversation
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.