Disable Copying on a website
Asked Answered
C

11

8

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:

  1. Disable Ctrl + c
  2. Disable Menu Edit Copy.
Corbel answered 3/12, 2011 at 3:30 Comment(9)
You are wasting your time. As soon as one of them finds out how to bypass whatever measures you take, it will be all over.Perice
You could always use say php to put the text into an image and just display the image. But ocr could make decent work of it (even google docs has abilities of doing it). Best advice, either don't care (I like this one), or ask questions whose answers can't be copied.Comprehension
It's ok if they copy their answers as long as they retype the text? Talk about old school learning! Maybe you should make them turn in work on slates so they can't copy and paste. Just let them copy all they want - they'll pay the price at test time. Why waste your time to prevent something that ultimately hurts them?Existential
Jon: Hopefully the semester will be over before someone figures it out and the word gets out.Corbel
Blender: I use jQuery at every opportunity because I never know what's going to fail in FF,IE,Chrome,Safari, etc.Corbel
mazzzzz: That actually is something that I could possibly do.Corbel
Paul: I don't think it's old school. I'm trying to hold their nose into the book and forcing them to type something from the textbook is one way I can think of.Corbel
they can just print screen and put it on pdf. Pdf detects the writing in the image. Futile.Scarbrough
If they demonstrated the ability to print screen an dput it on a pdf only to have the pdf detect the writing, then I would give them full credit just for the mad skillz.Corbel
N
3

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

Nary answered 14/10, 2017 at 17:22 Comment(0)
G
13

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....

Genitor answered 3/12, 2011 at 3:38 Comment(6)
Everything can be defeated. Disable JavaScript in the browser or tweak it in Firebug comes to mind.Existential
Yeah, this is good. It's constantly clearing the clipboard. I didn't think about that - I was thinking on the front end.Corbel
Can setInterval("cldata();", 1000); be rewritten without the quotes?Corbel
Why do a clipboardData.clearData() in one place and a clipboardData.setData('text','') in another?Corbel
@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.Genitor
@CharmingPrince but you can do: setInterval(cldata, 1000)Allanite
N
6

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>
Nutriment answered 2/5, 2013 at 7:55 Comment(2)
I didn't realize that I hadn't accepted any of the previous answers. I'll have to try your solution out @Amit.Corbel
This 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.Downgrade
A
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>.

Anthroposophy answered 13/6, 2014 at 9:59 Comment(0)
S
3

If you have your texts in particular divs, you could put a transparent div on top of those divs. Secondly, you could make all your protected text dynamic, and inject it into the divs from javascript where is would exist in a coded form -- that would defeat a 'view-source'.

Spokeshave answered 3/12, 2011 at 3:39 Comment(3)
I 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?Corbel
Transparent 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.Spokeshave
Anything web can be inspected.Helbona
B
3
<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

Bearberry answered 6/3, 2014 at 7:12 Comment(0)
H
3

A simple and valid solution - bind to the 'copy' event and prevent it. You can also set what text will be copied (and later pasted by the user).

document.addEventListener('copy', function (e){
    e.preventDefault();
    e.clipboardData.setData("text/plain", "Do not copy this site's content!");
})
Hoehne answered 30/8, 2016 at 18:40 Comment(0)
N
3

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

Nary answered 14/10, 2017 at 17:22 Comment(0)
C
1

To achieve that you need to block mouse click and context menu click on your webpage.

Here is a sample code:

<script language="JavaScript1.2">
    var msgpopup="COPYING CONTENT IS PROHIBITED";
    function handle(){
          if(toShowMessage== "1") alert(message);
              if(closeSelf== "1") self.close();
              return false;
    }
    function mouseDown() {
         if (event.button == "2" || event.button == "3"){handle();}
    }
    function mouseUp(e) {
         //if (document.layers || (document.getElementById && !document.all)){
              if (e.which == "2" || e.which == "3"){ handle();}
         //}
    }
    document.onmousedown=mouseDown;
    document.onmouseup=mouseUp;
    document.oncontextmenu=new Function("alert(msgpopup);return false")
    </script>
Cracow answered 3/12, 2011 at 3:41 Comment(1)
I like this. Q: Why does mouseDown not include e as a parameter?Corbel
S
1

You can put the text in an input tag attributed as readonly and prevent the user from copy using JS. So, user cannot copy it even from developer menu.

Stingy answered 6/4, 2022 at 17:15 Comment(0)
A
0

If you're trying to achieve this with react, you can achieve this by:

useEffect(() => {
        const preventCopyPaste = (e: any) => {
            e.preventDefault();
        };

        document.documentElement.addEventListener("cut", preventCopyPaste);
        document.documentElement.addEventListener("copy", preventCopyPaste);
        document.documentElement.addEventListener("paste", preventCopyPaste);

        return () => {
            document.documentElement.removeEventListener(
                "cut",
                preventCopyPaste,
            );
            document.documentElement.removeEventListener(
                "copy",
                preventCopyPaste,
            );
            document.documentElement.removeEventListener(
                "paste",
                preventCopyPaste,
            );
        };
    }, []);
Anonym answered 17/4 at 10:18 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Chaing
E
0
jQuery('html').on('contextmenu cut copy', function() { return false; });
Eldwin answered 12/8 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.