Disable Copying on a website

30,743

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

Share:
30,743
Phillip Senn
Author by

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, 2020

Comments

  • Phillip Senn
    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:

    1. Disable Ctrl + c
    2. Disable Menu Edit Copy.
  • Paul
    Paul over 12 years
    Everything can be defeated. Disable JavaScript in the browser or tweak it in Firebug comes to mind.
  • Phillip Senn
    Phillip Senn over 12 years
    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?
  • Phillip Senn
    Phillip Senn over 12 years
    I like this. Q: Why does mouseDown not include e as a parameter?
  • Phillip Senn
    Phillip Senn over 12 years
    Yeah, this is good. It's constantly clearing the clipboard. I didn't think about that - I was thinking on the front end.
  • Phillip Senn
    Phillip Senn over 12 years
    Can setInterval("cldata();", 1000); be rewritten without the quotes?
  • Phillip Senn
    Phillip Senn over 12 years
    Why do a clipboardData.clearData() in one place and a clipboardData.setData('text','') in another?
  • Charming Prince
    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
    Mark Robbins over 12 years
    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;heig‌​ht:10000px;z-index:9‌​999"] -- 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
    Phillip Senn almost 11 years
    I didn't realize that I hadn't accepted any of the previous answers. I'll have to try your solution out @Amit.
  • WendiT
    WendiT over 8 years
    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.
  • uhfocuz
    uhfocuz over 7 years
    Anything web can be inspected.