是否有一个简单的,通用的针对JQuery/Javascript的出站点击跟踪方法? [英] Is there a simple, universal outbound click tracking method for JQuery / Javascript?

查看:80
本文介绍了是否有一个简单的,通用的针对JQuery/Javascript的出站点击跟踪方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用Javascript/JQuery跟踪网页中所有出站点击的方法,而无需修改任何现有的页面代码.该解决方案必须适用于框架,iframe,来自不同域的内容,AJAX等.

I'm looking for a way of tracking all outbound clicks from a web page using Javascript/ JQuery without modifying any of the existing page code. The solution must work with frames, iframes, content from different domains, AJAX etc.

也许,对于通用解决方案而言,JavaScript是错误的技术.如果是这样,请让我知道会更好.

Perhaps, Javascript is the wrong technology for a universal solution. If so, please let me know what would be better.

推荐答案

捕获外部链接上的所有点击非常容易.

It's easy enough to capture all clicks on external links.

$(document).ready(function() {
    $("a[@href^=http]").each(function(){
        if(this.href.indexOf(location.hostname) == -1) {
            // Handle click here
        }
    });
});

使其适用于iframe有点棘手.由于相同来源政策,iframe应该位于同一域中.如果是这样,您应该能够将上面的css选择器更改为$("#iframe_id").contents().find("[@href^=http]")

Getting this to work for iframes is a little trickier. The iframe should reside from the same domain due to the Same origin policy. If so you should be able to change the css selector above to something like $("#iframe_id").contents().find("[@href^=http]")

这篇关于是否有一个简单的,通用的针对JQuery/Javascript的出站点击跟踪方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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