Is there a way to force firefox to send ajax-request when user clicks on a link and redirect occurs?
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.
Vsi odgovori (12)
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!
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.
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?
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.
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?
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.
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!
Spremenil Epicaleb
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.
Maybe Adobe has a solution for this, as it affects all of their customers...
jscher2000, thanks I'll ask them.
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. :)
Thanks anyway!