Click here to Skip to main content
Licence 
First Posted 5 Aug 2005
Views 76,649
Bookmarked 21 times

Disabling the right click on web page

By | 12 Aug 2005 | Article
Article shows two methods to disable context menu on right click on web page first method can be found on majority of web pages, but second method which cannot be found anywhere is simplest

Download demo project - 2.57 Kb

Introduction

Want to protect your source code? Using this code will prevent the vast majority of users from right-clicking over a page and choosing "View source", or right-clicking over an image and saving it.

Some times there might be the requirement to disable the pop up menu on click of right button.

 

Programmers may need to disable right click to prevent user from saving the images on the page or viewing the source of the page.

 

Though disabling right click is not complete solution to save the data, but it will make task difficult, or may be impossible for the rookies.

 

I searched on a net for the solution to this problem and i got following solution.

Method 1

      In this method we add a javascript method, in this we check if click is right click or left click if it is right click then a message is displayed like "Right click disabled"

 

<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(e)
{
  if(event.button==2)
   {
     alert(status);
     return false;	
   }
}
</script>
 

But in this solution if you click the right click it displays the message

“Right click disabled”.

 

If we want to remove the messegebox then this solution do not work.

 

  I am having another simple solution to achieve the same result and also it does not show the messegebox.

Method 2

          In this method we set the oncontextmenu="return false" in the body part of the page

so body part will look like

<body oncontextmenu="return false">
...
</body>

So whenever user clicks the right mouse button nothing will happen, no message no context menu.

 

 Sometimes there might be requirement to disable the right click on specific control to achive this we have to follow these steps.

1.      remove added code in body (i.e. oncontextmenu=”return false”)

2.      Add one table and in its row add the control e.g. datagrid control

3.      put the oncontextmenu=”return false” in the <tr> for that control

 

So code will look like this

<Body>
  <Table>
   <tr oncontextmenu="return false">
    <td>
     <asp:datagrid id="dgGrid1">---</asp:datagrid>
   </td>
  </tr>
 </Table>
</Body>

So by this method context menu on the right click of right mouse click can be disabled.

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

RohitDighe

Web Developer

India India

Member

Rohit Dighe
B.E. Computer from University of Pune, India.
 
Working on Asp.Net and C# from 2 years

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembershaminders23:07 7 Apr '12  
GeneralThis helped me. Thanks PinmemberNeCroFire2:56 22 Nov '10  
Generaluseless trick Pinmemberanojkumar18:47 1 Jul '08  
QuestionHow to disable Copy/Past into a TextBox PinmemberSudipta(Bubun)20:22 28 Jun '07  
AnswerRe: How to disable Copy/Past into a TextBox Pinmemberanil 1kumar19:05 23 Oct '07  
GeneralOther ways to access the menu PinmemberPhilippe Mori8:14 13 Aug '05  
GeneralWaste of time PinsussAnonymous7:14 13 Aug '05  
GeneralWhy you shouldn't do this: PinmemberPhil Uribe12:18 12 Aug '05  
QuestionAre you serious?? PinmemberJoeySpimoley4:18 7 Aug '05  
GeneralDon't do this PinPopularmemberector10:40 5 Aug '05  
GeneralHTML trick... PinmemberPixiGreg2:59 5 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 12 Aug 2005
Article Copyright 2005 by RohitDighe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid