0

why do developers use href="javascript:;"

e.g.:Content of the A Tag:

<a class="anchor" title="title" href="javascript:;" id="someId">...</a>
Kerem
  • 11,377
  • 5
  • 59
  • 58
ciochPep
  • 2,472
  • 4
  • 26
  • 30
  • this won't send you to a url instead will run the javascript on click – polin Feb 10 '13 at 10:04
  • what javascript code would run? what would happen if the href was empty? it would not run javascript? – ciochPep Feb 10 '13 at 10:05
  • on that case it will do nothing – polin Feb 10 '13 at 10:08
  • Check out the discussion here at: http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0 Indirectly answers your question... loads of good information! – Forhad Ahmed Feb 10 '13 at 10:06

3 Answers3

1

The javascript: pseudo-protocol tells the browser that what follows is JavaScript code that should be executed when the link is clicked. What follows in your example is a no-op, and so clicking the link does nothing. Typically this is used when the link isn't being used as a link but rather as a button, where the actual behavior the coder wants is specified by a click handler.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I see. is it for the browser or for other developers?what does the browser do with this info? – ciochPep Feb 10 '13 at 10:07
  • 1
    This answer is correct, but needs a bit more info. The use of 'javascript:' in the `href` PREDATES the existence of the DOM0 handlers, such as `onclick`, etc. Once `onclick` existed, the `javascript:` should have no longer been used. – Jeremy J Starcher Feb 10 '13 at 10:07
  • @ciochPep: As I said, the browser will interpret what follows the `javascript:` as JavaScript code and execute it. In this case, that code does nothing. So you end up with something that looks and acts like a link but isn't a link. – T.J. Crowder Feb 10 '13 at 10:08
  • *clicking the link does nothing* AFAIk, it spawns a new javascript interpreter. I'd say it's bad practice. – KooiInc Feb 10 '13 at 10:40
  • @KooiInc: Firing up a JavaScript interpreter is not a significant operation in modern browsers. I agree that this is bad practice for any number of other reasons, though. :-) – T.J. Crowder Feb 10 '13 at 11:07
0

from my experience using, if people use <a href="www.google.com"> I can right click and open it on a new tab. But if it is href=javascript:openLink() I can't open it on a new tab

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
cix.yong
  • 35
  • 3
  • 9
0

Good question. Its so that their link doesn't do anything. The developer is probably adding in the javascript function dynamically using Jquery's click event or something similar.

It seems to be a legacy bad habit that people have. They probably want the style of an href but then don't want the functionality of it. You can easily do this without using an href. In a way, they are saying "Here is the value for a link" and then immediately going "ignore the value for my link".

There is a good discussion here:Unobtrusive Javascript

According the the w3schools the value for an href should be a url or a script. When someone does href="javascript:;"

They are using a script that does nothing. They just do it so they can using the styling/tabbing the href offers... which, in my opinion, should be done another way like using css classes.

Also according to the w3schools the tag cannot use certain other attributes if the href is not present, but in HTML5 the tag should only be used for hyperlinks.

Community
  • 1
  • 1
TigerBear
  • 2,479
  • 1
  • 21
  • 24