What does the value mean in the tag javascript:void(0)
fields in ?href
a
HTML
Example:
<a href="javascript:void(0)" id="btnClick">Click</a>
What does the value mean in the tag javascript:void(0)
fields in ?href
a
HTML
Example:
<a href="javascript:void(0)" id="btnClick">Click</a>
The operator
void
evaluates the given expression and returnsundefined
The reason for using this expression in a
href
of alink
is because this attribute causes a redirection to a plain text version of the return of the function. But if the result isundefined
the redirect does not occur. This is the shortest way to not redirect and do nothing in alink
.Differences with other methods for not redirecting:
href=""
Reload the current pagehref="#"
Scroll to the top of the pagehref="javascript: void(0)"
Does nothinghref="javascript:;"
It doesn't do anything, but it doesn't work in all browsers, for example, in IE7 it redirects to a new window AND it's not as accepted as a de facto standard than the previous oneMore info:
href="javascript:;"
? https://stackoverflow.com/questions/5237105/why-use-javascriptvoid0-instead-of-javascript-as-an-href-do-nothing-placThe operator
void
, like other programming languages, defines a function that does not return a value.In the case of Javascript:
void(expresion)
, specifies an expression that is evaluated but will not return a value.As an example a link that does not perform any action since it returns
undefined
since it evaluates the value of 0:Variables in Javascript are initialized as
undefined
, you can't use itvoid
to define a data type and it's not really necessary,example: