跟踪用ajax的出站链接(JavaScript的)链接、ajax、JavaScript

2023-09-10 18:31:23 作者:孤心亦寒ぴ

我有一个关于这个答案的问题在这里 http://stackoverflow.com/a/2078233/560972

I have a question regarding this answer here http://stackoverflow.com/a/2078233/560972

据我了解,使用JS(阿贾克斯)来跟踪传出的链接点击的最常见的问题是,前(快)的脚本可以获取数据,有时用户离开网页...?

As i understand, the most common problem using JS (Ajax) to track outgoing link clicks is that sometimes user leaves page before (faster) the script can grab the data...?

因此​​,也许有可能迫使某种延迟为了让脚本完成录制,然后让用户浏览到了另一个网站?延时当链接被点击和导航离开时

So maybe it is possible to force some sort of delay in order to let script finish recording and then let user navigate away to other site? Delay when link is clicked and navigation away occurs

这是否会帮助吗?我想〜200毫秒/ 300毫秒不会对用户可见,但它可能是不够的Ajax调用?

Will this help? I suppose ~200ms/300ms won't be visible for user but it could be enough for ajax call?

你觉得是什么?

谢谢!

推荐答案

使用jQuery:

$('a').click(function(e) {
   //check that it is offsite
   if($(this).attr("href").indexOf("http")==1) {
      //prevent the redirect;
      e.preventDefault();
      //do your tracking 
      $.ajax{
          url: 'yourtracking.php',
          data: "link=" + $(this).attr("href"),
          complete: function(){
              //now do the redirect
              window.location = $(this).attr("href");
          }

     }
  }
});