0

A-void(0) javascript:void(0)

Posted September 14th, 2009 in javascript and tagged , , by Michael

Today seems to be the day of IE6 bugs… the rather old “comma-bug” caused some troubles in one app and another one did not work properly in IE6. The following code was involved:

<a href=”javascript:void(0)” onclick=”someFunc()”>my link</a>

The someFunc() dynamically loaded a new script file – which worked fine in all browsers except for Internet Explorer 6 – the file was never loaded. Reason for this is that javascript:void(0) terminates all loading, including newly added &ltscript> tags or XHR requests.

Solving this is quite simple – the default handler of the link must never be executed:

<a href=”javascript:void(0)” onclick=”someFunc(); return false;“>my link</a>

But because there are more issues around the javascript: pseudo protocol I highly recommend not using javascript:void(0) at all. Instead use the following code:

<a href=”#” onclick=”someFunc(); return false;“>my link</a>

In the worst case the default handler is executed, which means the site goes to an empty anchor… in practice this means the “#” is appended to the URL, that’s it. No scary JavaScript function involved, that only breaks IE6. And shorter to type too.

More information: here and here.

Leave a Reply