What I understand from your words is that you want to create a link just to run JavaScript code.
Then you should consider that there are people who blocks JavaScript out there in their browsers.
So if you are really going to use that link only for running a JavaScript function then you should add it dynamically so it won't be even seen if the users didn't enable their JavaScript in the browser and you are using that link just to trigger a JavaScript function which makes no sense to use a link like that when JavaScript is disabled in the browser.
For that reason neither of them is good when JavaScript is disabled.
Aand if JavaScript is enabled and you only want to use that link to invoke a JavaScript function then
<a href="javascript:void(0)" onclick="myJsFunc();">Link</a>
is far better way than using
<a href="#" onclick="myJsFunc();">Link</a>
because href="#" is going to cause the page to do actions that are not needed.
Also, another reason why <a href="javascript:void(0)" onclick="myJsFunc();">Link</a>
is better than <a href="#" onclick="myJsFunc();">Link</a>
is that JavaScript is used as the default scripting language for most of the browsers. As an example Internet Explorer, uses an onclick attribute to define the type of scripting language that would be used. Unless another good scripting language pops up, JavaScript will be used by Internet Explorer as the default too, but if another scripting language used javascript:
, it would let Internet Explorer to understand which scripting language is being used.
Considering this, I would prefer using and exercising on
<a href="javascript:void(0)" onclick="myJsFunc();">Link</a>
enough to make it a habit and to be more user friendly please add that kind of links within the JavaScript code:
$(document).ready(function(){
$(".blabla").append('<a href="javascript:void(0)" onclick="myJsFunc();">Link</a>')
});
onclick=
. Using jQuery for example might look "lightweight" for its size ($('#foo').click(doSomething);
), but the amount of work and function calls that this causes is immense. – Camilo Martin May 27 '13 at 0:25<a href="javascript:void(0)" onclick="myJsFunc();">
makes absolutely no sense. If you must use thejavascript:
psuedo-protocol, you don't need theonclick
attribute as well.<a href="javascript:myJsFunc();">
will do just fine. – Wesley Murch Jun 2 '13 at 17:41myJsFunc()
has a return value, your page will break. jsfiddle.net/jAd9G You'd still have to usevoid
like so:<a href="javascript:void myJsFunc();">
. But then, the behavior would still differ. Invoking the link via context menu does not trigger theclick
event. – gilly3 Jul 31 '13 at 0:24<a href="javascript:;" onclick="myEvent()"
? – 3k- Nov 20 '13 at 12:12javascript:;
is a lot quicker to type thanjavascript:void(0)
– Mike Causer Dec 12 '13 at 12:26