Sunday, 15 June 2014

Disable Right Click On Web Page Using Javascript









Description:-
In this Example I explain that how to Disable Right click or Prevent right click on your webpage or Document.

As a web developer we always want something different like disabling mouse button right clicks. Or disable cut copy facility  etc.. for some security purpose.

This is simply thing that disable right click on the webpage by simply add one line of code in <body> tag like
<body oncontextmenu="return false">
...
</body>
Or by using javascript like
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(event)
{
  if(event.button==2)
   {
     alert(status);
     return false;   
   }
}
</script>



But my problem is that I was faced in my life is that I want to disable the right click on a webpage if normal user is login in my website but if Admin is Login then we want to provide the Right click facility so there is different and some condition is apply on code are as shown below that are useful in your future if you have passed in this situation that I was passed already.
<body onload="call('<%= Session["userid"].ToString() %>');" style="margin: 0 0 0 0; height: 100%; width: 100%">

  <script type="text/javascript">

function call(id) {
           if(id != -1)
           document.oncontextmenu = new Function("return false")
       }
     
            </script>

Here in above code I was fetch the user id of the user that was login in your website in .Aspx  code and call the javascript Function in <body> tag onload event and in javascript function we will check that if id is equal to -1 then user is Admin user and we will provide right click facility but if id is not equal to -1 then user is normal user and we will disable the right click on a webpage.

MVC Registration Form with client and server side validation Validation in Registration Form in MVC4



Upload multiple file with progressbar Uploading multiple file at a time with progressbar

0 comments:

Post a Comment