Thursday, 15 December 2011

Disable Right Click on your browser using JavaScript or Jquery or making onconextmenu false in body tag.

Sometimes you want to disable the right click on your browser to
protect your source code to be viewed by some one.

To do this you can use Javascript or JQuery:

JavaScript Code:  Put this code in between the Script tag as:
<script>

var message="Sorry, Right Click is disabled.";
   //To disable the right click on the page
    function click(e)
    {
        if (document.all)
         {
            if (event.button == 2)
            {
            alert(message);
            return false;
            }
         }
        if (document.layers)
         {
            if (e.which == 3)
            {
            alert(message);
            return false;
            }
         }
    }
    if (document.layers)
    {
        document.captureEvents(Event.MOUSEDOWN);
    }
    document.onmousedown=click;

</script>

JQuery Code:  Put this code in between the Script tag as:
<script>
   $(document).ready(function(){
    $(document).bind("contextmenu",function(e){
return false;
    });
});
</script>

OR
<script type="text/javascript>
document.oncontextmenu=new Function("return false");
</script> 

OR

<html>
<head>
<title>Disable Right Click using jQuery</title>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript">
$(function() {
     $(this).bind("contextmenu", function(e) {
         e.preventDefault();
     });
}); 
</script>
</head>
<body>
Disable Right Click event using jQuery
</body>
</html>

Using OnContextMenu in Body Tag: 
The Simplest one:
<body oncontextmenu="return false;">




 

No comments:

Post a Comment