关注链接时捕获事件 [英] Catching event when following a link

查看:122
本文介绍了关注链接时捕获事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试跟踪外部链接上的点击次数(不使用重定向页面)。

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:


  1. 左键点击链接

  2. 右键点击链接并在新窗口中打开

  3. 使用键盘激活链接

  4. 还有其他方法可以激活链接吗?

  1. Left clicks on the link
  2. Right clicks on the link and open it in a new window
  3. Use the keyboard to activate the link
  4. 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):


  1. 左键单击


    • 对于< a target =_ blank> < a target =_ new> ,该页面将加载到新标签。

    • 当用户按 CTRL 时,页面将加载到新标签页

    • 当用户按 SHIFT 时,页面将加载到新窗口

    • 其他目标值,该链接可以加载到当前屏幕另一个框架

  1. Left click
    • For <a target="_blank"> or <a target="_new">, the page will be loaded in a new tab.
    • When the user presses CTRL, the page will be loaded in a new tab.
    • When the user presses SHIFT, 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.

  • 无论修改键如何,页面都将加载到新标签中。


  • 选择在新窗口中打开链接(选项卡,窗口)时,页面将加载到新选项卡窗口中。

  • 当没有选择任何这些时,没有任何加载


  • Contextmenu(参见3.)

  • 输入 - 参见1.

  • Contextmenu (see 3.)
  • Enter - See 1.

如何捕获这些链接

没有可靠的捕获所有链接事件的方法。

How to capture these links
There is no reliable way to capture all link events.


  • onmouseup 事件可用于检测1,2和3.

    可以通过 event.button (或 event.which 属性。在所有非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 the event.button (or event.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 the event.altKey and event.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>)?

这篇关于关注链接时捕获事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆