Javascript :; vs javascript: void (0);

I would like to know what is the difference between javascript:; and javascript:void(0); if I use them in the href attribute to bind a anchor (link)

 <a href="javascript:;" onclick="DoSomething();">Link</a> <a href="javascript:void(0);" onclick="DoSomething();">Link</a> 

I see that they work the same in all browsers, but what is the technical difference?

Regards, Magdy

+18
javascript cross-browser href anchor
Jun 26 '10 at 22:09
source share
3 answers

One runs JavaScript, which has no operators, the other runs JavaScript, which evaluates to 0 , and then returns undefined .

There should be nothing.

+15
Jun 26 '10 at 22:17
source share

Only the latest javascript:void(0); the more readily accepted agreement is that it says this code does nothing.

It is worth noting that industry standards have come a long way with regard to this syntax. You should learn Progressive Enhancement.

+5
Jun 26 '10 at 10:18
source share

I agree with David that none of them should be used. The javascript pseudo-protocol can put a page in a wait state in some browsers, which can have unexpected consequences. As one example, I spent several hours debugging a web application that crashed IE6 whenever someone clicked a javascript link: shortly after the page loaded. It turned out that the page entering the standby state contradicts the Flash movie trying to initialize. I solved the problem by replacing the link with one in this format:

 <a href="#" onclick="DoSomething(); return false;">Link</a> 

"return false" prevents the link from actually executing.

+5
Jun 27 '10 at 3:11
source share



All Articles