Skip to content

Disable right click in browser using JavaScript

In some scenarios, we need to disable the right click option in the web browser to avoid users from copying content from your page and for other security reasons.

When mouse button is clicked then the MouseEvent Button property returns an integer value which describes the left, right or middle mouse buttons.

The following code can be used to disable right click in the best possible way.

<script language ="javascript" >
  function disableRightClick() 
  {
     if(event.button == 2) 
      {
       alert("Right Click is disabled.")
      }
   }
      document.onmousedown=disableRightClick;
</script >

To disable left click, change it to if(event.button == 0)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.