4

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

glaz666
  • 8,707
  • 19
  • 56
  • 75

4 Answers4

3

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.

bobince
  • 528,062
  • 107
  • 651
  • 834
2

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 https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/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];
}
Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Lee
  • 1,652
  • 1
  • 13
  • 21
  • It is possible solution, but not in my case, as I want to check usability by finding which controls are used on the site – glaz666 Apr 06 '10 at 07:52
  • 1
    Maybe you need to reword your question. "what I'd like is to track exact event of following a link." If someone right clicks there is no way to know what action they performed, but you can determine if they followed the link by making the links unique and tracking them at the destination. The end result should be the same. – Lee Apr 06 '10 at 12:49
1

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

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

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • I see that which returns 1, 2,3, but what about 2 buttons mouse or mac? :) – glaz666 Apr 05 '10 at 11:39
  • I'm not 100% sure, but jQuery should deliver the same results on different browsers or OS, 1=left, 2=middle, 3=right – jAndy Apr 05 '10 at 11:44
  • See http://www.quirksmode.org/js/events_properties.html#button for the sordid details on `which` and `button`. – bobince Apr 05 '10 at 14:56
0

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

https://web.archive.org/web/20160305021055/http://mislav.net/2011/03/click-hijack/

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
doublejosh
  • 5,548
  • 4
  • 39
  • 45