Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable <a onclick> when the user holds control

Usually, when people click on a link, I have onclick bound to it. And then return false.

When people click with "control", they expect a new page to open up. Therefore, I want to ignore the onclick AND/OR detect it. How do I do this?

like image 383
TIMEX Avatar asked Dec 07 '25 15:12

TIMEX


1 Answers

The event object has a "ctrlKey" boolean flag, so you can check that in your handler. It depends a little on your framework, but generally if your handler returns false then you'll have "defeated" the click.

In IE, the event object is a global (that is, a property of the "window" object). In other browsers, it's a parameter passed to the handler. A common idiom therefore is:

function clickHandler(theEvent) {
  theEvent = theEvent || window.event;
  // ...
}
like image 183
Pointy Avatar answered Dec 10 '25 04:12

Pointy