In one of my previous posts, we saw how you could disable text selection on a web page that works based on CSS that ultimately prevents users from selecting any text from your webpage, but why should we do this?
You should do this to prevent people from stealing/copying your content. But there are many other ways to copy the content from a webpage, even if the section of text is disabled on your webpage.
So, alternately you can use a more robust javascript code that does not only disable the selection of text but also disables CTRL + U (can be used to see actual HTML code), CTRL+C (can be used to copy highlighted/selected text), and right-clicks (displays list of options to do something with the item such as view source, copy, paste, etc.) on your website/blog.
So now, let’s move to the tutorial of disabling CTRL+U, CTRL+C key, and right-click on your website/blog. In your blog’s HTML, search for the tag. Just above it, paste it below javascript code.
<script type='text/javascript'>
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 17)
isCtrl=true;
if((e.which == 85) || (e.which == 67) && isCtrl == true)
{
// alert(‘Keyboard shortcuts are cool!’);
return false;
}
}
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>
Now, save the change, and voila! You’ve successfully disabled these features from your webpage.