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.

Example 1

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

<!DOCTYPE html>
<html>
<head>
   <title>Example</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.

Note that 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, suchas 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.

There are a few other ways you can disable right-clicking on a website using JavaScript:

Example 2

Using the 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>Example</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.

Updated on: 28-Dec-2022

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements