How can we disable right click on website

Spread the love

Many times you want to disable right click on the website so that nobody can see the page source of your web pages. Also, you may want people should not download the images present on the page or copy some content from your webpage.


We can disable right click on the website using Javascript and also by using Jquery.
If you want to just disable the right click then we can achieve this using plain Javascript using following code.
<script language="javascript">
   document.onmousedown = disableRightclick;
   msg = "Right Click is Disabled";
   function disableRightclick(e)
   {
     if(event.button==2)
      {
        alert(msg);
        return false;  
      }
   }
</script>

Disable right click using Jquery

We can achieve same functionality to disable right click using Jquery. Just add a Jquery library to your page and insert below code of Jquery to disable right click on the website.

<script language="javascript" src="jquery-1.11.1.min.js"></script>

<script>
    $(document).ready(function(){
        $('body').bind('cut copy paste', function (e) {
            e.preventDefault();
        });
    });
</script>

Disable Cut, Copy, Paste along with disable right click on website using Jquery:

To disable cut, copy and paste along with disable right click on the website we can use following Jquery code.

<script language="javascript" src="jquery-1.11.1.min.js"></script>

<script>
$(document).ready(function(){
 
    // Disable cut, copy, paste
    $('body').bind('cut copy paste', function (e) {
        e.preventDefault();
    });
    //Right mouse button disable
    $("body").on("contextmenu",function(e){
        return false;
    });
});
</script>

Disable right click, cut, copy and paste of specific section or div on the webpage.

Let’s say if you want to disable right click, copy, cut and paste disabled for a specific section of a page, then give that section an ID and use that ID in below Jquery.

<h1>Disable Right Click</h1>
<div id="disableId">This text wont get copied.<div>
<script language="javascript" src="jquery-1.11.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function () {
        
 //Disable right click on section with "disableId" id.
    $("#disableId").on("contextmenu",function(e){
        return false;
    });
 
    //Disable copy cut paste on section with "disableId" id.
    $('#disableId').bind('copy cut paste', function (e) {
        e.preventDefault();
    });
});
</script>

Hope above solutions will help you to disable cut, copy, paste and disable right click on the website.


Spread the love

Leave a Comment