Reputation: 2246
Maybe I am missing something simple here, but it is frustrating me. So what I need to do is update something on my server when this div around a link is clicked (I am doing click thru analytics). I figured I would do a click event and do an ajax call (via get).
$.get("stat/418491289);
But the problem is, the call isn't being made. The user is being sent to the next page and not calling get call isn't being completed/called. I would know because on the server it would update the database. Maybe I shouldn't be making an ajax call, or maybe there is something I should be doing, but I am not sure. I cannot suspend the link action for now because it is wrapped in the div. What I would like to do ideally is fire off the request and then not worry about it. Updating the stat is not critical, so if it doesn't work 1% of the time, that is more than ok. How would I do this?
I guess I might not be being clear. What I am asking is: Can I fire off a request to my server via javascript before the browser goes to the linked page? I don't care what the response is, I just need to send the request.
Upvotes: 0
Views: 603
Reputation: 31043
if i have understood the question clearly you need to use preventDefault
inorder to cancel the default behavior of the link like
$("#linkID").click(function(e){
e.preventDefault();
//ajax call here
});
or put a return false;
$("#linkID").click(function(){
//ajax call here
return false;
});
Upvotes: 2
Reputation: 25565
Have you checked the network traffic for the page to see if the get request is getting made at all (vs not being handled by the server)? Perhaps the URL is not right and you're getting back a 404 response.
It also might be how your handling the click event. If you ajax request is being made from the div click handler and the user is clicking on a link () then the link will handle the click first taking the user to the page. I'm not sure if the default link handler propagates clicks to parent elements.
If this is the problem, might be easier to see some code to help find the issue.
Upvotes: 1