admin管理员组

文章数量:1389803

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
});

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
});
Share Improve this question edited Dec 3, 2012 at 20:45 codepuppy asked Dec 3, 2012 at 20:08 codepuppycodepuppy 1,1402 gold badges15 silver badges25 bronze badges 6
  • 1 Don't do it. It's annoying and your users will A) never visit your site again or B) bypass it via script blockers and/or manually. – jbabey Commented Dec 3, 2012 at 20:11
  • This is an exact duplicate. stackoverflow./questions/706655/… – Andrew Hubbs Commented Dec 3, 2012 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 Commented Dec 3, 2012 at 20:14
  • @Andrew - the thing about your referenced question, is that the accepted answer is wrong. contextmenu is now supported in jQuery. – ahren Commented Dec 3, 2012 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 Commented Dec 3, 2012 at 21:22
 |  Show 1 more ment

1 Answer 1

Reset to default 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/V3sWc/

本文标签: javascriptSuppressing a Browsers Default Right Click MenurefinementStack Overflow