6

Are there any advantages/disadvantages of

<a href="javascript:;" onclick="myFunc()">do myFunc</a>

over

<a href="javascript:" onclick="myFunc()">do myFunc</a>
AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • isn't the `;` technically correct syntax? – Eonasdan Mar 21 '13 at 18:51
  • I feel dumb for not trying this in jsfiddle :( I usually use `javascript:void(0);` – Eonasdan Mar 21 '13 at 18:58
  • At a more meta level, there is a vast disadvantage (in terms of maintainability) to using inline javascript like ``onClick`` for events. While it requires more "work" upfront, using ``addEventListener`` would be a much better option for future sanity [MDN](https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener). – Nick Tomlin Mar 21 '13 at 19:08
  • I would use neither and create a button instead. Links should not be used for custom user interaction unless they also link to an actual page. – Felix Kling Mar 21 '13 at 19:32

1 Answers1

1
 <a href="javascript:;" onclick="myFunc()">do myFunc</a>- 

      Will kill script execution


  <a href="javascript:" onclick="myFunc()">do myFunc</a>
       Execute Script

For example: http://jsfiddle.net/JZje5/

Amit
  • 15,217
  • 8
  • 46
  • 68
  • Just adding this link could be relevant to this discussion http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0 – ncubica Mar 21 '13 at 19:09
  • 2
    What do you mean? In your fiddle both seem to work the same way for me (after adding the missing "1" to the second function). – AndreKR Mar 22 '13 at 00:19
  • 2
    This answer is blatantly wrong and should be removed. – Adrien Brunelat Dec 12 '16 at 14:36