Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Detecting middle click OR ctrl + click in vanilla JS

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #166389
    shaneisme
    Participant

    Using an Google Analytics (GA), I use e.preventDefault() on external links so I can give GA about 100ms to detect the click before leaving the site. This is a common practice, from what I’ve read.

    I never use target=_blank unless I’ve got a gun pointed to my head, and I’ve finally brought around the talking heads to stop using it. GREAT… BUT… using e.preventDefault(), this also stops the defaults for middle click as well as ctrl+click to open in a new tab should the user choose to.

    Now, I found that e.which being equal to 2 means a middle click, and that’s reliable enough. What I can’t find good info on is detecting a ctrl+click without relying on jQuery’s keydown – in fact this has to be vanilla JS through and through.

    Does anyone have experience with this specifically that’s reliable?

    #166530
    Taufik Nurrohman
    Participant

    Depends with what element you want to click → http://jsbin.com/boxosipo/1/edit

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.