5

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).

CHRIS
  • 957
  • 3
  • 10
  • 27

3 Answers3

2

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)

Community
  • 1
  • 1
dainichi
  • 103
  • 1
  • 10
  • 1
    Sadly it seems that options will be leaving in Firefox 23. – NoBugs Jul 06 '13 at 06:03
  • @NoBugs Do you have a reference for that? Thanks! – mikemaccana Jul 19 '13 at 13:37
  • @nailer Look in content options in Firefox beta, there is no Javascript option. https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/23 – NoBugs Jul 20 '13 at 01:07
  • 1
    @nobugs Ah, I thought you meant onContextMenu() was being disabled. Instead the 'Disable JavaScript' option is being moved away from users. So we can keep using onContextMenu() where we need it. – mikemaccana Jul 22 '13 at 09:55
  • Working with Firefox 86: none of these solutions even disable basic shift-rightclick. I've given up on trying to disable it with JS. Is there a FF flag to disable it? @dainichi, you suggested there is / was a Firefox setting? Not finding it.. – Codesmith Mar 06 '21 at 18:55
2

There is actually example in official documentation that blocks directly context menu event:

document.oncontextmenu = function () { // Use document as opposed to window for IE8 compatibility
  return false;
};

window.addEventListener('contextmenu', function (e) { // Not compatible with IE < 9
  e.preventDefault();
}, false);
icl7126
  • 5,740
  • 4
  • 53
  • 51
1
document.ondblclick = function(e) { 
    if(e.button == 2 || e.button == 3) {
        e.preventDefault();
        e.stopPropagation();
        return(false);
    }
};
Dan
  • 59,490
  • 13
  • 101
  • 110
spksa
  • 11
  • 1