: How to disallow selection of text on website? 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
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: 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.
More posts by @Speyer207
2 Comments
Sorted by latest first Latest Oldest Best
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:
I can temporarily disable JavaScript.
I can use a browser that doesn't run JavaScript at all.
I can view source via the browser chrome menus.
I can view source via a key combination.
I can select large chunks of text by double-clicking on paragraphs.
I can select all via key combination and edit externally.
I can fetch the document with wget or similar.
I can feed the page to a mobilizer (Instapaper, Readability, etc.) then copy the text unrestricted from there.
I can save to Evernote or similar.
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.
I think, it's a really bad practice and annoying but the site itself using the script below.
<script>
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
target.style.MozUserSelect="none"
else //All other route (For Opera)
target.onmousedown=function(){return false}
target.style.cursor = "default"
}
disableSelection(document.body);
</script>
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.