最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Disable Firefox's silly right click context menu - Stack Overflow

matteradmin2PV0评论

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

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

Share Improve this question asked May 4, 2013 at 18:05 CHRISCHRIS 9573 gold badges10 silver badges28 bronze badges 2
  • cancel the event in an onmousedown – dandavis Commented May 4, 2013 at 18:33
  • Already tried that as well. – CHRIS Commented May 4, 2013 at 18:39
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

window.addEventListener('contextmenu', function (e) { // Not patible with IE < 9
  e.preventDefault();
}, false);

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 ment on this other SO question: How to disable right-click context-menu in javascript)

document.ondblclick = function(e) { 
    if(e.button == 2 || e.button == 3) {
        e.preventDefault();
        e.stopPropagation();
        return(false);
    }
};
Post a comment

comment list (0)

  1. No comments so far