JavaScript/jQuery: How to make sure cross-domain click tracking event succeeds before the user leaves the page?

天大地大妈咪最大 提交于 2019-12-02 06:51:22

I would try to return false from the link event handler, remember the URL and navigate away only when JSONP request succeeds. Hopefully it shouldn't add too much latency. Considering you are on the inranet, it might be OK.

I would try a different approach. You can bind to a different event like:

$(window).unload(function(event) {
  // tracking code here
});

Solved!

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!