3

Is there a way to prevent the default behaviour of the Microsoft Edge browser, when I use Ctrl + Left click on a link?

Edge is opening the link in a new tab, but I want to have my own event for Ctrl + Left click.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Tream
  • 1,049
  • 2
  • 13
  • 29
  • This is a common convention across browsers, at the browser level. Why would you want to change the way in which this convention works on [your site], thus potentially confusing the user? – Sampson Nov 03 '15 at 19:38

1 Answers1

3

You can assign a click event handler to your links.
Then, in a handler, you can do e.preventDefault() in order to prevent default browser action. e.ctrlKey will tell you if Ctrl button is clicked.

$("a").click(function(e) {
  if (e.ctrlKey === true)
  {
    e.preventDefault();
    // your custom actions
  }
});

Here is the working JSFiddle demo.

Since you have defined a jquery tag, I have used this. Of course, it can be easily done in vanilla JS.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • This doesn't work in OSX, where ctrl-click [opens a context menu](http://stackoverflow.com/questions/14725824/any-way-to-detect-ctrl-click-in-javascript-for-osx-browsers-no-jquery) – joews Nov 03 '15 at 10:13
  • @joews The OP wants it to work in Microsoft Edge browser, which is not available for OS X. – Yeldar Kurmangaliyev Nov 03 '15 at 10:15
  • SO isn't just for the OP - the comment will be useful for other people who come across this question. – joews Nov 03 '15 at 10:17
  • I tested both your code and your jsFiddle, both doesn´t work with the Edge browser (but it does work in Firefox). – Tream Nov 03 '15 at 12:03
  • @YeldarKurmangaliyev, no, the link is just opened in a new tab. Nothing else happens. – Tream Nov 03 '15 at 12:13