Today we will discuss how to disable mouse right click using JavaScript in website or asp.net webpage. It’s a very simple script to disable right click event. You can disable the right click event in whole website by adding this script in master page.
<script type="text/javascript">
if (document.layers)
{
//Capture the MouseDown event.
document.captureEvents(Event.MOUSEDOWN);
//Disable the OnMouseDown event handler.
document.onmousedown = function ()
{
return false;
};
}
else {
//Disable the OnMouseUp event handler.
document.onmouseup = 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.oncontextmenu = function ()
{
return false;
};
</script>
|
Above 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.
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.
Summary
In this article I try to expose disabling mouse right click property. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.