Why is javascript: void (0) considered harmful?

Can someone tell me or point me to some resource that explains why using javascript:void(0) in hyperlinks is harmful (especially in Internet Explorer 6)?

+6
javascript
source share
2 answers

Using the javascript: keyword javascript: in the link is not recommended. I managed to find only one article on why this could be harmful:

a href = "javascript: void (0);" - avoid emptiness

But the general consensus shows that you should not use it because it can confuse browsers without javascript support, as some of these browsers can be parsed as an invalid link.

Instead, you must provide a link to a page working on the functionality that javascript will provide, or display a message about the site for which javascript works correctly. Follow the same link return false; from your event, for example:

 <a href="noscript.html" onclick="doSomething(); return false;">I'm a link</a> 

Or, conversely, use return false; or preventDefault() and returnValue in your javascript code:

 element.onclick = function () { /* // return false is better for most situations (see bobince comment) if (event.preventDefault) event.preventDefault(); else event.returnValue = false; */ doSomething(); return false; } 
+4
source share

Click <a href="javascript:void(0)" /> fires the "beforeunload" event in the window object in IE (I tested in IE10), but click <a href="#" /> no.

+1
source share

All Articles