0

Trying to handle ctrl+click event on Internet explorer. All the other versions like Alt+click, Shift+click works fine, but not ctrl+click. Checked on Windows IE Edge and 10,9,8 compatability modes.

Also read on other multiple stackoverflow questions that any mouse click should trigger 'click' event on IE. This ain't happening too. Only mouseup and mousedown events are getting handled in this case.

Is there any change that can be made to below code that makes ctrl+click and mouse clicks work?

<html>
<body>
  <a href="https://www.google.com" target="_blank">Google Link</a>
  <script>
    var handler = function(event) {
      console.log(event);
    };
    var anchorTag = document.getElementsByTagName('a')[0];
    if (window.addEventListener) {
      anchorTag.addEventListener('click', handler, false);
    }
    else anchorTag.attachEvent("onclick", handler);
  </script>
</body>
</html>

Much appreciated if can be answered without Jquery.

Sanjay
  • 111
  • 1
  • 9
  • I don't see any code that attempts to check for the `CTRL` key in your attempt? What have you tried? Where is your `handler` code? – Scott Marcus Feb 27 '18 at 18:34
  • You must have other issues because the standard `click` event absolutely works in all IE versions going back decades. Prior to IE 9, you need to use `attachEvent("onclick", handler)`, but it does work. – Scott Marcus Feb 27 '18 at 18:36
  • var handler = function(){..} is the first line in the script. the log statement inside handler logs for all other click events but not ctrl+click. i would work out the details once the handler starts getting triggered. – Sanjay Feb 27 '18 at 18:38
  • should've tested in other boxes before posting. As you said, something could be wrong on this particular machine. will update once tested. – Sanjay Feb 27 '18 at 18:39
  • it might be because ctrl+click is edge's shortcut for opening a new tab and so instead of it registering as a dom event the browser just handles it and does not trickle it down to the dom. Try with the other open tab/window shortcuts and see if it does the same (CTLR+SHIFT+CLICK, and ALT+SHIFT+CLICK) – Patrick Evans Feb 27 '18 at 18:48
  • Tested on two machines(windows10 and windows8), results are same. @PatrickEvans yes, that is intended shortcut in all versions of IE and other browsers too. Similar to ctrl+click, shift+click and alt+click do their own stuff of opening in new window, opening in new tab and loading the tab. But the click event is triggered in these other 2 cases. – Sanjay Feb 27 '18 at 18:49
  • then your problem probably is with the browser intercepting that and not communicating it with the dom. you could [try overriding it](https://stackoverflow.com/questions/3680919/overriding-browsers-keyboard-shortcuts) – Patrick Evans Feb 27 '18 at 18:56
  • Not sure how that is valid in this case. IE as per documentation should trigger click event in this case too as does all the other browsers. Also, overriding global ctrl + click with custom thingy shouldn't be recommended as this would change how all the links would behave with same combination. – Sanjay Feb 27 '18 at 19:25

0 Answers0