Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable right-click menu in chrome [duplicate]

I'm writing aWebGL game and want to use right-click as a control. However, it throws up a menu. Is it possible to disable that? I've tried

}  else if (event.which == 2 || event.which == 3)  {     doRightClickControl();     event.preventDefault();     event.stopPropagation();      return false; } 

Thanks dknaack for the hint. I got it to work like this:

window.oncontextmenu = function(event) {     event.preventDefault();     event.stopPropagation();     return false; }; 
like image 908
Chris Avatar asked Jul 22 '11 12:07

Chris


People also ask

How do I change right click Settings in Chrome?

Right-click your Google Chrome shortcut and click Properties. Note: You have to put a space between chrome.exe before the code. It's two hyphens, and not a long em dash (–). Click OK, launch Chrome, and you'll see the right-click context menu is back to normal.

How do I disable right click disabled?

Method 3: Using JavaScript Code Another easy way to enable the right-click menu on any webpage is by using a simple code snippet. For that, navigate to the target webpage and copy + paste the following line of code in the address bar: javascript:void(document. oncontextmenu=null); and hit Enter.


2 Answers

Using jQuery for this purpose only is overkill. This will do the trick:

(function () {   var blockContextMenu, myElement;    blockContextMenu = function (evt) {     evt.preventDefault();   };    myElement = document.querySelector('#myElement');   myElement.addEventListener('contextmenu', blockContextMenu); })(); 

myElement can be replaced with window.

like image 107
bennedich Avatar answered Oct 18 '22 15:10

bennedich


Use this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <head> <title>disable rightclick menu - LabLogic</title> </head> <body> <script language="javascript" type="text/javascript">   document.oncontextmenu=RightMouseDown;   document.onmousedown = mouseDown;     function mouseDown(e) {       if (e.which==3) {//righClick       alert("Disabled - do whatever you like here..");    } } function RightMouseDown() { return false;} </script> </body> </html> 

Tested in chrome 17, works with other new browsers as well

like image 36
LabLogic Avatar answered Oct 18 '22 16:10

LabLogic