I am Hack Sparrow
Captain of the Internets.

Disabling the Context Menu using JavaScript

You want to disable the rightclick menu using JavaScript

So you would'd like to disable the default context menu that shows up when you rightclick on a webpage? Maybe you are in the process of creating the latest and the shiniest web app with its own custom context menu. Here's how to do it.

Disable context menu using JavaScript: the non-elegant way
Just add this piece of inline JavaScript code to the HTML body tag of your webpage.

<body oncontextmenu="return false">

Disable context menu using JavaScript: the elegant way
Include this snippet of JavaScript in your webpage.

document.oncontextmenu = function() {
    return false;

There you go! You now have disabled context menu, no more rightclicking. Time to create your own context menu, genius?


  • This is the technique of disbaling rightclick on a webpage
  • The technique will fail if the user disables JavaScript
  • The second way of disabling the contextmenu (the elegant way) is the recommended way to do it


  1. How do you create a custom context menu for a webpage?
  2. Why is the second way, the recommended way?


2 Responses to “Disabling the Context Menu using JavaScript”

  1. Aleksandra Nikolova says:

    Nice, if you want you can see also shieldui contextmenu widget

  2. Zeek says:

    Is there a similar way to reinstate the normal Context-menu?
    I just want it disabled for one (streaming-mode) text input field, when it has focus, and reinstate it when it loses focus.

Make a Comment