jQuery: click handler is not invoked when link is

2019-06-26 04:04发布

I have noticed when user clicks on a link with, say middle button, or shift/ctrl+left button, click handler attached to hyperlink is not called.

I have seen solutions to track mousedown event, but what I'd like is to track exact event of following a link.

Are there any suggestions? Thanks

4条回答
倾城 Initia
2楼-- · 2019-06-26 04:30

If the link is on your site then track it when the page loads instead of on the page where they get the link. If the link is to another site you need to use a redirect url so your site can track it(example:http://yoursite.com/redirect.html?redirect=http://othersite.com).

In the redirect page you might do something like this(if you want to use javascript):

$(document).ready(function(){
  //insert your tracking code here...

  var redirect = getParameterByName('redirect');
  if(redirect != ''){
    window.location = redirect;
  }
});

//From http://stackoverflow.com/questions/901115/get-querystring-with-jquery/901144#901144
function getParameterByName( name ){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
    return "";
    else
    return results[1];
}
查看更多
家丑人穷心不美
3楼-- · 2019-06-26 04:44

you can use 'mousedown' 'mouseup' events in combination with "event.which".

example: http://jsbin.com/ikahe/edit

查看更多
爷的心禁止访问
4楼-- · 2019-06-26 04:45

mousedown/mouseup is indeed the only way you can get notified of middle button interaction, so detecting a down-then-up event without intervening mouseout event is more or less the best you can do. It's not very good.

I wouldn't bother, since even if you trapped this one eventuality, there are many other interactions you can't pick up. As well as middle-click (which might not be ‘Open in new tab’ in all browser/configurations; for example in IE6 that'll be the user turning on scrolling mode), the user might right-click and ‘Open in new window’, or drag the link to the address bar or new tab, or various other browser-specific actions to perform a navigation.

查看更多
聊天终结者
5楼-- · 2019-06-26 04:47

How to do click hijack right... or rather left.

http://mislav.uniqpath.com/2011/03/click-hijack/

查看更多
登录 后发表回答