30

In a HTML page user should not be allowed to copy a text, but at the same time I want to give option for the user to select a particular text (for highlighting purpose). That means CTRL+C should be disabled and CTRL+A should be enabled.

Can anyone tell me how to do this?

R.Subramanian
  • 341
  • 1
  • 3
  • 4
  • 2
    you can use CSS: ` -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; ` – Shawn Oct 25 '16 at 21:03

2 Answers2

70

You cannot prevent people from copying text from your page. If you are trying to satisfy a "requirement" this may work for you:

<body oncopy="return false" oncut="return false" onpaste="return false">

How to disable Ctrl C/V using javascript for both internet explorer and firefox browsers

A more advanced aproach:

How to detect Ctrl+V, Ctrl+C using JavaScript?

Edit: I just want to emphasise that disabling copy/paste is annoying, won't prevent copying and is 99% likely a bad idea.

Community
  • 1
  • 1
actual_kangaroo
  • 5,971
  • 2
  • 31
  • 45
  • 4
    _"annoying"_ Totally! I was trying to copy **public** information, and the copy function is disabled. It makes no sense. _"won't prevent copying"_ Yes, I just blanked out `oncopy` in Firefox Inspect Element to read `oncopy=""` and the Copy function worked fine. I was able to copy anything I wanted from the page :) – ADTC Feb 12 '18 at 16:51
  • 1
    @ADTC Just because information is displayed publicly doesn't mean you have the rights to it. This is the kind of misunderstanding that gives people the impression that they have right to copy-paste content from someone's website onto their own. – Hashim Aziz May 05 '23 at 17:38
  • 1
    @HashimAziz there was no talk about rights, or violation thereof. I am **not** encouraging plagiarism, which is copying someone else's work and passing it off as one's own. The legitimate reasons to copy what's on the web include 1. copying information inside my own account to be pasted in a private place for my own reference (badly designed bank and govt websites are notorious for disabling all right-click functions in the name of _false_ "security"), and 2. copying things to share with friends or acquaintances with adequate understanding that I didn't author what I'm sharing, not to profit. – ADTC May 06 '23 at 07:30
39

You can use jquery for this:

$('body').bind('copy paste',function(e) {
    e.preventDefault(); return false; 
});

Using jQuery bind() and specififying your desired eventTypes .

Vainglory07
  • 5,073
  • 10
  • 43
  • 77