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

arp14 JavaScript, Tutorial Tags: , , 5

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.


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

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!

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

      1. 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.

  1. This is not working for Chrome version Version 57.0.2987.133 (64-bit)
    I am aware that it is to late to post the comment on this thread. But I have used many possible alternate to disable the right click . But everything seems to be failed . Can you tell me what could be the obstacles for not working the solutions given on internet

Leave a Reply

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