In this article I will explain how to disable mouse right click in ASP.Net (ASPX) page using jQuery.
The script has to be copied in Master Page or Content Page and it will disable mouse right click in ASP.Net (ASPX) page using jQuery.
Explanation
The following script has to be copied in the Head section of the Master Page or ASP.Net (ASPX) page and it can also be placed within the ContentPlaceHolder of the Content Page.
The script disables the following three events in order to disable the Right Click in browsers.
OnMouseDown
The script first checks whether the document has layers, and if yes then the OnMouseDown event is captured and inside its event handler the event is cancelled by using return false.
OnMouseUp
And if the document does not have layers then inside the OnMouseUp event handler, first the Mouse Button that was clicked is determined and if the Button is Middle or Right then the event is cancelled using return false.
OnContextMenu
The OnContextMenu event is cancelled using the OnContextMenu event handler.
Disable Mouse Right Click in ASP.Net (ASPX) Page using jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
if (document.layers) {
//Capture the MouseDown event.
document.captureEvents(Event.MOUSEDOWN);
//Disable the OnMouseDown event handler.
$(document).mousedown(function () {
return false;
});
}
else {
//Disable the OnMouseUp event handler.
$(document).mouseup(function (e) {
if (e != null && e.type == "mouseup") {
//Check the Mouse Button which is clicked.
if (e.which == 2 || e.which == 3) {
//If the Button is middle or right then disable.
return false;
}
}
});
}
//Disable the Context Menu event.
$(document).contextmenu(function () {
return false;
});
</script>
Browser Compatibility
The above code has been tested in the following browsers.
* All browser logos displayed above are property of their respective owners.
Demo
Downloads