I would like to know what is the difference between javascript:;
and javascript:void(0);
if I use them in href
attribure for a
anchor (link)
<a href="javascript:;" onclick="DoSomething();">Link</a> <a href="javascript:void(0);" onclick="DoSomething();">Link</a>
I see them acting the same on all browsers but what is the technical difference?
Regards, Magdy
Another alternative to JavaScript void 0 is to use return false. When the click returns false, the browser will not take any action.
javascript: void(0); is used as a placeholder URL to indicate that an onclick event is tied to the link to do the actual action.
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 href= “” will only load the current page, while href= “#” scrolls the current page to the top, while href= 'javascript:void(0)' will do nothing at all. The same effects of javascript:void(0) are realized by returning false from the click event handler of the <a> tag with either of the two methods.
One runs JavaScript that is just an empty statement, the other runs JavaScript that evaluates the statement 0
and then returns undefined
.
Neither should be used.
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