DEV Community

Cover image for Disable the right-click context menu in JavaScript?
Manoj Kumar
Manoj Kumar

Posted on

Disable the right-click context menu in JavaScript?

The context menu, accessible by right-clicking on a webpage element, is a handy tool for users. It offers quick actions like copying text, saving images, or inspecting the page code. But have you ever wondered if it's possible to disable this menu?

Yeah, we can disable the context menu! Let's see how we can implement that on our webpages.

Disable for Whole Page

window.addEventListener("contextmenu", e => e.preventDefault());
Enter fullscreen mode Exit fullscreen mode

Disable using oncontextmenu

<body oncontextmenu="return false;">
   <div>
       Page Content
   </div>
</body>
Enter fullscreen mode Exit fullscreen mode

Disable for specific element without

const disableClick = document.getElementId("component");

disableClick.addEventListener("contextmenu", e => e.preventDefault());
Enter fullscreen mode Exit fullscreen mode

Limitations and Considerations

  • Ineffective Protection: Disabling the context menu doesn't truly safeguard your content. Savvy users can still access the source code or copy images using browser developer tools or extensions.

  • User Workarounds: Tech-savvy users can bypass these methods, rendering them ineffective.

  • Browser Safeguards: Modern browsers often have built-in features that prevent websites from completely disabling the context menu.

Top comments (0)