JavaScript 中链接日志记录的最佳实践?
基本上,onclick
将仅在左键单击时触发。
大多数地方推荐 onmouseup
,因为它会在左键、中键和右键单击时触发。右键当然可以过滤掉。但是,通过左键单击或中键单击从文本拖动到链接,仍然可能会从 onmouseup
生成误报。
JavaScript 中是否有更直接的方法,当且仅当跟随外部链接时才会触发?或者 onmouseup
是唯一实用的解决方案吗?
Basically, onclick
will trigger exclusively on left clicks.
Most places recommend onmouseup
, because it triggers on left, middle, and right clicks. The right clicks can of course be filtered out. However, it is still possible to generate false positives from onmouseup
by left or middle click dragging from text to the link.
Is there a more direct way in JavaScript that will be triggered if and only if an external link is followed? Or is onmouseup
the only practical solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
window.onbeforeunload
。让它调用一个执行您命令的函数。你想检测什么?You could try something with
window.onbeforeunload
. Have it call a function that does your bidding. What are you wanting to detect?我只喜欢
onmouseup
。对于
onbeforeunload
; Opera 不支持此方法。其余所有主流浏览器都支持它。也没有公开的规范。onbeforeunload
是由 Microsoft IE 4 引入的,随后被其他浏览器复制。I prefer
onmouseup
only.For
onbeforeunload
; this method is not supported in Opera. Rest all leading browsers supports it. There is no public specification for it too.onbeforeunload
was introduced by Microsoft IE 4 and has subsequently been copied by other browsers.