This question may have been asked before, but I had trouble finding an answer, since I didn't quite know what search terms to use.
In HTML, how do you make a link go a place, but execute JavaScript instead when it is clicked? Right now, I have a link that's somewhat like this:
<a href="#" onclick="javascript:dostuff()">Stuff</a>
But, the thing is, when someone right clicks the link, I want them to be able to copy a direct URL to the link. Also, if someone's browser does not support JavaScript, or they have it disabled, I would still like them to be able to reach the page开发者_Go百科. Is it just
<a href="http://linkhere" onclick="javascript:dostuff()">Stuff</a>?
Or is there something more complicated?
Thanks!
<a href="http://linkhere" onclick="return dostuff()">Stuff</a>
Make sure that the dosuff function returns false if you want the link to not be followed when the script runs.
(The 'javascript:' is pointless. It labels a loop, but you don't have a loop. See the HTML spec for how to specify which language is being used in the intrinsic event handler attributes — better yet, use unobtrusive JavaScript and progressive enhancement).
You need to let the event return false to block the default action.
<a href="http://example.com" onclick="doSomething(); return false;">
精彩评论