login
I\'ve seen such href
s many times, but I don\'t know what exactly
void
is an operator that is used to return a undefined
value so the browser will not be able to load a new page.
Web browsers will try and take whatever is used as a URL and load it unless it is a JavaScript function that returns null. For example, if we click a link like this:
<a href="javascript: alert('Hello World')">Click Me</a>
then an alert message will show up without loading a new page, and that is because alert
is a function that returns a null value. This means that when the browser attempts to load a new page it sees null and has nothing to load.
An important thing to note about the void operator is that it requires a value and cannot be used by itself. We should use it like this:
<a href="javascript: void(0)">I am a useless link</a>
Web Developers use javascript:void(0)
because it is the easiest way to prevent the default behavior of a
tag. void(*anything*)
returns undefined
and it is a falsy value. and returning a falsy value is like return false
in onclick
event of a
tag that prevents its default behavior.
So I think javascript:void(0)
is the simplest way to prevent the default behavior of a
tag.
The
void
operator evaluates the given expression and then returnsundefined
.The
void
operator is often used merely to obtain theundefined
primitive value, usually using “void(0)
” (which is equivalent to “void 0
”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).
An explanation is provided here: void operator.
The reason you’d want to do this with the href
of a link is that normally, a javascript:
URL will redirect the browser to a plain text version of the result of evaluating that JavaScript. But if the result is undefined
, then the browser stays on the same page. void(0)
is just a short and simple script that evaluates to undefined
.
You should always have an href on your a tags. Calling a JavaScript function that returns 'undefined' will do just fine. So will linking to '#'.
Anchor tags in Internet Explorer 6 without an href do not get the a:hover
style applied.
Yes, it is terrible and a minor crime against humanity, but then again so is Internet Explorer 6 in general.
I hope this helps.
Internet Explorer 6 is actually a major crime against humanity.
It's worth mentioning that you'll sometimes see void 0
when checking for undefined, simply because it requires fewer characters.
For example:
if (something === undefined) {
doSomething();
}
Compared to:
if (something === void 0) {
doSomething();
}
Some minification methods replace undefined
with void 0
for this reason.
Usage of javascript:void(0)
means that the author of the HTML is misusing the anchor element in place of the button element.
Anchor tags are often abused with the onclick event to create pseudo-buttons by setting href to "#" or "javascript:void(0)" to prevent the page from refreshing. These values cause unexpected behavior when copying/dragging links, opening links in a new tabs/windows, bookmarking, and when JavaScript is still downloading, errors out, or is disabled. This also conveys incorrect semantics to assistive technologies (e.g., screen readers). In these cases, it is recommended to use a
<button>
instead. In general you should only use an anchor for navigation using a proper URL.
Source: MDN's <a> Page.