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="#">
JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.
Generally, you want to avoid href="javascript:void(0)" , as it will cause the browser to parse the value of the link URL, which is both costly and unnecessary. It also introduces a potential XSS security vulnerability, as javascript: URLs violate Content Security Policy (CSP).
The main reason why users encounter the said error message is that Javascript is disabled on their browsers. Javascript is a plugin which is mostly disabled by default on all browsers. Here, you have to manually enable the Javascript so the site can render its page using the technology.
Disable add-ons/extensions If you've suddenly started getting the javascript void 0 error after installing a new extension/add-on, disable it and try again. What is this? Try disabling all extensions/add-ons and then access the website.
<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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With