Link to home
Start Free TrialLog in
Avatar of gyra
gyra

asked on

CrossBrowser Ctrl+Click, regular Click call function detection...

Hey all, here's what i'm trying to do. I have a link, i want it so that when you click this link it calls a function, and if you control click on the link, i want a different function to be called. Can this be done? How? Thanx all.
Avatar of TName
TName

Well, I'd call one function, check there for ctrl and call one of 2 functions:

<html>
<head>
<script>

function ctrlYes() {  
  alert('Ctrl!');
}

function ctrlNo() {  
  alert('No Ctrl!');
}

function testCtrl(event) {  
    if (!event) event = window.event;
    if (event.ctrlKey == true) {
       ctrlYes();
     }
     else  {
        ctrlNo();
     }
}
</script>
</head>
<body>
<a href="#" onClick="testCtrl(event)" > Click this link </a>
</body>
</html>
Avatar of gyra

ASKER

okay, that seemed to work, but the only problem is this, in firefox and ie7, when i ctrlClick on the link, it opens up a new window for the link. How do i stop the window from opening up?
Ok, replace
<a href="#" onClick="testCtrl(event)" > Click this link </a>

with this:
<a href="javascript:void(0)" onClick="testCtrl(event)" > Click this link </a>

or with this:
<a href="#" onClick="testCtrl(event);return false;" > Click this link </a>
Avatar of gyra

ASKER

I tried both and it either solution works fine for IE, but for firefox it still opens a new window/tab.
ASKER CERTIFIED SOLUTION
Avatar of TName
TName

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gyra

ASKER

Wow, NICE!

Real cool, fake it out! Worked great! Thanx man a million

gyra