yazz.com
yazz.com

Reputation: 58816

Is there a cross browser way to prevent cut, copy and paste on a website in plain 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

Upvotes: 13

Views: 9511

Answers (5)

Omar Faruk
Omar Faruk

Reputation: 11

oncopy="return false" oncut="return false" onpaste="return false"

This code will be prevent cut, copy and paste of a website.

Working Snippet

Upvotes: 1

bubu
bubu

Reputation: 31

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

Upvotes: 3

technosaurus
technosaurus

Reputation: 7812

Edit: adding this to a body tag seems to work on all of my test browsers including the Opera, Chrome, Seamonkey (so I assume Firefox) and IE9

<body oncopy='return false' oncut='return false' onpaste='return false'>

you can put them in other tags if you want to allow some functions in some places and not in others

Upvotes: 2

Cerbrus
Cerbrus

Reputation: 72957

You can catch a [Ctrl]+[C] keypress:

addEventListener("keydown", function(e){
    evt = (e) ? e : window.event; // Some cross-browser compatibility.
    if(evt.ctrlKey && evt.which == 67){ // [x] == 88; [c] == 67; [v] == 86;
        console.log("Ctrl+C pressed!");
        evt.preventDefault(); // Cancel the copy-ing function for the client.
        // Manual Copy / Paste / Cut code here.
    }
});​

Working snippet

Upvotes: 1

hereandnow78
hereandnow78

Reputation: 14434

of course it is not appropriate to do stuff like this, but that was not @Zubairs question, so i think voting down is not correct here, as he made his point clear.

now to the question: if jQuery can do it, native javascript can do it of course too.

you must prevent the cut, copy and paste events:

document.body.oncopy = function() { return false; }
document.body.oncut = function() { return false; }
document.body.onpaste = function() { return false; }

this prevents the right-click-context-menu, this is not needed if you use the 3 other event-handlers but just to let you know ;-)

document.body.oncontextmenu = function() { return false; }

IMPORTANT: the body must be loaded (of course), document.body because IE needs it (document.oncopy will only work in chrome/firefox/safari)

Upvotes: 16

Related Questions