问题描述
我正在尝试跟踪外部链接上的点击次数(不使用重定向页面)。
I am trying to track clicks on an external link (without using a "redirect page").
如何在用户关注链接时捕获事件,无论用户是否:
How can I capture an event when a user follows a link, regardless of whether the user:
- 左键点击链接
- 右键点击链接并在新窗口中打开
- 使用键盘激活链接
- 还有其他方法可以激活链接吗?
- Left clicks on the link
- Right clicks on the link and open it in a new window
- Use the keyboard to activate the link
- Is there other ways to activate a link?
onClick事件仅适用于第一个。
The onClick event only applies to the first.
如果设置 href =javascript:fireEventAndFollowLink()
用户将无法在新窗口中打开链接(2),因此这不是解决方案。
If setting href="javascript:fireEventAndFollowLink()"
the user will not be able to open the link in a new window (2), so this is not a sollution.
推荐答案
可以通过某种方式触发链接(假设是现代浏览器,请参阅脚注):
A link can be triggered in some ways (assuming modern browsers, see foot note):
- 左键单击
- 对于
< a target =_ blank>
或< a target =_ new>
,该页面将加载到新标签。 - 当用户按时,页面将加载到新标签页。
- 当用户按时,页面将加载到新窗口。
- 其他
目标
值,该链接可以加载到当前屏幕或另一个框架。
- 对于
- Left click
- For
<a target="_blank">
or<a target="_new">
, the page will be loaded in a new tab. - When the user presses , the page will be loaded in a new tab.
- When the user presses , the page will be loaded in a new window.
- For other
target
values, the link can be loaded in the current screen or another frame.
- For
- 无论修改键如何,页面都将加载到新标签中。
- 选择在新窗口中打开链接(选项卡,窗口)时,页面将加载到新选项卡窗口中。
- 当没有选择任何这些时,没有任何加载
- Contextmenu(参见3.)
- - 参见1.
- Contextmenu (see 3.)
- - See 1.
如何捕获这些链接
没有可靠的捕获所有链接事件的方法。
How to capture these links
There is no reliable way to capture all link events.
-
onmouseup
事件可用于检测1,2和3.
可以通过event.button
属性。在所有非IE浏览器中,0 =左,1 =中,2 =右。在IE中,1 =左,2 =中,4 =右。 -
onclick
事件可用于捕获1和2 。
可以通过event.altKey
和event.shiftKey
检测修饰键。属性。 -
oncontextmenu
事件可用于捕获3 -
onkeydown
事件可用于捕获4。
- The
onmouseup
event can be used to detect 1, 2, and 3.
The used button can be tracked through theevent.button
(orevent.which
) property. In all non-IE browsers, 0=Left, 1=Middle, 2=Right. In IE, 1=Left, 2=Middle, 4=Right. - The
onclick
event can be used to capture 1 and 2.
The modifier key can be detected through theevent.altKey
andevent.shiftKey
properties. - The
oncontextmenu
event can be used to capture 3 - The
onkeydown
event can be used to capture 4.
修饰符的行为密钥在任何规范中都没有形式化,因此是特定于浏览器的。至少Firefox(至少3+)和Chrome(全部?)实现此关键行为。
- 如何拦截链接上的动作(< a>
)?
Related question - How to intercept an action on a link (<a>
)?
这篇关于关注链接时捕获事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!