Question:
What does the value javascript:void(0)
in the fields href
tags a
in HTML
?
Example:
<a href="javascript:void(0)" id="btnClick">Click</a>
Answer:
The void
operator evaluates the given expression and returns undefined
The reason for using this expression in a link
href
is because this attribute causes a redirect to a plain text version of what is obtained from the function. But if the result is undefined
the redirection does not occur. This is the shortest way to not redirect and do nothing on a link
.
Differences with other methods to not redirect:
-
href=""
Reload the current page -
href="#"
Scrolls to the top of the page -
href="javascript: void(0)"
Does nothing -
href="javascript:;"
It does nothing, but it does not work in all browsers, for example, in IE7 it redirects to a new window And also, it is not as accepted as a de facto standard than the previous one
More info:
- You can find more info in this post: https://stackoverflow.com/questions/1291942/what-does-javascriptvoid0-mean
- You can find documentation at this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
- Differences: https://stackoverflow.com/questions/6968911/what-is-the-difference-between-href-href-and-href-javascriptvoid0
- Why not use
href="javascript:;"
? https://stackoverflow.com/questions/5237105/why-use-javascriptvoid0-instead-of-javascript-as-an-href-do-nothing-plac