0

I want to disable Ctrl + mouse Left Click event on links. I tried with keypress event but it doesn't work:

$('a').keypress(function (e){  
    if (e.ctrlKey && e.keyCode === 13) {
        return false;
    }
});

jsFiddle

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Mustapha Aoussar
  • 5,833
  • 15
  • 62
  • 107
  • By CTRL + left click you mean opening the context menu? On Windows, that would be the same a clicking the right mouse button. – Codo Feb 02 '15 at 11:47
  • possible duplicate of [How to detect control+click in Javascript from an onclick div attribute?](http://stackoverflow.com/questions/16190455/how-to-detect-controlclick-in-javascript-from-an-onclick-div-attribute) – makeitmorehuman Feb 02 '15 at 11:48
  • 3
    It is never a good idee, to change the human expected behavior. – Ron van der Heijden Feb 02 '15 at 11:50
  • 3
    I specifically searched for these terms in order to make everyone out there who thinks this is a good idea *an appeal*: **Please, please, don't do this.** Seriously. For the love of God. Don't. do. this. – Marc.2377 Oct 15 '16 at 00:02
  • 3
    ...On why (not): HTML hyperlinks are meant to *redirect* the user to some other content, and how it's done is browser's problem (and users'). **If you provide me a hyperlink which doesn't open in a new tab if I control+click it, I will find a way around that, and get pissed at you.** – Marc.2377 Oct 15 '16 at 00:16
  • FWIW, questions like this (and the answers) can be helpful insomuch as they help debug things or override things when such functionality does happen to occur inadvertently or in other people's code. – Max Starkenburg Jul 03 '18 at 17:20

2 Answers2

12

The code that you have disables Ctrl+enter. To disable Ctrl+click, you would use the click event:

$('a').click(function (e){  
    if (e.ctrlKey) {
        return false;
    }
});

Demo: http://jsfiddle.net/WYxUE/45/

Note: Actually disabling ctrl+click would normally not be a good idea, and not very effective. In windows it's used to open the page in a new tab, and if that is disabled you can just use shift+click to open it in a new window instead, or right click and choose Open link in new tab.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    "if that is disabled you can"... Also, right-click -> *copy link address* and then Control+T and Control+V, or use a Greasemonkey script, or... well, just don't rely on it. – Marc.2377 Oct 15 '16 at 00:36
3

The full code is:

<script src="https://code.jquery.com/jquery-2.1.0.js"></script>

<script>

$(window).load(function(){

    // Disable CTRL Mouse Click    
    $('a').click(function (e){  
      if (e.ctrlKey) {
        return false;
      }
    })

    // Disable SHIFT Mouse Click    
    $('a').click(function (e){  
      if (e.shiftKey) {
        return false;
      }
    })

})

</script>
Grzegorz Smulko
  • 2,525
  • 1
  • 29
  • 42