Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a cross browser way to prevent cut, copy and paste on a website in plain Javascript?

Tags:

javascript

I found an answer, but it was for JQuery. Here is the link:

http://jquerybyexample.blogspot.com/2010/12/disable-cut-copy-and-paste-function-for.html

: I want something in plain Javascript which work on chrome, latest firefox, safari, and IE 8 and 9.

Update

Due to all the negative comments saying that this is a bad idea for an internet site I can only say "I agree". Please note that this is for an "intranet" application where cut, copy, and paste need to be overidden as the default browser behaviour for cut copy and paste needs to be customized to handle embedded tags in a rich text area

like image 763
yazz.com Avatar asked Nov 30 '12 14:11

yazz.com


People also ask

How to disable cut and copy and paste in HTML?

You can disable cut, copy, and paste using the oncut, oncopy, and onpaste event attributes with the target HTML elements. If you want to disable cut, copy, and paste for the complete web page, you need to use these event attributes with the body tag. You can also disable drag and drop using ondrag and ondrop event attributes.

How to prevent text selection and copy/cut on your website?

Apply the onmousedown and onselectstart Events to the <body> or <div> tags to prevent text selection and copy/cut on your website. It override the default behavior of the browsers.

How to disable cut and copy in a textbox using jQuery?

By adding these attributes into a textbox’s <input> tag, you can disable cut, copy and paste features. The user is left with the option to enter the field manually with these attributes set. The same effect can be achieved by using the jQuery bind () function specifying cut and copy events that are fired when the user cuts or copies a text.

How do I enable copy and paste in jQuery?

In the bind () function, you have to specify the cut, copy, and paste events that are fired when a user tries to cut, copy, or paste anything on the web page. Make sure to embed the script tag in the head section to load jQuery before using it. Cut, Copy, and Paste is disabled for the complete web page.


1 Answers

a good way

var D=document.getElementById('b4');

if(D.addEventListener){
D.addEventListener('paste',function(e){false;e.preventDefault();},false);}
else{
D.attachEvent('onpaste',function(){return false;});}

warning : code must be under html target/s , just before the close body tag for example

like image 170
bubu Avatar answered Sep 27 '22 02:09

bubu