How to disable right-clicking on a website using JavaScript?

To disable right-clicking on a website using JavaScript, you can use the contextmenu event to cancel the default behavior of the right-click.

The contextmenu event is a DOM event that is triggered when a user right-clicks on an element in a user interface. It is a type of mouse event, similar to the click event, but it is specific to right-clicks.

In JavaScript, you can use the addEventListener method to attach a contextmenu event listener to an element. The event listener function will be called whenever the contextmenu event is triggered on the element.

Here's an example of how you might use the contextmenu event in JavaScript:

const element = document.getElementById("my-element"); 
element.addEventListener("contextmenu", (event) => {
    // do something when the context menu is shown
});

In the event listener function, the event object contains information about the contextmenu event, such as the position of the mouse and the element that was clicked. You can use this information to customize the behavior of the context menu or to perform other actions.

For example, you might use the event.preventDefault method to cancel the default behavior of the right-click and to show a custom context menu instead:

const element = document.getElementById("my-element"); 
element.addEventListener("contextmenu", (event) => {
    event.preventDefault();
    // show a custom context menu
});

Steps

To disable the right-click context menu on a webpage using JavaScript, you can follow these steps:

  • Step 1 ? Create an HTML page with a script element.

  • Step 2 ? In the script element, use the addEventListener method to attach an event listener function to the contextmenu event.

  • Step 3 ? In the event listener function, use the preventDefault method to cancel the default action of the event.

Using addEventListener Method

In this example, we disable right-click using the "contextmenu" event.

<!DOCTYPE html>
<html>
<head>
    <title>Disable Right Click</title>
</head>
<body>
    <h2>Disabling right-clicking on a webpage using JavaScript</h2>
    <p>Right Click is disabled</p>
    <script>
        document.addEventListener("contextmenu", (event) => {
            event.preventDefault();
        });
    </script>
</body>
</html>

In this example, the JavaScript code uses the addEventListener method to attach a contextmenu event listener to the document object. The event listener function cancels the default behavior of the right-click by calling the preventDefault method of the event object. This will disable right-clicking on the page and prevent the context menu from appearing.

Using oncontextmenu Attribute

You can use the oncontextmenu attribute to attach a JavaScript function to an element that will be called when the contextmenu event is triggered. This can be a convenient way to handle the contextmenu event if you only need to cancel the default behavior of the right-click and do not need to perform any other actions.

Here's an example of how you might use the oncontextmenu attribute:

<!DOCTYPE html>
<html>
<head>
    <title>Disable Right Click with Attribute</title>
</head>
<body>
    <h2>Disabling right-clicking on a webpage</h2>
    <div oncontextmenu="return false;">
        Right-clicking on this element is disabled.
    </div>
</body>
</html>

In this example, the oncontextmenu attribute is used to attach a JavaScript function to the div element that returns false when the contextmenu event is triggered. This will cancel the default behavior of the right-click and prevent the context menu from appearing.

Disabling for Specific Elements

You can also disable right-clicking for specific elements instead of the entire document:

<!DOCTYPE html>
<html>
<head>
    <title>Disable Right Click on Specific Elements</title>
</head>
<body>
    <h2>Right Click Protection</h2>
    <div id="protected">This div is protected from right-clicking</div>
    <div>This div allows right-clicking</div>
    
    <script>
        const protectedElement = document.getElementById("protected");
        protectedElement.addEventListener("contextmenu", (event) => {
            event.preventDefault();
            alert("Right-clicking is disabled on this element!");
        });
    </script>
</body>
</html>

Comparison

Method Scope Use Case
document.addEventListener Entire document Global right-click protection
oncontextmenu attribute Specific element Simple inline protection
element.addEventListener Specific element Targeted protection with custom actions

Important Considerations

Note: This approach will only work in browsers that support the contextmenu event, and it will not prevent users from accessing the context menu using other methods, such as using keyboard shortcuts or using the context menu button on a touch screen device. It is also important to consider the usability implications of disabling right-clicking, as it can be frustrating for users who rely on this feature for tasks such as copying and pasting text or saving images. You should only use this feature if it is necessary for your specific use case.

Conclusion

Disabling right-click can be achieved using the contextmenu event with either addEventListener or the oncontextmenu attribute. However, remember that this is not a foolproof security measure and should be used judiciously to avoid hampering user experience.

Updated on: 2026-03-15T23:19:00+05:30

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements