0

I'm appending a string to the HREF for outgoing links on my site, it works like this. I need to append it on click because although I wrote it as a string here, it's generated at the time of click so I can't just append it to all the links before any clicking happens:

//middle-click
$(document).on("mousedown", function (e1) {
    $(document).one("mouseup", function (e2) {
        if (e1.which == 2 && e1.target == e2.target) {
            var e3 = $.event.fix(e2);
            e3.type = "middleclick";
            $(e2.target).trigger(e3)
        }
   });
});

$('a[href*="link.php"]').on('click middleclick', function(e) {
    let link = this.getAttribute('href');
    e.preventDefault();
    window.open(`${link}`+`&appended=1`);
});

So this works for clicks and clicks with middle mouse button BUT what about users who right-click and open in a new window/tab? How do I detect this in jquery?

pg.
  • 2,503
  • 4
  • 42
  • 67
  • Possible duplicate of [How to distinguish between left and right mouse click with jQuery](https://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery) – showdev Mar 15 '19 at 19:48
  • It's similar I admit, I guess I could change my if e1.which==2 to 2 OR 3? – pg. Mar 15 '19 at 19:58
  • According to the documentation, [`event.which`](https://api.jquery.com/event.which/) reports "1 for left button, 2 for middle, and 3 for right" for the `mousedown` event. – showdev Mar 15 '19 at 20:29

0 Answers0