codepuppy
codepuppy

Reputation: 1140

Suppressing a Browsers Default Right Click Menu - refinement?

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

Upvotes: 3

Views: 4686

Answers (1)

ahren
ahren

Reputation: 16959

$('#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/

Upvotes: 7

Related Questions