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

<script type="text/javascript">
window.oncontextmenu = function () { return false; }
</script>

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!

Comments

      • says

        I feel there is no need of jQuery for this tiny thing. jQuery is a JavaScript library. The actual purpose of using jQuery is to “write less, do more”. Then, why to include jQuery library for this little work!
        BTW, your code is not a jQuery code, its plain JavaScript! The different is you’re preventing the document and I’m preventing the entire window. window is the root of everything and document is top DOM object.

Leave a Reply

Your email address will not be published. Required fields are marked *