17

I have seen href="javascript:void(0)" and I have seen href="javascript:;" Is there any reason I would not just use href="javascript:" instead?

Edit: Let me make it clear: I am combining this with an onclick and have no objection to using return false if it becomes necessary to use the alternative. Of course this is only if the alternative makes better sense over javascript:.

Also, I have yet to see a answer to my question shown (clearly I think) in the first paragraph. Thanks, david. :)

I have seen href="javascript:void(0)" and I have seen href="javascript:;" Is there any reason I would not just use href="javascript:" instead?

700 Software
  • 85,281
  • 83
  • 234
  • 341
  • How often do you see URLs with just `http:`? I suspect it's because standards mandate that when a protocol or href type is announced, you need to put something after it! :-) – Platinum Azure Mar 08 '11 at 19:13
  • 3
    http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0 - about the same topic, may be helpfull – Bakudan Mar 08 '11 at 19:20
  • See also: http://stackoverflow.com/questions/3666683/href-javascript-vs-href-javascriptvoid0 – Pang May 30 '13 at 02:11

4 Answers4

18

Personally I'd avoid using either. I'd have onclick='return false;' instead, and just set href='#'.

Why? Because if you put Javascript code in the href, then it appears in the browser's status bar when you hover over the link, just like any other URL. It just looks messy.

In fact, since it's going to be ignored anyway, you could put any URL you fancy into the href, not just a hash. The hash is safest, just in case your user has Javascript disabled, but aside from that point, it could be anything.

But I have to point out: having an <a> tag with the href going to a void Javascript code seems to rather defeat the point of having an <a> tag in the first place. I can see how you'd want to enable and disable a link at runtime, but simply setting the href to void does seem somewhat pointless. If it isn't going to be a real link, why not just have a <span> or some other tag. If you must make it look like a link, it's trivial to do that in CSS.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 8
    The hash symbol is meant to be used as an anchor rather than for this purpose. You will get undesirable effects from time to time when you use it this way such as your page jumping all over the place. – RubyFanatic Mar 08 '11 at 19:24
  • 1
    @RubyFanatic - to be pedantic about it, the hash symbol is a *link to an anchor*, but I accept your point. I don't agree though; as long as Javacript is enabled, you won't get undesirable effects, and even if you do, jumping to the top of the current page is rarely a disaster. Besides, the hash symbol has been completely usurped for use with Ajax functionality now, to the point that it's actually recommended as good practice, so doing this will hardly be going against the grain. – Spudley Mar 08 '11 at 19:36
  • Just to answer your question: I don't use span because then it does not work with the keyboard and does not automatically get blue/underline/pointer on the CSS. – 700 Software Mar 08 '11 at 21:48
  • 1
    well actually. If you have a `tabindex="0"` on your span, then it would react to :focus events. i've tried this example in IE 7, 8 and other newer browsers: http://jsfiddle.net/vptB3/1/ (IE doesn't support :focus anyway) – Tokimon Jan 31 '13 at 16:54
16

Doesn't answer you question but may shed a bit more light on it, some early versions of browsers like netscape had problems when a script was used within the href.

The void operator was pretty much to only way force the click to do nothing.

Now, with browsers properly implementing "pseudo URLs", you can safely just use javascript:;.

700 Software
  • 85,281
  • 83
  • 234
  • 341
david
  • 4,218
  • 3
  • 23
  • 25
2

The ideal situation is to not put any code on the element itself, but rather use javascript in either a script tag, or in an external file and bind the event handler there.

Jason Miesionczek
  • 14,268
  • 17
  • 76
  • 108
  • Yes. Javascript event binding on the hyperlink should produce the most readable code and easy to maintain for the future. – RubyFanatic Mar 08 '11 at 19:31
1

It's better not to use either. The href attribute indicates a url. It's better to use event handlers to get the desired behaviour, which is probably the onclick event.

In this case the desired behaviour apparently is to do nothing. Firstly, why use an anchor tag at all when it doesn't link anywhere? Secondly, this can be handles by preventing event bubbling, by letting the onclick event handler return false. Then the href can either point to #, or better even to have it point to a url that more or less has the same effect as the event handler, so it will degrade gracefully when javascript is turned off.

Rian Schmits
  • 3,096
  • 3
  • 28
  • 44
  • If JavaScript is turned off there will be a big red `noscript` warning on top. Also, I do not see how it is better for the status bar to have a URL instead of the work javascript. – 700 Software Mar 10 '11 at 16:28
  • 1
    Correction: "word" JavaScript – 700 Software Aug 09 '11 at 02:02
  • 4
    @GeorgeBailey I'm impressed you come back 5 months later to correct a typo. +1 – n0nag0n Jun 14 '12 at 15:44
  • How about when you need to **apply the anchor styles to something** that "does not link anywhere" but executes a Javascript script that you don't want to expose to the browser's status bar? – andreszs May 23 '22 at 12:40