December 23, 2015

How to disable mouse right click on your webpage


Though this used to be a hot subject of discussion and received a lot of search queries early when Javascript came on the scene. I still consider it helpful and useful depending on what you intend to do at the moment. If you've worked on an Online Quiz project before, maybe your client must have raised something in this direction. Disabling candidates from trying to right click to maybe copy something on the webpage. Similarly, someone maybe a visitor to your site wants to copy your source code by trying to right click to inspect element or know the path of your file directory on your hosting server. As I pointed it out early, it all depends on what you intend to do at the moment. For the time being, let's see what we can use Javascript to achieve in this respect.

This Javascript can be on an external file or be embedded on the same page. The choice is yours. Paste this code your Javascriot section and make the necessary changes needed.
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(event)
{
  if(event.button==2)
   {
     alert(status);
     return false;    
   }
}
</script>

In addition to that code above, this should be pasted on the page you intend to disable mouse right click.
<body oncontextmenu="return false">
...
</body>

With this you are good to go.

0 comments:

Post a Comment