OK, I know the title is kind of funny, but funny titles are increasing your curiosity, and the chance that you will read this article 🙂 However, every geek, that has a little knowledge about the web, know the we can not prevent that a user will copy our text or images from our website. We can only make the way of “stealing” a little bit difficult.
Ok, now from the beginning. Recently I got a project, exactly a webpage to develop. And there was a strange request. I should develop the page in this way, that the user can not easily copy the text and than used it for different purposes. I accepted the project, so the feature had to be implemented. And there is the solution I choose.
The folowing JavaScript code prevent to use the right-click and event to trigger the action of button combination: CTRL+A, CTRL+C, CTRL+V, CTRL+X.
//DISABLE COMBINATIONS LIKE CTRL+A, CTRL+C, CTRL+V
function disableCtrlKeyCombination(e){
//list all CTRL + key combinations you want to disable
var forbiddenKeys = new Array('a', 'c', 'x', 'v');
var key;
var isCtrl;
if(window.event){
key = window.event.keyCode; //IE
if(window.event.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}else{
key = e.which; //firefox
if(e.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}
//if ctrl is pressed check if other key is in forbidenKeys array
if(isCtrl){
for(i=0; i < forbiddenKeys.length; i++){
//case-insensitive comparation
if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase()){
return false;
}
}
}
return true;
}
document.onkeypress=function(e){return disableCtrlKeyCombination(e);}
document.onkeydown=function(e){return disableCtrlKeyCombination(e);}
//DISABLE RIGHT CLICK
document.oncontextmenu = function(){return false}
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown = function(e){
if(e.target==document)return false;
}