I have a question about preventing the selection of content and right-clicking with your mouse on a website. Some websites that I have encountered display a dialog which prohibits right-clicking but the website in question does not show any dialog. To visually make it clear, here is the website in question: http://www.amandakjones.com/homemade-creamy-garlic-dressing.php.

Notice that you are not allowed to right-click, let alone select text. I would like to know how this is done.

  • 21
    Copyright laws is what protects your content. JavaScript and other tricks will never achieve that no matter how hard your try. – laurent Dec 27 '12 at 13:41
  • 7
    Why do you need it? If anyone wants to copy it they will find a way, even if it means retyping the whole thing. (or use F12 in the browser to copy it from HTML) – rdkleine Dec 27 '12 at 14:19
  • 4
    It appears you've added the JavaScript code on the web page. I would like to advise you to remove it, because now I can't even right-click on an hyperlink. Moreover, just running document.body.onselectstart = document.oncontextmenu = null removes the contraints completely. – pimvdb Dec 27 '12 at 16:04
  • 2
    I hate this "feature". As I read a website, I am constantly selecting text as a sort of super short-term bookmark. When I come across websites like this, I make a note in my mind to avoid them... and there is more than 1 website which I commonly see in google and avoid only because of this. – Earlz Dec 28 '12 at 7:40
  • 4
    If the goal is to annoy your users, might as well take a screenshot of the website and then replace the text with the image. – frozenkoi Dec 28 '12 at 11:33

How to disallow selection of text on website?

You don't.
It's effectively impossible, assuming some minimal effort on the part of the person who wants the text. All you can really do is create speed bumps that will do nothing but irritate them. Also note that there are perfectly valid uses for the right-click menu (for example) that have nothing to do with stealing your content. If the person is after one of those, then you're really going to be annoying them.

Pretty much all attempts at this (preventing right-click or text selection) depend upon JavaScript, which means:

  1. I can temporarily disable JavaScript.
  2. I can use a browser that doesn't run JavaScript at all.
  3. I can view source via the browser chrome menus.
  4. I can view source via a key combination.
  5. I can select large chunks of text by double-clicking on paragraphs.
  6. I can select all via key combination and edit externally.
  7. I can fetch the document with wget or similar.
  8. I can feed the page to a mobilizer (Instapaper, Readability, etc.) then copy the text unrestricted from there.
  9. I can save to Evernote or similar.
  10. Besides the numerous workarounds, these tricks tend to be fragile. Some Firefox extension I have–there's nothing installed that targets this sort of thing–is allowing me to right click on that site without issue. The same may be the case for modifications to other browsers.

The list goes on.
If you don't want people copying your text, then don't put it someplace they can get to it.

If, for some reason, all you actually need to do is take some control of the appearance of text being selected, that can be accomplished with the user-select CSS property (or ::selection though it seems like it's on the way out), but that has no effect on the ability to copy the affected text.

  • 2
    There's even a built-in Firefox option - Options > Content > Enable Javascript > Advanced > Disable or replace context menus – Olly Hodgson Dec 27 '12 at 15:19
  • 2
    Here's another one: At one point, I'd run across one that pops up an alert when you right-clicked, and accidentally discovered that if you held down the left mouse button while you did the right-click, the alert never showed and the context menu worked just fine. IIRC, this was bare-bones Firefox install, no extension at all – Izkata Dec 27 '12 at 16:18
  • 1
    "If you don't want people copying your text, then don't put it someplace they can get to it". Nailed it perfectly. – That Brazilian Guy Dec 27 '12 at 22:46
  • 1
    Moreover, 99% of the time, you're really annoying legitimate visitors who would actually promote your site/product or otherwise increase your revenue. When I visit a site and then find that I can't use context search to look up a word, google a phrase, or IMDb a movie title, I immediately stop visiting the site. I imagine there are a lot of other people out there that are equally offended by this practice with zero intention of "stealing content". – Lèse majesté Dec 28 '12 at 0:18

I think, it's a really bad practice and annoying but the site itself using the script below.

    document.oncontextmenu=new Function("return false");
    function disableSelection(target){
        if (typeof target.onselectstart!="undefined") //For IE 
            target.onselectstart=function(){return false}
        else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
        else //All other route (For Opera)
            target.onmousedown=function(){return false}
        target.style.cursor = "default"
  • 1
    I removed the incomplete/invalid code at the start (due to copy/paste). This was unnecessary anyway since it targeted out-of-date browsers. – MrWhite Dec 27 '12 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.