I found many web developers who wish to hide the source code of their website tried to disable the right mouse click event of the browser.
However, if you still want to disable the right click event for some reason, here are some code snippets that you can use.
Disabling the body’s context menu
All you have to do is to add the
oncontextmenu attribute into the
body tag with
return false value. This will disable the content menu or right click content menu of the browser.
<body oncontextmenu="return false"> ... </body>
Disabling right click with alert message
If you wanted to show a message to the user when trying to access the context menu, let’s bind a function into the mouse click event so when the user clicks the right mouse button, the function will run. Let’s handle the event to listen which button is clicked then run the alert message and block the event if the second button is detected.