7
  <a href="javascript:void(0)" id="loginlink">login</a>

I've seen such anchor tag's href property having javascript:void(0) value many times. I don't know what exactly that means.I saw this when working with angular 5. Why is javascript:void(0) used as a value for href property?

lwairore
  • 624
  • 1
  • 7
  • 11
Guvaliour
  • 397
  • 1
  • 5
  • 17

1 Answers1

13
javascript:void(0)

The snippet show above simply ignores the link "click." This can be done in a similar fashion by the following:

<a href="#" onclick="return false;">link</a>
Rob
  • 177
  • 1
  • 5
  • 1
    This answer doesn't really define why people are using `javascript:void(0)`. The simple answer is that javascript via a framework such as Angular, React, or Vue usually have click handlers that don't properly address the browser wanting an `href`. The browser won't treat the link as a clickable link unless it has the `href`. Therefore, people started putting `href=""`, and substituting it rather than changing CSS etc. The reason people DON'T want to use a `#` is because it's treated AS A ROUTING MECHANISM in these, not just a 'scroll here!' on the same page link... – Kody Jun 08 '22 at 23:05
  • Alternatively use `onclick="event.preventDefault()"` – Pascal R. Sep 29 '22 at 09:39