Disable Copying on a website
Solution 1
Selecting text, copy, the right click can be disabled on a web page easily using jQuery. Below is the simple jQuery code snippet which can do this task easily:
<script type="text/javascript">
// Disable right click on web page
$("html").on("contextmenu",function(e){
return false;
});
// Disable cut, copy and paste on web page
$('html').bind('cut copy paste', function (e) {
e.preventDefault();
});
</script>
Source: Disable right click, copy, cut on web page using jQuery
Solution 2
Its some how daunting to create a function that would do that, what you should target is, clearing the clipboard so even if, the user press Ctrl + C, nothing is copied into the clipboard, a simple function like this should do the trick :
<script language="javascript">
function clearData(){
window.clipboardData.setData('text','')
}
function cldata(){
if(clipboardData){
clipboardData.clearData();
}
}
setInterval("cldata();", 1000);
</script>
<body ondragstart="return false;" onselectstart="return false;" oncontextmenu="return false;" onload="clearData();" onblur="clearData();">
although this can still be defeated....
Solution 3
Just add the following code right before closing </HEAD> tag of your web page:
<script>
function killCopy(e){
return false;
}
function reEnable(){
return true;
}
document.onselectstart=new Function ("return false");
if (window.sidebar){
document.onmousedown=killCopy;
document.onclick=reEnable;
}
</script>
Solution 4
I would suggest you to use:
<div oncopy="return false;">Here you have protected text</div>
Support for this method could be found here: http://help.dottoro.com/ljwexqxl.php
It is simple and in my opinion sufficient against regular users. To be honest there is no option to fully prevent copying text. One can always use for example Chrome Developer Tools and copy even dynamically loaded text from there.
For more effective protection you should place oncopy
in <body>
tag because otherwise it is possible to copy text by starting selection from outer <div>
.
Solution 5
<script type="text/javascript" language="javascript">
$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
});
</script>
<script type="text/JavaScript">
function killCopy(e){ return false }
function reEnable(){ return true }
document.onselectstart=new Function ("return false");
if (window.sidebar)
{ document.onmousedown=killCopy;
document.onclick=reEnable; }
</script>
//By using above code you right click will be disabled as well as no one can copy your page content
Phillip Senn
Developer in: Microsoft SQL Server, Adobe ColdFusion (and Lucee), HTML, CSS, JavaScript (with jQuery's help). Tools: Dreamweaver, Fireworks, Beyond Compare. Adjunct Instructor: Lenoir-Rhyne University. Twitter: @PhillipSenn
Updated on October 19, 2020Comments
-
Phillip Senn over 3 years
I know that it's impossible to thwart the world's most advanced minds, but I'd like to put the slightest of barriers on my website to keep my students from copying text from it and posting that text as their answer. (If they hand type it, that's ok).
I'm just so afraid of JavaScript because of cross browser inconsistencies.
Given that I have jQuery loaded and prefer to use jQuery whenever possible, how do I:
- Disable Ctrl + c
- Disable Menu Edit Copy.
-
Paul over 12 yearsEverything can be defeated. Disable JavaScript in the browser or tweak it in Firebug comes to mind.
-
Phillip Senn over 12 yearsI like both these ideas. I think I have the know-how to inject it with a jQuery ajax command, but what's a transparent div?
-
Phillip Senn over 12 yearsI like this. Q: Why does mouseDown not include e as a parameter?
-
Phillip Senn over 12 yearsYeah, this is good. It's constantly clearing the clipboard. I didn't think about that - I was thinking on the front end.
-
Phillip Senn over 12 yearsCan setInterval("cldata();", 1000); be rewritten without the quotes?
-
Phillip Senn over 12 yearsWhy do a clipboardData.clearData() in one place and a clipboardData.setData('text','') in another?
-
Charming Prince over 12 years@cf_PhillipSenn you have to call the function using setInterval continuously in a loop.in this case 1sec. without the quotes, it won't work.
-
Mark Robbins over 12 yearsTransparent div is just a div with no background and thats as big as it needs to be to cover what your want to protect -- it must have a high z-index to, so it can be on top [div style="position:absolute;left:0px;top:0px;width:10000px;height:10000px;z-index:9999"] -- in BODY would cover your whole page and make everything unclickable,etc -- you will want to create this dynamically to get the height and width right, otherwise your scrollbars will go huge.
-
Phillip Senn almost 11 yearsI didn't realize that I hadn't accepted any of the previous answers. I'll have to try your solution out @Amit.
-
WendiT over 8 yearsThis code doesn't work with the latest Firefox (43.0.4) and therefore it shouldn't be used. People cannot fill in a form, like a log in field.
-
uhfocuz over 7 yearsAnything web can be inspected.