+4  A: 

Why would you do this when you can use addEventListener/attachEvent? If there is no href-equivalent, don't use an <a>, use a <button> and style it accordingly.

eyelidlessness
using a button instead of a link isn't a feasible option in many cases.
nickf
Because it looks ugly. You can add an icon beside the link. They are hard to format the same across browsers. And generally, people are moving to links instead of buttons--look at most of stack over flow.
Darryl Hein
It looks ugly? It looks like whatever you want it to look like. In fact, I think buttons actually have more style flexibility than links, in that they're inline but can properly take padding across browsers. Anything that can be done with a link can be done with a button, with regards to CSS.
eyelidlessness
why button? Why not use a span. The css will be simpler (shorter) than with buttons.
Gene
Span can't be keyboard-focused.
bobince
And button is (gasp) semantically correct. Span is semantically meaningless. A button is *exactly* what the author intends to use, semantically.
eyelidlessness
+1 agreed ++++++++++
alex
+2  A: 

you forgot another method:

5: <a href="#" id="myLink">Link</a>

with the javascript:

document.getElementById('myLink').onclick = function() {
    // do stuff
};

I can't comment on which of the options has the best support or which is semantically the best, but I'll just say that I much prefer this style because it separates your content from your JS. It keeps all the Javascript together, which is much easier to maintain (especially if you are applying this to many links), and you can even put it in an external file which can then be packed to reduce filesize and cached by client browsers.

nickf
If you have 20 or 30 different links with JS this can get quite tedious and end up with a lot of JS.
Darryl Hein
So use more concise code. var $ = document.getElementById;
eyelidlessness
it's no more tedious than wading through HTML to change javascript. ...or you could use something like jQuery which can easily change the events on multiple elements at once... $('#menu a').click(function() { .. })
nickf
+2  A: 
<a href="#" onClick="DoSomething(); return false;">link</a>

I will do this, or:

<a href="#" id = "Link">link</a>
(document.getElementById("Link")).onclick = function() { DoSomething(); return false; };

Depending on the situation. For larger apps, the second one is best because then it consolidates your event code.

nlaq
If you have 20 or 30 different links with JS this can get quite tedious and end up with a lot of JS.
Darryl Hein
and bugs which creep into your code can be VERY hard to debug. These bugs appear very easily, as evidenced by your first example. :)
nickf
ouch. Thanks for the heads up :p
nlaq
+14  A: 

I quite enjoy Matt Kruse's Javascript Best Practices article. In it, he states that using the href section to execute javascript is a bad idea. Even though you have stated that your users must have javascript enabled, there's no reason you can't have a simple HTML page that all your javascript links can point to for their href section in the event that someone happens to turn off javascript after logging in. I would highly encourage you to still allow this fallback mechanism. Something like this will adhere to "best practices" and accomplish your goal:

<a href="javascript_required.html" onclick="doSomething(); return false;">go</a>
cowgod
as the href value will be shown to user in the status bar? (the bar at the bottom) I would consider using a bit more user friendly link eg. "js.html?doSomething" the page can still be static html. This way the user will clearly see a difference between links.
Gene
You can override this value with the title attribute, e.g. <a href="javascript_required.html" title="Does Something" onclick="doSomething(); return false;">go</a>
Conspicuous Compiler
A: 

Method #2 has a syntax error in FF3 and IE7. I prefer methods #1 and #3, because #4 dirty the URI with '#' although causes less typing... Obviously, as noted by other responses, the best solution is separate html from event handling.

Pier Luigi
Method #2 of what? The questions don't stay in the same order as when YOU saw them.
Diodeus
Method #4 *won't* mess up the URI if you make sure to `return false`.For that reason method #4 is probably best (of those listed).
Már Örlygsson
A: 

What Not To Do

<a href="javascript:doSomething()">link</a>
<a href="#" onClick="doSomething()">link</a>
<a href="#" onClick="javascript:doSomething();">link</a>
<a href="#" onClick="javascript:doSomething(); return false;">link</a>

via http://www.javascripttoolbox.com/bestpractices/

drorhan