在我的网站上记录超链接点击 [英] Logging hyperlink clicks on my website

查看:143
本文介绍了在我的网站上记录超链接点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我允许其他开发者托管内容。
我的目标是记录每个超链接上的点击(即使是由其他开发者托管的内容)。



我最初的方法是如下所示:

  $('a')。click(function(event)
{
/ /做我的日志
返回true;
}
);



现在用上面的方法,我面临以下问题:




  • 开发人员可能在锚链接中存在图像,因此事件目标是图像而不是href。许多开发人员都有自己的方式使用onclick事件而不是简单的href =''attr

  • 有些开发人员将自定义属性添加到标记中,并使用自定义函数来处理点击



所以基本上,问题是,可用的锚点标记种类繁多,并且记录点击并不那么简单。

许多情况下允许我记录我想要的数据,但是有一些情况严重破坏了代码。



我在这个论坛发帖的目的是:




  • 讨论什么是在动态环境中进行超级链接点击登录的正确方法

  • 是否有插件可以使用这种功能。



我知道facebook和google都有这个功能,但是他们对他们的环境中托管的东西有控制权。



任何帮助非常感谢。

解决方案

为每个链接添加点击处理程序不是好主意。您应该使用事件委托(它只会在文档的根部附加一个事件处理程序):

  $(document).delegate('a','click',function(event){
// logging
});

更新(17.12.2011): b

由于jQuery 1.7,人们会使用 .on() [docs]

 <$ c $ $(document).on('click','a',function(event){
//记录
});






关于您的问题: b
$ b


开发人员可能在锚链接中有图片,所以事件目标是图片而不是href


只要传播未取消,事件就会冒泡。这取决于你想记录什么。使用委托 event.target 属性将指向图片,但是 this (处理程序内部)将指向 a 元素。

所以你应该在这里没有问题(例如: http://jsfiddle.net/cR4DE/ )。

但是这也意味着如果开发者取消传播,你将错过点击。



注意:你可以解决这个问题,让事件处理程序触发在 捕获阶段 中,但IE不支持此操作(因此jQuery不会)。


许多开发人员都有自己的方式来处理href点击,使用onclick事件而不是简单的href =''attr


这不会触及现有的事件处理程序。


有些开发人员添加自定义att r,添加到标签,并拥有用于处理点击的自定义功能。


不确定这里是什么意思。






这也取决于其他内容的包含方式。例如。上述代码不会跟踪iframe中的点击次数。


I have a website, where I allow other developers to host content. My aim is to log clicks on every hyperlink (even the content that is hosted by other developers) ,which exists on the page.

My initial approach was as follows:

$('a').click(function(event)
             {
                //do my logging
                return true;
             }
);

Now with the above approach , I am facing the following issues:

  • Developers may have images inside the anchor link, so the events target is an image rather than href
  • Many developers have their own way of handling an href click , using an onclick event rather than a simply href='' attr
  • Some developers add their custom attr , to the tag, and have custom functions to handle the clicks

so basically , the issue is , there is a huge variety of anchor tags available, and logging clicks is not as simple.
Many cases allowed me to log the data I wanted, but a few cases , broke the code badly.

My aim to post on this forum was:

  • to discuss what is the right approach to do hyperlink clicks logging in a dynamic environment
  • is there a plugin out there , which allows a functionality like this.

I know facebook and google have this , but they have a totol control, on what is being hosted in their environments.

Any help is greatly appreciated.

解决方案

Adding a click handler to every link is not a good idea. You should make use of event delegation (which will only attach one event handler at the root of the document):

$(document).delegate('a', 'click', function(event) {
    // logging
});

Update (17.12.2011):

Since jQuery 1.7, one would use .on() [docs]:

$(document).on('click', 'a', function(event) {
    // logging
});


Regarding your problems:

Developers may have images inside the anchor link, so the events target is an image rather than href

Events bubble up as long as propagation is not canceled. It depends on what you want to log. With delegate the event.target property will point to the image, but this (inside the handler) will point to the a element.
So you should have no problems here (example: http://jsfiddle.net/cR4DE/).

But that also means to you will miss clicks if the developers cancel the propagation.

(Side note: You could solve this letting the event handler fire in the capturing phase, but IE does not support this (hence jQuery does not either).)

Many developers have their own way of handling an href click , using an onclick event rather than a simply href='' attr

This will not touch existing event handlers.

Some developers add their custom attr , to the tag, and have custom functions to handle the clicks

Not sure what you mean here.


It also depends on how the other content is included. E.g. the above code won't track clicks in iframes.

这篇关于在我的网站上记录超链接点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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