Tuesday, 13 August 2013

recreating natural link click event after preventing default

recreating natural link click event after preventing default

$(document).on('click','a',function(e){
e.preventDefault();
var href = $(this).attr('href');
sendAnalytics("clicked a link",function(){
// on success callback, continue with link's redirect
window.location = href;
// or location.assign(href);
});
setTimeout(function(){
window.location = href;
},5e3); // just incase callback never fires
});
How can I simulate the default link clicking behavior in this example? I
notice that if I open in a new tab (control+click, or middle-click), it
still redirects the current tab.
I know I can catch each click and detect if it is middle vs. left mouse,
but is there an easier way to accomplish this, including things like
control+click ?


So far the only alternative i see is to store the values in a cookie, then
on each pageload read cookies, send analytics, and delete cookies.

No comments:

Post a Comment