Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to disable right click using jQuery?
To disable right click on a page, use the jQuery bind() method. This prevents users from accessing the context menu that typically appears when right-clicking on web elements.
Example
You can try to run the following code to learn how to disable right click −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).bind("contextmenu", function(e){
return false;
});
});
</script>
</head>
<body>
<p>Right click is disabled on this page.</p>
<p>Try right-clicking anywhere on this page - the context menu will not appear.</p>
</body>
</html>
Alternative Method Using on()
You can also use the more modern on() method instead of bind() −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).on("contextmenu", function(e){
e.preventDefault();
});
});
</script>
</head>
<body>
<p>Right click is disabled using the on() method.</p>
<p>This approach uses preventDefault() instead of returning false.</p>
</body>
</html>
Both methods work by intercepting the contextmenu event and preventing its default behavior. The on() method is preferred in newer jQuery versions as bind() has been deprecated.
Advertisements
