为提升您的使用体验,本站正在维护,部分功能暂时无法使用。如果本站文章无法解决您的问题,您想要向社区提问的话,请到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 提问,我们的支持社区将会很快回复您的疑问。

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

Is there a way to force firefox to send ajax-request when user clicks on a link and redirect occurs?

  • 12 个回答
  • 1 人有此问题
  • 10 次查看
  • 最后回复者为 vitalatron

more options

I'm trying to send ajax-request with web-analytics data when user clicks on a link. But Firefox cancels the request and moves to the link location. Sync requests or waiting for response is not an option because performance is critical.

I'm trying to send ajax-request with web-analytics data when user clicks on a link. But Firefox cancels the request and moves to the link location. Sync requests or waiting for response is not an option because performance is critical.

所有回复 (12)

more options

Hi!

I'm not quite sure what you're trying to do. Are you trying to write code that sends an Ajax request to the server and then do something with the data that is sent back?

Thanks for the clarification!

more options

Hi!

Actually I use the Sitecatalyst analytics library and the code which makes ajax calls is already implemented in it. And no, I don't need server's responses at all because all I need is to send analytics data (e.g. link name user clicked on) to the server.

PS: after some measurements using browser console logging and wireshark I found that it is about 500ms time gap between user clicks on link and ajax request is sent, but JS handling takes only about 7ms of it.

more options

It sounds like you want the Ajax request to be sent to the server before the user's browser follows the link.

Could you show a small code sample?

more options

I think when Firefox unloads a page it terminates all pending requests generated by that page. If you can't wait, consider handling it on the server side.

more options

Epicaleb, it's just something like that: <script> ... jQuery(".web-analytics") .on("click", function(){

   var data = jQuery(this).data("analytics");
   s.tl(data); // "s" is obfuscated analytics framework code

}); ... </script> ... <a href="..." class="header-nav web-analytics" data-analytics="{foo:'bar'}">Home</a>

jscher2000, what do you mean by handling it on the server side?

more options

By handling it on the server side, I mean logging the event through the request to the server.

As an example, if you have used Google search, you've probably noticed that result links do not load directly, but go are routed through a Google tracking page. This is attached using JavaScript.

more options

Correct me if I'm wrong here:

jQuery.data() saves the {"foo": "bar"} JSON object to variable data, and then the s.tl() call sends data to the server, right?

You could use jQuery's event.preventDefault() method to stop the browser from automatically following the link on click. You could wait until the Ajax request was finished before following the link.


I hope that solved your problem!

If it did, would you please choose this answer as your solution? It would help other Firefox users to find help on the forums faster and more efficiently. Thanks!

And of course, feel free to post back if you need more help!

Happy browsing!

由Epicaleb于修改

more options

jscher2000, now I understand but it can't be a solution because I can not know what exactly the analytics object is going to send in request so I can not imitate it on server side.

Epicaleb, you assumption is correct but solution is not acceptable because it badly affects performance (as it adds about 500ms to each navigation) and performance is critical.

more options

Maybe Adobe has a solution for this, as it affects all of their customers...

more options

jscher2000, thanks I'll ask them.

more options

Because Ajax is asynchronous, the script will not naturally wait for the request to complete before moving on (e.g. following a link), unless you build a checker into the code or use jQuery's powerful Ajax APIs.

Jscher2000's advice is good: Adobe is more likely to be able to provide support than Mozilla Support members.  :)

more options

Thanks anyway!