3

Ok so as far as I understand it so far we have 2 approaches

Firstly some javascript which I can employ within a given script. But this switches right click off for everything.

window.oncontextmenu = function() {
        return false;
};

or in the html can code

<body oncontextmenu="return false;">

but I cannot thus far find anywhere that will give me javascript or jquery solution where I can apply this to a given selector. Is this simply not possible or am I mis-understanding something.

I am finding that on a single right click my submenu appears immediately followed by the browser's default menu. The only way I have found to suppress this is setting oncontextmenu to false. Is there a more refined solution?

Further Note to accepted answer

Also applied successfully on a dynamic menu using delegate:

$(document).on("contextmenu", "#existing_Flavours .field_Input_Left.flavour", function(){
        return false;   // suppress browsers default right click menu
});
codepuppy
  • 1,130
  • 2
  • 15
  • 25
  • 1
    [Don't do it](http://www.sitepoint.com/dont-disable-right-click/). It's annoying and your users will A) never visit your site again or B) bypass it via script blockers and/or manually. – jbabey Dec 03 '12 at 20:11
  • This is an exact duplicate. http://stackoverflow.com/questions/706655/bind-event-to-right-mouse-click – Andrew Hubbs Dec 03 '12 at 20:12
  • Well I can't say that I fully understand why that should be. But I am sure you are right. But surely there must be a way to stop the default menu from appearing where right click is being used. @jbabey – codepuppy Dec 03 '12 at 20:14
  • @Andrew - the thing about your referenced question, is that the accepted answer is *wrong*. `contextmenu` is now supported in jQuery. – ahren Dec 03 '12 at 20:15
  • @ahren They are both right and both answers are in that question, which is the exact same question as this one. – Andrew Hubbs Dec 03 '12 at 21:22
  • @Andrew - the first line of the accepted answer reads: *"There is no built-in oncontextmenu event handler in jQuery"* - This is wrong. – ahren Dec 03 '12 at 21:24

1 Answers1

7
$('#mySelector').on('contextmenu', function(){
  return false;
});

Just bind it like you would any other event...

However, if you're wanting to do this to "protect" content, it's very little security. It's easy to bypass.

Demo: http://jsfiddle.net/V3sWc/

ahren
  • 16,803
  • 5
  • 50
  • 70
  • No no I don't want to protect or prevent anyone from looking at the code, I just want to stop my cancellation menu which is off the right click from being covered up by the default menu. As @jbabey is casting doubt on this am I able to assume that using it in a jquery context won't cause his script blockers from being in a huff. By the way this is exactly what I wanted to find out so many thanks. I am just going to test it. – codepuppy Dec 03 '12 at 20:21
  • Ok my selected elements don't display the default right click menu everywhere else on the page does. Absolutely perfect. – codepuppy Dec 03 '12 at 20:28