How to Prevent/Disable Context Menu (Right-Click Menu) on Web Pages

Hi folks,

You might want to prevent users from opening Context menu (aka Right-Click menu) by disabling it on your pages. In this post we are going achieve that feature using very simple one-line of JavaScript.

Code

You can place this code anywhere in the page. You can also put it on a external JavaScript file.

Explanation of the code

In the above JavaScript, ‘oncontextmenu‘ is a event and it is called if context menu or right click menu is opened on a particular element. Here, the element is entire window. If a right clicking on anywhere within window happen, a standalone function will be called which returns false. Returning false means the function prevents the default action by browser when that event occurs. The default action is the context menu which will be blocked by the function.

If you have question on Disabling Context menu / Right click menu, then please comment below. You can also visit JavaScript tutorials.

Thank you!