Search code examples
javascripthtmlmouseright-click

How to disable mouse right click on a web page?


I want to disable mouse right click on an HTML page. I have a page where user has to enter the details. I don't want the user to see the menu thats get displayed with the mouse right click. Rather I want to display a custom menu. I know there are some plugins available to do that. But my requirement does not need any plugins.


Solution

  • It's unprofessional, anyway this will work with javascript enabled:

    document.oncontextmenu = document.body.oncontextmenu = function() {return false;}
    

    You may also want to show a message to the user before returning false.

    However I have to say that this should not be done generally because it limits users options without resolving the issue (in fact the context menu can be very easily enabled again.).

    The following article better explains why this should not be done and what other actions can be taken to solve common related issues: https://www.sitepoint.com/dont-disable-right-click/