7

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

rae1
  • 6,066
  • 4
  • 27
  • 48
Abhi
  • 2,298
  • 4
  • 29
  • 34

2 Answers2

4

Use of the javascript: keyword in a link isn't recommended anyway. I've only managed to find one article on why it might be harmful:

a href=”javascript:void(0);” — avoid the void

But the general consensus shows that you shouldn't use it because it might confuse browsers without javascript support, for some of those browsers it could be parsed as an invalid link.

Instead, you should provide a link to a page either working around the functionality that would be provided by javascript or displaying a message about the site requiring javascript to work correctly. On the same link, return false; from your event, like so:

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

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

element.onclick = function ()
{
    /*
        // return false is better for most situations (see bobince's comment)
        if (event.preventDefault)
            event.preventDefault();
        else
            event.returnValue = false;
    */

    doSomething();

    return false;
}
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • Thanks for that Andy. I know the issues associated with clicks and have taken a look at the blog you mentioned. But, I have not found even a single valid explanation for why javascript:void(0) is considered harmful (assuming we do all the return false and such things). – Abhi Feb 15 '10 at 10:21
  • @Abhi: That blog post suggests images disappear when wrapped with a `void(0)` link in IE, can't say I've experienced it myself. Beyond this I'm not sure `void` is *really* harmful. – Andy E Feb 15 '10 at 10:25
  • 2
    Me neither, I don't think that problem, whatever it was, was related to `void`. However, never using `javascript:` links anyway is good advice. If you do use JavaScript-assigned event handlers in preference to `onclick="..."` attributes (and this is in general the best approach), using `return false` is by far the easiest response as it will work on IE and other browsers without needing the event object at all. The example above doesn't `cancelBubble` on IE, and doesn't work on other browsers due to the lack of `event` parameter (the `event` global is IE-only). – bobince Feb 15 '10 at 10:33
  • http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0/1691745#1691745 mentions a couple of things associated with javascript:. What does it mean when he says instantiates a new engine? So was thinking whether there is some post written about the ill effects of javascript:. – Abhi Feb 15 '10 at 11:09
  • @Abhi: I assume he means `javascript:` links start a javascript compiler, much like `eval()` does. However, I thought (er, think) the same was/is true of `onclick="doSomething()"`. See Eric Lippert's explanation on eval at http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx. – Andy E Feb 15 '10 at 14:35
  • @bobince: Good point, `return false;` is the best way to cancel the default action of an event in most situations. – Andy E Feb 15 '10 at 14:36
  • @AndyE your only article stating void is harmful is dead – Szymon Toda May 22 '15 at 12:12
1

Click on <a href="javascript:void(0)" /> triggers event "beforeunload" in object "window" в IE (I have tested in IE10), but click on <a href="#" /> doesn't.

dizel3d
  • 3,619
  • 1
  • 22
  • 35