In this article I will explain with an example, how to disable / restrict / prevent mouse right click in web page using
JavaScript and
jQuery.
Disabling Mouse Right Click in HTML using JavaScript
Inside the oncontextmenu event handler, the event is cancelled by returning FALSE.
<script type="text/javascript">
//Disable the Context Menu event.
document.oncontextmenu = function () {
return false;
};
</script>
Disabling Mouse Right Click in HTML using jQuery
Inside the HTML, following script file is inherited.
1. jquery.min.js
Inside the contextmenu event handler, the event is cancelled by returning FALSE.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
//Disable the Context Menu event.
$(document).contextmenu(function () {
return false;
});
</script>
Disabling Mouse Right Click in HTML using JavaScript and jQuery
You will need to place the following script in HEAD section of your HTML page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
//Using JavaScript
<script type="text/javascript">
//Disable the Context Menu event.
document.oncontextmenu = function () {
return false;
};
</script>
//Using jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
//Disable the Context Menu event.
$(document).contextmenu(function () {
return false;
});
</script>
</head>
<body>
<form id="form1" runat="server">
<h1>Try to Right Click on this Page.</h1>
</form>
</body>
</html>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads