Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctrl+click on links with click handlers

I have a link (anchor) that has an href attached to it to navigate to a specific URL say 'www.bla.com'.

<a href='http://www.bla.com' />

I also have an click handler attached to the link that performs some actions and then opens an html view in the same window. Everything works perfectly well.

However, when the user uses 'ctrl+click' to open the link in a new tab/window, the click handler seems to be taking precedence and opens the html view in the same window. But I want to retain the 'ctrl+click' behavior and allow the user open the link in a new tab/window (just as a normal link). How could I do that?

Thanks in advance!

like image 973
Saket Avatar asked Nov 07 '12 03:11

Saket


2 Answers

function process(e){
   var evt = e ? e:window.event;
   if(evt.ctrlKey)
      alert("ctrlClicked");
}​

evt.ctrlKey will return true if control key is pressed, you can write your conditions within "if" block, I tested this for chrome and ff only.

like image 174
Sanjeevi.V Avatar answered Sep 19 '22 17:09

Sanjeevi.V


Perhaps something like this?

function onclick(e){
   var event = e ? e:window.event;
   this.target = event.ctrlKey?"_blank":"_self";
}​
like image 30
Shadow Avatar answered Sep 22 '22 17:09

Shadow