How to disable right click on visualforce page?

Sometimes we need to disable right click on visualforce page. Here in this article i will demonstrate how to disable right click on visualforce page using javascript.

Let’s take a simple example:
The below visualforce page is a simple entry page of “Student” custom object. Here the javascript method “RightClickDisabled”, disabled right click in form onmousedown event.

<apex:page standardcontroller="Student__c">
  <script>
      function RightClickDisabled(event){    
        if (event.button==2)
        {
            alert("Right click is not allowed");       
        }
      }
  </script>
  <apex:form onmousedown="RightClickDisabled(event)">
      <apex:pageblock title="Create Student">
          <apex:pageblockbuttons>
              <apex:commandbutton action="{!save}" value="Save">
          </apex:commandbutton></apex:pageblockbuttons>
          <apex:pageblocksection columns="1">
              <apex:inputfield value="{!Student__c.First_Name__c}">
              <apex:inputfield value="{!Student__c.Last_Name__c}">
              <apex:inputfield value="{!Student__c.Date_of_Birth__c}">
              <apex:inputfield value="{!Student__c.Address__c}">              
          </apex:inputfield></apex:inputfield></apex:inputfield></apex:inputfield></apex:pageblocksection>
      </apex:pageblock>
  </apex:form>
</apex:page>

download

download (1)