I created a function in javascript that i add to an anchor as such
javascript :
somefunction = function () {alert(\'foo\')}
html :
The trick is that your onclick returns the value of somefunction, so that if you make sure somefunction always return false, then the event will return false as well, and cancel the default behavior of the anchor.
So this should solve your problem:
somefunction = function () { alert('foo'); return false; }
But I should mention that when there is an error somewhere in somefunction, the rest of the javascript will not execute, and the browser will still follow the link to #. It is advisable, therefore, to use javascript:void(0) instead, as this will rid you of that problem.