Thursday, December 22

jQuery - Disable right mouse click on the page

In this article we will explore how to disable right click of mouse button when javascript is enabled. We will also find out the way to inform user whether javascript is enabled or disabled. If javascript is disabled right click will work otherwise it won't work.

Let's write some code to achieve it.

Step 1: Include jquery in the head section of aspx page.
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>

Step 2: Place noscript tag along with the content in the body tag. noscript tag runs only when javascript is disabled.

<noscript>
      
<h1
>
            
Javascript is disabled

      </h1>
      
Right click will work, please enable javascript to disable right click.
</noscript>



Step 3: Place the below div inside the body tag. It will be hidden and will be displayed only if javascript is enabled.

<div id="bodyContent" style="display: none">
      
<h1
>
            
Javascript is enabled
      </h1
>
      
<br
/>
      
Right click is disabled.
</div>


Step 4: Place below javascript to display above div and disable right click of mouse.

<script language="javascript">
      
$(document).ready(function
() {
            $("#bodyContent").css('display', 'block'
);
            $(document).bind('contextmenu', function
(e) {
                  return false
;
            });
      });
</script>
Live Demo Like us if you find this post useful. Thanks! 
Shibashish mohanty
 

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty