3

I want to inactive selecting & copying text in html page. when I used Javascript & inactive right click user can use Ctrl+V!!

  • 2
    +1 - I disagree with folk who are downvoting this simply because they don't like it when web sites do this - sometimes web customers demand the programmer do things like this. No point punishing the person asking the question. – Bork Blatt Jun 29 '09 at 08:07
  • 1
    While I agree that this is a bad thing to do 99% of the time, that's no reason to down vote it. +1 accordingly. – Kevin Montrose Jun 29 '09 at 08:07

3 Answers3

6

You can't. Don't even try. Don't annoy your users.

If you put it publicly on the web, it can be copied. Technically, it already is copied as soon as the user sees it. As colithium pointed out, all the techniques can be circumvented. Heck, you can look at the source code. You can curl the raw data from the command line, no JS/IMG/layer hack can prevent that.

deceze
  • 510,633
  • 85
  • 743
  • 889
3

There is no full proof solution. You can play javascript games (easy to turn off). You can place invisible layers about the text so it can't be selected easily (easy to view source). You can use images instead of text (just bad).

colithium
  • 10,269
  • 5
  • 42
  • 57
0

While I agree in principle with the other posters that trying to do this may annoy the user, sometimes a manager or customer demands that this be done, and so an answer needs to be supplied.

Check out this page on www.dynamicdrive.com that will supply you with a few JavaScripts towards this end. Specifically see "Disable Text Select Script" and "No right click script".

Disable Text Select Script:

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
    target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    target.style.MozUserSelect="none"
else //All other route (ie: Opera)
    target.onmousedown=function(){return false}
target.style.cursor = "default"
}

//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"

No right click script:

//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com
var message = "Function Disabled!";

///////////////////////////////////
function clickIE4() {
    if (event.button == 2) {
        alert(message);
        return false;
    }
}

function clickNS4(e) {
    if (document.layers || document.getElementById && !document.all) {
        if (e.which == 2 || e.which == 3) {
            alert(message);
            return false;
        }
    }
}

if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS4;
} else if (document.all && !document.getElementById) {
    document.onmousedown = clickIE4;
}

document.oncontextmenu = new Function("alert(message);return false")
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Bork Blatt
  • 3,308
  • 2
  • 19
  • 17
  • 1
    It would be better to explain to the manager or customer why it is a very bad idea to try to prevent this. – Sander Marechal Jun 29 '09 at 08:09
  • 1
    True - and this is the approach I try - but sometimes they put their foot down, and then you have no choice. – Bork Blatt Jun 29 '09 at 08:13
  • Sander: It is not a bad idea at all. For example, if it is against the EULA/TOS to copy this text to outside contexts, then making it _difficult_ to do puts the onus on the abuser. It is like locking your front door (or at least closing it). Of _course_ it does not prevent burglary -- but it creates a definite barrier against the lazy or amateur thief and removes any possible dispute or confusion about the appropriateness of entering your home. – jwl Jan 12 '10 at 17:27
  • 1
    Link-only answers [are not good answers](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). I've copied the relevant scripts, which fortunately still exist (although in the case of the right-click one the quality is really suspect), so I've copied them into the answer for you. – T.J. Crowder Jul 17 '14 at 16:30