开发者

HTML/JavaScript Linking

开发者 https://www.devze.com 2022-12-09 19:45 出处:网络
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.

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;">
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号