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.

I don’t see any good reason of doing so and I don’t even recommend this to anybody. You can’t hide your JavaScript, CSS, or HTML code by just disabling the right click event. Instead, you can minify your code to make it hard for others to copy and understand your code.

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.

<script language="javascript">
	document.onmousedown=disableRightClick;

	function disableRightClick(event)
	{
	  if(event.button==2)
	   {
		 alert('Right Click Disabled');
		 return false;
	   }
	}
</script>