Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript:; vs javascript:void(0);

Tags:

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

like image 919
Ahmed Magdy Avatar asked Jun 26 '10 22:06

Ahmed Magdy


People also ask

What can I use instead of JavaScript void 0?

Another alternative to JavaScript void 0 is to use return false. When the click returns false, the browser will not take any action.

What does JavaScript void 0 ); mean?

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.

Is JavaScript void 0 Safe?

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).

Which href value should I use for JavaScript links or JavaScript void 0?

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.


1 Answers

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.

like image 145
Quentin Avatar answered Sep 29 '22 21:09

Quentin