Saturday, December 8, 2012

Disable right click on web page and images

Sometimes it is required to prevent images on web pages being copied by another one. You can prevent the images by disabling the right click on your web page or by disabling image context menu on images only. Here I am going to share that how can you achieve this?

Disable right click on images
For disabling the image context menu on right click on images, just add the below event handler to the img or image tag as shown below


<asp:Image ID="Image1" runat="server" ImageUrl="~/pic.jpg" oncontextmenu="return false;"/>
<img src="pic.jpg" oncontextmenu="return false;" /> 

Disable right click on web page
Similarly we can disable the right click on web page by adding the oncontextmenu handler in body tag of the page. This will disable right click on each and every control of a webpage.

<html>
   <head>
      <title></title>
   </head>
   <body oncontextmenu="return false;">
    .........
   </body>
 </html>

Show alert message on right click

<script type="text/javascript">
        function disableRightClick() {
            alert("Sorry, right click is not allowed !!");
            return false;
        }
</script>

   <body oncontextmenu="return disableRightClick();" >
    .........
   </body>
Alert Message
I hope after reading this article you will be able to do this.
Happy coding!!
kick it on DotNetKicks.com

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo