<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?
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).
If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “javaScript:void(0)”, returns the undefined primitive value. The void operator is used to evaluate the given expression. After that, it returns undefined.
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>
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