的JavaScript / jQuery的:如何确保跨域点击跟踪事件的成功用户离开页面之前? [英] JavaScript/jQuery: How to make sure cross-domain click tracking event succeeds before the user leaves the page?

查看:190
本文介绍了的JavaScript / jQuery的:如何确保跨域点击跟踪事件的成功用户离开页面之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的点击,以添加一些迫切需要的人群来源的流行相关的功能在我们的企业内联网的各种网页的跟踪(在过去24小时内你的部门最流行的链接,等等。)

I'm implementing click tracking from various pages in our corporate intranet in order to add some sorely needed crowd-sourced popular link features ("most popular links in your department in the last 24 hours", etc.)

我使用jQuery的.live()绑定到页面上的所有链接元素MouseDown事件,筛选事件,然后在返回前火了各种数据的伪Ajax请求到后端服务器真正使链接操作大火:

I'm using jQuery's .live() to bind to the mousedown event for all link elements on the page, filter the event, and then fire off a pseudo-ajax request with various data to a back-end server before returning true so that the link action fires:

$("#contentarea a").live("mousedown", function(ev) {
    //
    // detect event, find closest link, process it  here
    //
    $.ajax({
        url: 'my-url',
        cache: false,
        dataType: 'jsonp',
        jsonp: 'cb',
        data: myDataString,
        success: function() {
            // silence is golden -- server does send success JSONP but 
            // regardless of success or failure, we allow the user to continue
        }
    });

    return true; // allow event to continue, user leaves the page.
}

正如你可能从上面的猜测,我有几个限制:

As you can probably guess from the above, I have several constraints:


  • 后端跟踪服务器是从调用页面不同的子域。我无法避开这个问题。这就是为什么我使用JSONP(和GET),而不是用POST适当的AJAX。我无法实现的AJAX代理作为Web服务器没有脚本的出站网络访问。
  • 这可能是不相关,但在完全公开的利益,内容和文字是一个主要内容的iframe中(这是不会改变的,我很可能会最终事件侦听器移动到父框架监视它的链接和所有子内容,但步骤1中得到它的1子窗口)的情况下,简化正常工作。家长和孩子都是同一个域。
  • 后端是IIS / ASP(同样,约束 - 不要问!),所以我不能马上叉的后端进程或终止响应,但保留处理我就像在一个更好的平台

尽管如此,在大多数情况下,该系统的工作原理 - 我点击页面上的链接,且都出现在数据库pretty无缝

Despite all this, for the most part, the system works -- I click links on the page, and they appear in the database pretty seamlessly.

然而,它是不可靠的 - 为大量的联系,特别是场外具有自己的目标设定为_top链接,它们不会出现。如果链接在新标签或窗口打开,它会注册确定。

However it isn't reliable -- for a large number of links, particularly off-site links that have their target set to "_top", they don't appear. If the link is opened in a new tab or window, it registers OK.

我已经排除了脚本错误 - 它似乎,无论是:

I have ruled out script errors -- it seems that either:

(一)请求永远不会做它及时后端;或

(a) the request is never making it to the back-end in time; or

(二)请求作出,但ASP是检测​​客户端不久之后断开,而且因为它是一个GET请求,不处理它。

(b) the request is making it, but ASP is detecting that the client is disconnecting shortly afterwards, and as it is a GET request, is not processing it.

我怀疑(B),由于延迟到服务器的速度非常快,环节多注册确定。如果我把在事件触发后,警报弹出窗口,或设置返回值为false,点击注册确定。

I suspect (b), since latency to the server is very fast and many links register OK. If I put in an alert pop-up after the event fires, or set the return value to false, the click is registered OK.

我如何能解决这个问题(因为我不能改变我的有限的情况下)有什么建议?我不能让GET请求同步,因为它是不正确的AJAX。

Any advice on how I can solve this (in the context that I cannot change my constraints)? I can't make the GET request synchronous as it is not true AJAX.

问::将它的工作更好,如果我是做一个POST请求到ASP?如果(b)是罪魁祸首会是不同的表现为POST VS得到什么?如果是这样,我可以使用一个隐藏的iframe /表来发表的数据。不过,我怀疑这将是缓慢和笨重,而且可能仍然不能使它时间。我就没法听,看是否请求完成,因为它是跨域。

Q: Would it work better if I was making a POST request to ASP? If (b) is the culprit would it behave differently for POST vs GET? If so, I could use a hidden iframe/form to POST the data. however, I suspect this would be slower and more clunky, and might still not make it in time. I wouldn't be able to listen to see if the request completes because it is cross-domain.

问::我可以只延迟添加到脚本的GET请求被触发关闭后?如何在单线程的方式做到这一点?我需要从我的函数返回true,确保默认事件最终触发,所以我不能使用的setTimeout()。将紧密循环等待'成功'开火并设置一些变量的工作?我很担心,这将冻结的东西太多了,响应会很慢。我认为jQuery的延迟()插件仅仅是一个循环呢?

Q: Can I just add a delay to the script after the GET request is fired off? How do I do this in a single-threaded way? I need to return true from my function, to ensure the default event eventually fires, so I can't use setTimeout(). Would a tight loop waiting for 'success' to fire and set some variable work? I'm worried that this would freeze up things too much and the response would be slowed down. I assume the jQuery delay() plugin is just a loop too?

抑或是别的东西,我没有想到的可能是罪魁祸首?

Or is something else I haven't thought of likely to be the culprit?

我不需要防弹可靠性。如果所有的环节都同样开捕的95%的时间它是好的。然而现在,有些链接是开捕时间的100%,有的则是抓不到的 - 这是不会削减它为我想要实现

I don't need bullet-proof reliability. If all links are equally catchable 95% of the time it is fine. However right now, some links are catchable 100% of the time, while others are uncatchable -- which isn't going to cut it for what I want to achieve.

先谢谢了。

推荐答案

解决了!

简短的回答是:没有可靠的方法,这样做跨域使用GET请求。我试过各种,包括存储的事件,并试图后来重播事件,黑客的所有方式试图获取工作。

The short answer is: there is no reliable way to do this cross-domain with a GET request. I tried all sorts, including storing the event and trying to replay the event later, and all manner of hacks to try to get that to work.

我又试图紧的循环,他们是不可靠的要么。

I then tried tight loops, and they weren't reliable either.

最后,我只给中并用于动态创建的形式发布的结果,设置为隐藏iFrame目标。

Finally, I just gave in and used a dynamically created form that POSTed the results, with the target set to a hidden iFrame.

的工作可靠 - 它似乎在浏览器暂停在移动之前完成其POST请求,和ASP荣誉的POST。原来这不是'笨拙'的。当然,由于浏览器的安全模式我看不到结果......但它并没有在这种情况下无​​所谓。

That works reliably -- it seems the browser pauses to finish its POST request before moving on, and ASP honours the POST. Turns out it's not 'clunky' at all. Sure, due to the browser security model I can't see the result... but it doesn't matter in this case.

我现在踢自己,我并没有第一次尝试这样的选择。

I am now kicking myself that I didn't try that option first.

这篇关于的JavaScript / jQuery的:如何确保跨域点击跟踪事件的成功用户离开页面之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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