CHRIS CHRIS - 4 months ago 35
HTML Question

Disable Firefox's silly right click context menu

I am making an HTML 5 game which requires the use of right click to control the player.

I have been able to disable the right click context menu by doing:

<body oncontextmenu="return(false);">

Then it came to my attention that if you hold shift and right click, a context menu still opens in Firefox!

So I disabled that by adding this JS as well:

document.onclick = function(e) { if(e.button == 2 || e.button == 3) { e.preventDefault(); e.stopPropagation(); return(false); } };

However, if you hold shift, and then double right click in Firefox it still opens!

Please tell me how to disable this bloody thing once and for all (I'm even willing to revert to some obscure, hacky, and unpractical solution, as long as it works).


You will never be able to entirely disable the context menu in all cases, as firefox has a setting that allows the user to tell the browser to ignore such hijinx as you are trying to pull. Note: I'm on a mac, but this setting is in pretty uch the same place over all platforms.

That being said, try event.preventDefault() (see Vikash Madhow's comment on this other SO question: How to disable right-click context-menu in javascript)