Theme images by Storman. Powered by Blogger.

Saturday 8 October 2016

Disable cut(CTRL+X), copy(CTRL+C), paste(CTRL+V) and mouse right click in web page

In this article, I am going to share you how you can disable right-click and cut/copy/paste keyboard option using JavaScript and HTML on your web page.

Before starting anything I am going to give you brief insight about the usage of  disabling right-click and cut/copy option on the web page. So these are few cases.

1. Prevent user to view the source code.
2. Prevent user to copy content.
3. Prevent user to save images or any assets from the web page.

These are the methods you can use to achieve it.

METHOD 1 :  Disable right click and cut, copy, paste using HTML.

Set the oncontextmenu, oncopy, oncut, onpaste to return false in body tag. Your code will be look like this :

<body oncontextmenu = "return false" oncopy="return false" oncut="return false" onpaste="return false">

METHOD 2 :  Disable right click and cut, copy, paste using JavaScript.

To disable right click using JavaScript you can bind function that performs the check for the given input on mouse down Event. Similarly, you can use oncopy, oncut and onpaste event and bind the function to it.
Here is the code snippet :

<script type="text/javascript">

//Disable cut copy paste
document.oncopy = function(){alert("copy option disabled"); return false;}
document.oncut = function(){alert("cut option disabled");return false;}
document.onpaste = function(){alert("paste option disabled");return false;}

//Disable mouse right click
document.onmousedown = disableclick;
msg = "Right Click Disabled";
function disableclick(e)
{
     if(event.button==2)
     {
     alert(msg);
     return false;
   }
}
</script>

METHOD 3 : Disable right click and cut, copy, paste using Jquery.

To do same with  jQuery first you should download jquery file and attach to your html file as given below:
<script src="jquery.min.js"></script>

Next write your Jquery code as given below:

<script type="text/javascript">
$(document).ready(function () {

    //Disable mouse right click
    $("body").on("contextmenu",function(e){
        return false;
    });

    //Disable cut copy paste
    $('body').bind('cut copy paste', function (e) {
        e.preventDefault();
    });

});
</script>

Disabling right-click and cut/copy/paste option will not give you the guarantee to secure your data from any action but it will make things difficult for the novice.




2 on: "Disable cut(CTRL+X), copy(CTRL+C), paste(CTRL+V) and mouse right click in web page"
  1. Thank you for these tips! I own a publisher enterprise and I want to establish an online-books-system in which the members can read a lot of books online with a small monthly fee (about U$ 3), but cannot copy-paste the books. The audio-books will be provided by Vimeo system, but now I also can have the books in text. Thank you!!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete