PDA

View Full Version : How do you disable the contextmenu with DOM event handlers



Bob90
05-08-2007, 11:16 AM
Hello,

So How do you disable the contextmenu with DOM event handlers?

I used to use


document.oncontextmenu=function(){return false};
document.oncontextmenu='';

But what do I use with, for example


if(document.addEventListener)
{
document.addEventListener('contextmenu', returnFalse, false);
}
function returnFalse()
{
return false;
}

because it doesn't seem to work.

Thanks in advance


EDIT: And how do you enable the menu after you've disabled it?

:)

Twey
05-08-2007, 11:19 AM
if(document.addEventListener)
document.addEventListener('contextmenu', function(e) { e.stopPropagation(); });
else if(document.attachEvent)
document.attachEvent('oncontextmenu', function() { event.cancelBubble = true; });

Bob90
05-08-2007, 11:23 AM
blummin' eck that was fast!

OK thats fantastic, but how do I re-enable the menu?

:D

Bob90
05-08-2007, 11:32 AM
I can't get it to work.

Firebug report 'not enough arguments' and the menu still displays

Do you have any idea as the rest of the web seems short on them.

:)

Bob90
05-08-2007, 11:34 AM
Just needed to add the argument false to the addeventlistener to stop bubbling.


if(document.addEventListener)
document.addEventListener('contextmenu', function(e) { e.stopPropagation(); }, false);
else if(document.attachEvent)
document.attachEvent('oncontextmenu', function() { event.cancelBubble = true; });


I've sorted the removeEventListener by creating my own function to be called and removing that.



function disableContextMenu(e)
{
if(!e)
{
event.cancelBubble = true;
}
else
{
if(e.stopPropagation)
{
e.stopPropagation();
}
else
{
return false; //redundant as not called
}
}

}

mwinter
05-08-2007, 03:48 PM
Just needed to add the argument false to the addeventlistener to stop bubbling.

The third argument to the addEventListener and removeEventListener methods doesn't affect bubbling. It determines in which event phase the listener to be added or removed would be called. Passing true places the listener in the capturing phase, whilst passing false places the listener in the bubbling phase. In general, capturing should never be necessary.

To prevent bubbling, call the stopPropagation method, or assign true to the cancelBubble property.

To disable the default action for a particular event, call the preventDefault method, assign false to the returnValue property, or return false. The latter option is preferable to the second as they are equivalent, but the latter is compatible with more browsers.

Mike

jscheuer1
05-08-2007, 04:37 PM
None of this stuff (it is essentially 'no right click' dressed up in drag) should be relied upon for anything. It will work in many cases and it will not work in may others. Even (if the issue is copying things) where it 'works' users will find another way to get the information they want copied.

Something like this is only appropriate where it enhances the web experience for the user. If it is for any kind of 'protection', get a condom. :p

Bob90
05-08-2007, 05:32 PM
I feel I need to defend myself from the heresy that is 'Javascript Protection'.

I never intended it to be for protection, I hope that all regular coders would know javascript is a placebo. (In the sense that it may be called protection, but offers very little except self satisfaction for the newbie)

I am creating a radial menu for my site, which I want to code properly. At the moment it is a mess and I merely wanted a way to stop the menu appearing.

p.s. the condom joke is a winner.

:)

jscheuer1
05-09-2007, 03:46 AM
Sounds like an excellent use for this sort of code, except if you are trying to replace the context menu with your radial menu, that would be problematical as, many users would never get to see it, and others would see it overlaid by the default context menu for their browser.

Bob90
05-09-2007, 07:48 AM
Thanks for your concern, but as you can gather from my second post I only want to temporarily disable it.

As for the people with problems, I am working around this at the moment.

If you have any suggestions I would love to hear them (About working around the contextmenu).

:)

jscheuer1
05-09-2007, 02:08 PM
I'm not sure we understand each other. It is impractical, impossible to disable the context menu reliably. If it is done to enhance the web experience for only those folks for whom it will work, fine. This is rare but could happen if your layout and other scripts would just look nicer without the context menu, but could function even with it. But, if your code relies upon it, use different code, a completely different approach, something that doesn't rely upon disabling the context menu.

Twey
05-09-2007, 03:41 PM
He was asking how to work around the context menu: i.e. check if it can be disabled or not, and if not, not show the radial menu. At least, that's how I read the post.