I'm using jQuery to bind to all links on the page (I'm using the 'click' event, but have tried various combinations of 'mousedown' and 'mouseup',together with bind() and live() to no avail).
I can intercept the click no problem (with all the above methods). What I am trying to do is send some data via a GET request, and when it completes, allow the default click action to proceed.
Since I am communicating accross-domain, I must use GET rather than POST, and so cannot make a synchronous call.
Therefore I have to return 'false' from the intercepted click event, store the event for later, then manually fire it again once the communication has completed. If I return true, the communication gets cut off mid-way as the page location changes.
The problem is, I can't find a way to fire the native click event later on.
var storedEvent;
$("#wrapper a").bind('click', function(event, processed) {
$(event.target).unbind('click'); // temporary to make code branching easier
storedEvent = event.target;
event.stopPropagation();
$.ajax({
dataType: 'jsonp',
data: linkData,
jsonp: 'cb',
url: 'xxx',
cache: false,
complete: function(response) {
// How do I now go back and fire the native click event here?
$(storedEvent).click();
}
});
return false;
}
I've tried using click() and trigger() where indicated, but neither worked.
I know the submission is succeeding, and the code is branching correctly -- I have debugged that far. I just don't seem to be able to replay the event.
Note that I can't do something simple, like store the href and set window.location later -- some of the links have their own onClicks set, while others have varius targets specified. I'd ideally like to just replay the event I stopped earlier.
I started off using event delegation with live() and had everything working, apart from this -- I have simplified it down to a bind() in order to simplify the problem.