How to disable right click on web page using JavaScript ?
Last Updated :
05 Mar, 2024
Disabling right-click on a web page can be done using the DOM Events. It is a client-side action, and it can be achieved using JavaScript.
However, it's important to note that attempting to prevent right-clicking is not a foolproof method for protecting your content, as users can easily bypass it by disabling JavaScript or using browser features.
To disable right click on web page in JavaScript we can use the methods given below:
HTML DOM addEventListener() Method
The addEventListener() Method attaches an event handler to the specified element.
Syntax:
document.addEventListener(event, function, useCapture)
JavaScript preventDefault() Event Method
The preventDefault() Method cancels the event if it can be canceled, meaning that it stops the default action that belongs to the event. For example- Clicking on a "Submit" button, prevent it from submitting a form.
Syntax:
event.preventDefault()
Parameters: It does not accept any parameter.
Example for disabling the Right Click on Web Page
The below examples uses the above mentioned method to disable the right click.
Example 1: This example disable the right click by adding an event listener for the "contextmenu" event and calling the preventDefault() method.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to disable right click on
web page using JavaScript ?
</title>
</head>
<body style="text-align: center;">
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
How to disable right click on
web page using JavaScript ?
</h3>
<button onclick="Fun_call()">
Disable Right Click
</button>
<h3 id="GFG" style="color:green;">
</h3>
<script>
let elm = document.getElementById("GFG");
function Fun_call() {
document.addEventListener('contextmenu',
event => event.preventDefault());
elm.innerHTML = "Right click disabled";
}
</script>
</body>
</html>
Output:

Example 2: This example disables the right-click on the image by adding an event listener for the "contextmenu" event and calling the preventDefault() method. Because, Sometimes we do not want the user to save the image.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to disable right click on
web page using JavaScript ?
</title>
</head>
<body style="text-align: center;">
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
How to disable right click on
web page using JavaScript ?
</h3>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190521140445/gfglogo2.png" />
<br><br>
<button onclick="Fun_call()">
Disable Right Click
</button>
<h3 id="GFG" style="color:green;">
</h3>
<script>
let elm = document.getElementById("GFG");
function Fun_call() {
document.addEventListener("contextmenu",
function (e) {
if (e.target.nodeName === "IMG") {
e.preventDefault();
}
}, false);
elm.innerHTML = "Right click disabled on image";
}
</script>
</body>
</html>
Output:
Similar Reads
How to disable right-click option using the jQuery ? The bind() method in jQuery is used to attach one or more event handlers for selected element and this method specifies a function to run when an event occurs. Syntax: $(selector).bind(event, data, function);Parameters: This method accepts three parameters as mentioned above and described below: eve
2 min read
How to Disable Textarea using JavaScript? This article will show you how to disable the textarea using JavaScript. Disabling a textarea using JavaScript can be done by setting the disabled property of the textarea element to true. This prevents the user from editing the content of the textarea. There are some reasons to use disabled textare
2 min read
How to Disable Scrolling Temporarily using JavaScript? Disabling scrolling temporarily using JavaScript means preventing users from scrolling a webpage during specific interactions, such as when a modal is open or a loading screen is displayed. Scrolling can be disabled using JavaScript using 2 methods:Table of ContentUsing window.onscroll functionSetti
2 min read
How to disable radio button using JavaScript ? Radio buttons let users select one option from many, commonly used in forms. Sometimes, based on a user's previous choice, certain options need to be disabled. For example, if a user selects "No" to playing games, we can disable related game options. Using JavaScript, about 65% of forms use such log
4 min read
Create a Disable Right Click, Copy, Cut & Paste using JavaScript In this article, we have created a simple user interface using HTML and CSS, where there is an input field on which Right-Click, Copy, Cut, and Paste counts are displayed. When the user tries to perform these operations, a warning or error message is displayed, and the counter value is incremented f
3 min read