21

I'm trying to prevent information to be copied from a page (for non-technical users of course). I know how to disable selecting text using the mouse. The following jquery code works:

$(function(){
  $.extend($.fn.disableTextSelect = function() {
    return this.each(function(){
      if($.browser.mozilla){//Firefox
        $(this).css('MozUserSelect','none');
      }else if($.browser.msie){//IE
        $(this).bind('selectstart',function(){return false;});
      }else{//Opera, etc.
        $(this).mousedown(function(){return false;});
      });
    });
    $('.noSelect').disableTextSelect();
});

But users can still use Ctrl+A to select the entire page. Any workarounds for this?

Keltex
  • 26,220
  • 11
  • 79
  • 111
  • 13
    Please, don't do that : there will always be a way to copy your content *(as you put it online)* ; and your "security" will do nothing more than annoy your users... – Pascal MARTIN Mar 08 '10 at 18:38
  • 5
    Horrrible idea. Why would you want to do such a thing? – Pekka Mar 08 '10 at 18:38
  • It is none of your business. If you don't want it copied, don't share it in the first place. – nobody Mar 08 '10 at 18:38
  • Not to mention that they can still do a File -> Save As... to copy the content... – kdmurray Mar 08 '10 at 18:38
  • Or disable JavaScript. Two clicks from where I'm sitting. – Pekka Mar 08 '10 at 18:39
  • But if you must do it, you could serve the text as an image. Seriously - that's the only halfway efficient way to protect textual content that comes to my mind. – Pekka Mar 08 '10 at 18:40
  • 5
    Whether it's a bad idea (which I agree :-)) or not, it's still a valid technical question, so I don't see the reasons for the downvotes. – Franci Penov Mar 08 '10 at 18:40
  • 9
    Boy I didn't know some questions were verboten. It's a business requirement for my client (and an Intranet application). So unfortunately I can't go to them and say, "sorry I can't do that. The stack overflow users downvoted the idea." – Keltex Mar 08 '10 at 18:40
  • 1
    `$('#pencil,#pen').pickup(function() { $(this).drop(); return false; });` – tvanfosson Mar 08 '10 at 18:42
  • 7
    @Keltex -- it's your job as a developer to explain to them why this is a horribly broken idea, won't solve the problem they are trying to solve anyway, and will likely tick off anyone who uses the system for no good reason. – tvanfosson Mar 08 '10 at 18:43
  • 1
    @tvanfosson - So you know, I have explained this. But some people are just insistent. – Keltex Mar 08 '10 at 18:50
  • @Keltex -- maybe point him at this question? – tvanfosson Mar 08 '10 at 18:54
  • 2
    There are actually use cases when it is totally valid to want to disable select all. A highly interactive Web-App for example that is anything but a structured text file but an interactive and very dynamic platform (i.e. a game interface, etc.). Of course it is senseless to use this on a blog/newspaper article. On the other hand platforms such as google maps do interfere with Select-All which is a really great argument with customers for not disabling select all: "google has a reason for not doing this..." – wirrbel Dec 06 '13 at 08:40
  • 1
    I want to disable selecting of text when ctrl+a is pressed as my application is very highly interactive and the ctrl+a key is being used to select specific objects in the tool which is what the users care about in our application. One could argue to use a different key combination. But most users know ctrl+a is for selection. Therefore this question is very valid and useful. Please answer the technical aspect of it instead of suggesting why this is a "bad" idea. – Kalyan May 06 '15 at 21:01
  • There is more than one reason for wanting to disable CTRL+A from selecting all content. For example, in our web app we have a drag and drop editor where people can select the objects they've selected, we user CTRL+A to select all the objects, but we don't want all the text on the page to be selected as well... Bad UX that just looks buggy..... – Mattisdada Apr 19 '16 at 02:24

5 Answers5

16

this code works for every combination of ctrl+key you want 65 is the ascii code of 'A'

add 97 if you want to check also for 'a'

$(function(){   
    $(document).keydown(function(objEvent) {        
        if (objEvent.ctrlKey) {          
            if (objEvent.keyCode == 65) {                         
                objEvent.disableTextSelect();
                return false;
            }            
        }        
    });
});    

Should works, I wrote it directly without testing..

Marcx
  • 6,806
  • 5
  • 46
  • 69
  • 5
    @Marcx - Thanks for answering rather than immediately downvoting because you don't like the business requirement. – Keltex Mar 08 '10 at 18:42
  • 4
    +1 for the solution. @Keltex I don't think any of the people who commented above actually downvoted your question. But airing comments about the practice is totally valid, and some of the arguments might tell your client something about the technical doability of this "business requirement." – Pekka Mar 08 '10 at 18:43
  • 1
    In Chrome 37.0.2062.94, "Uncaught TypeError: undefined is not a function" on `objEvent.disableTextSelect();`. – rgajrawala Aug 31 '14 at 07:14
  • 2
    It doesn't catch ctrl + A key combination on Firefox 32. – Marek Bar Oct 23 '14 at 11:03
  • Not working in the latest Chrome an Firefox... its due of jQuery, its not longer supported - version added: 1.6, deprecated: 1.9 and then removed. You have to use `objEvent.PreventDefault();`. – Legionar Jun 10 '15 at 11:24
7

Works for Windows (Ctrl+A) + MacOS (CMD+A) and use preventDefault() instead of return false:

$(function(){   
  $(document).keydown(function(e) {    
    if ((e.ctrlKey || e.metaKey) && e.keyCode == 65) {
      e.preventDefault();
    }
  });
});
ben
  • 1,432
  • 12
  • 17
1

Some clients honestly don't understand how the internet works so you should do your part in explaining to them that anything that is displayed to the user can easily be saved, regardless of what you do.

At best, you can disable certain things, making it hard for the simplest of users to copy text.

If you don't do this, someone is going to figure out a way to get by whatever stop-gap you put in place and they will come back to you saying "hey, I thought I told you to lock this down"

Allen Rice
  • 19,068
  • 14
  • 83
  • 115
  • 1
    @Allen. Already done so... unfortunately my client is very insistent. And he pays the bills. – Keltex Mar 08 '10 at 18:44
  • 2
    As long as he understands that this does not accomplish any form privacy or piracy prevention and that there are ways to easily get around it, AND they specifically desire this, I would be ok with it. – Allen Rice Mar 08 '10 at 18:45
  • 1
    sort of "As long as you don't mind paying for features that have no possibility of actually working, I guess I can develop it." I guess there's a time to go along to get along, but I'd feel bad taking money to write code I knew wouldn't work. – tvanfosson Mar 08 '10 at 18:49
  • @tvanfosson, I agree with you man, but some people can't hand pick their clients and those are the people who probably really need the business. *shrugs* Crappy situation all the way around. I for one would probably push back as hard as I possibly could against such a requirement. I have in the past and have been successful. – Allen Rice Mar 08 '10 at 18:55
1

The accepted answer is no more working, because disableTextSelect() was deprecated since jQuery 1.9, and later it was removed and now not supported.

You have to use objEvent.PreventDefault(); - this is working in the newest jQuery and also newest browsers:

$(function() {
    $(document).keydown(function(objEvent) {
        if (objEvent.ctrlKey) {
            if (objEvent.keyCode == 65) {
                objEvent.preventDefault();
            }
        }
    });
});
Legionar
  • 7,472
  • 2
  • 41
  • 70
0

It is easy with css:

body {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -user-select: none;
}

I hope it helps

Ámon Tamás
  • 141
  • 3
  • 8