DEV Community

Cover image for Preventing copying text in a webpage ๐Ÿ˜
Rahul
Rahul

Posted on

Preventing copying text in a webpage ๐Ÿ˜

This feature is really important if you have a very very unique content website or an online eBook. So here is the basic post and required post for all of you. In this post, we'll see how to prevent copying text in a webpage and I have got a bonus too.



Do you want development news right on your default chrome or firefox page? Then get the amazing daily.dev extension. There are only PROS of getting this extension no CONS.

Lez Go ๐Ÿ‘Š

Simple and Basic ONE!

-> This is the simple tutorial for preventing copying of an element on the web-page. Ex. p, div, main.
So here is how we can do that.

<div id="test" onmousedown='return false;' onselectstart='return false;'>
<!-- So we're disabling the texts coming under div tag-->
Enter fullscreen mode Exit fullscreen mode

Let's see an example-

See you can't select the text now. ๐Ÿ‘Š


Using CSS

-> You can directly disable by using CSS by just applying these properties to the whole body, you can move them to a class and apply that class to the elements you want to disable select.

/* Prevent text selection of a <body> element in all major browsers */
h2 {
    -youbkit-touch-callout: none; /* iOS Safari */
        -youbkit-user-select: none;   
/* Chrome 6.0+, Safari 3.1+, Edge & Opera 15+ */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none;       /* IE 10+ and Edge */
    user-select: none;      /* Non-prefixed version,
                  currently supported by Chrome and Opera */
}

Enter fullscreen mode Exit fullscreen mode

Here is the example for that-


Disabling Right Click

-> So technically no chances of copying.

We will use JavaScript and jQuery

document.oncontextmenu = new Function("return false;");

///JavaScript
Enter fullscreen mode Exit fullscreen mode
$(document).ready(function () {
    $("body").on("contextmenu",function(e){
        return false;
    });
});

// jQuery
Enter fullscreen mode Exit fullscreen mode

The main motive is to capture the onContextMenu event, and return false in the event handler. So, this will block the access from mouse right click as well from the keyboard.




Do you want development news right on your default chrome or firefox page? Then get the amazing daily.dev extension. There are only PROS of getting this extension no CONS.




Thanks For Reading.

If it's helpful. Then Like, Share and React. ๐Ÿ˜๐Ÿ˜Ž

Top comments (30)

Collapse
 
contourintegral profile image
โˆฎ

Well, I can copy using DevTools ๐Ÿ˜‚
Nice article, though ๐Ÿ‘

Collapse
 
rahxuls profile image
Rahul

Cheater๐Ÿ˜

Collapse
 
amgedalmogahed profile image
AmgedAlmogahed

Google lens can copy any text any where

Collapse
 
rahxuls profile image
Rahul

Found the second cheater.

Collapse
 
powerfuldruid profile image
powerfuldruid

Pretty sure that this also makes the content inaccessible

Collapse
 
rahxuls profile image
Rahul

Yea it will maybe.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Whatever you may try, one can always use Puppeteer, DevTools or View Page Source.

However, I would want to send signals to say, you are not intended to copy, unless you want to break the law. (And disable easy-copying, without breaking a11y, e.g. tab.)

Collapse
 
rahxuls profile image
Rahul

Hahaa yea.

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

It's better to choose a correct license I promise there is no merritt to any of the above techniques. Get the LAW on your side.

Collapse
 
rahxuls profile image
Rahul

Ahh!! ๐Ÿคฃ

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ • Edited

๐Ÿšจ๐Ÿš” DMCA anyone who dares oppose your IP

Collapse
 
janpauldahlke profile image
jan paul

user-select: none ?

Collapse
 
rahxuls profile image
Rahul

Yea simple. (See the browser support too brother)

Collapse
 
janpauldahlke profile image
jan paul

i use css shims for this :-)

Thread Thread
 
rahxuls profile image
Rahul

amazing.

Collapse
 
aayushidroid profile image
Aayushi Sharma

I think we can access content using Request module.

Collapse
 
rahxuls profile image
Rahul

I found the 4th cheater.

Collapse
 
contourintegral profile image
โˆฎ

This is also identical to using, say, cURL or HTTPie.
I cheated two times๐Ÿ™„๐Ÿ˜‚

Thread Thread
 
rahxuls profile image
Rahul

I'm requesting the Dev community to ban you ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

Collapse
 
masiota profile image
Mahmoud AlSharif

Thanks for sharing!
There is also more robust solutions like rendering text within the canvas or even better transform the text to SVG in the backend.

Collapse
 
oliverobenland profile image
Oliver Obenland

On mobile I can copy like usual (iPhone)

Collapse
 
rahxuls profile image
Rahul

Third cheater.

Collapse
 
wweiss profile image
William Weiss

So I tried the example in my favorite browser, curl, and had no problem copying...