Kunal Vashist
Kunal Vashist

Reputation: 2471

Need to disable "context menu" on right click and call a function on right click

I have used oncontextmenu to call a function on mouse right-click, it is working fine on Firefox but causing problem on IE, context menu also appears along with the function.

I just need to call same function having some parameters on right and left click in pure JavaScript.

<html>
<script>
function mouseDown(e,param) {
  if (parseInt(navigator.appVersion)>3) {
    var clickType=1;
    if (navigator.appName=="Netscape") clickType=e.which;
    else clickType=event.button;
 
    if (clickType==1) {
      alert("left" +param);
    }

    if (clickType!=1) { 
      alert('right' + param);
    }
 }
 return true;
}

</script>
<body>
<a href="javascript:void(0)"
   onclick="mouseDown(event,'test1');"
   oncontextmenu="mouseDown(event,'test2');">mouse</a>
</body>
</html>

Upvotes: 0

Views: 2832

Answers (2)

hofnarwillie
hofnarwillie

Reputation: 3660

Try onmousedown instead of onclick as this might run before the IE context menu appears and then you could stop the default behaviour with jQuery .preventDefault()

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

You need to return false; in the contextmenu event in order to prevent the default menu from showing up.

Bear in mind that some browsers (Firefox in particular) defaults to not allowing JavaScript to block context menus, so you may encounter problems there.

Upvotes: 1

Related Questions