How to disable right-click context-menu in javascript

This question already has an answer here:

  • How do I disable right click on my web page?

    20 answers

  • Is there a close event for the browser contextmenu
  • jQuery event contextmenu triggers underlaying div
  • Is it possible to override the context-menu in a browser?
  • Disable the context menu in chrome apps?
  • Context menu becomes unclickable above HTML video layer
  • How to get id like “invokedOn” text by a right click event with Bootstrap ContextMenu?
  • jQuery Right-Click Context Menu Help!
  • Creating dynamic context menu in Chrome Extension is failing
  • Capturing the newly created node with JSTree contextmenu
  • Right mouse click detection on SVG shape in JavaScript not working
  • Anchor tag event not firing
  • Enabling jQuery contextMenu item on ajax request
  • 6 Solutions collect form web for “How to disable right-click context-menu in javascript”

    Capture the onContextMenu event, and return false in the event handler.

    You can also capture the click event and check which mouse button fired the event with event.button, in some browsers anyway.

    If you don’t care about alerting the user with a message every time they try to right click, try adding this to your body tag

    <body oncontextmenu="return false;">
    

    This will block all access to the context menu (not just from the right mouse button but from the keyboard as well)

    However, there really is no point adding a right click disabler. Anyone with basic browser knowledge can view the source and extract the information they need.

    I have used this:

    document.onkeydown = keyboardDown;
    document.onkeyup = keyboardUp;
    document.oncontextmenu = function(e){
     var evt = new Object({keyCode:93});
     stopEvent(e);
     keyboardUp(evt);
    }
    function stopEvent(event){
     if(event.preventDefault != undefined)
      event.preventDefault();
     if(event.stopPropagation != undefined)
      event.stopPropagation();
    }
    function keyboardDown(e){
     ...
    }
    function keyboardUp(e){
     ...
    }
    

    Then I catch e.keyCode property in those two last functions – if e.keyCode == 93, I know that the user either released the right mouse button or pressed/released the Context Menu key.

    Hope it helps.

    Dark side-note – I’ve never seen a right-click script that would work on Opera, even if Opera is set to allow right-click intercepting (which is by default off).

    If your page really relies on the fact that people won’t be able to see that menu, you should know that modern browsers (for example Firefox) let the user decide if he really wants to disable it or not. So you have no guarantee at all that the menu would be really disabled.

    You can’t rely on context menus because the user can deactivate it. Most websites want to use the feature to annoy the visitor.