Disabling Text Selection, Cut, Copy and Right Click on a Page

In this post, we will cover how to stop content theft from your website simply by disabling text selection, cut, copy and right click function via mouse and keyboard. We will also cover how we can encrypt HTML so that source code can only be readable to the browsers.


1. Disable Text Selection with JS/jQuery

We can use below code to prevent text selection (and hence copy-cut) within the body or div tag. It basically overrides the default browser’s behavior on onmousedown and onselectstart event.


If we want to allow text selection, but still prevent copy-cut, we can use below code. We can also use jQuery bind() to achieve the same by specifying cut and copy events which are fired when the user cuts or copies a text.




2. Disable Text Selection with CSS

The user-select property indicates where users are able to select text within an element. If set to none, it blocks selection from starting on that element. We can use this property and apply it on tag to prevent text selection.


Instead of applying these properties to the whole body, we can move them to a class and apply that class to the elements you want to disable select.


3. Disable Right Click –

We can use below code to prevent mouse right click on any page. The idea is to capture the onContextMenu event, and return false in the event handler. This will block all access to the context menu from mouse right click as well from the keyboard.




4. HTML Encryption –

All above mentioned methods will work fine but your HTML source code is still vulnerable. We can prevent the source code from getting stolen by others by using this HTML encrypt tool.

HTML Encryption converts your web page contents to a non-easily understandable format. This may protect your code from being stolen by others upto great extent. The one limitation of it is that your page will be seen on JavaScript enabled browsers only.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

newest oldest most voted
Notify of
Deepankar Dey

There are also many wordpress plugin to disable copy and right click for wordpress blogger.


How can copy your code for use?


There’s no such thing as ‘content theft’ or ‘[code] being stolen’ on the web. Stealing requires removing a given thing from its original place, thereby depriving the victim of the possession of the thing. What you’re talking about preventing would be better called copyright infringement.

Speaking of copying, web browsing implies making private copies of data from servers to clients. Hence, if you publish something, you effectively allow everybody to make these private copies. As long as such copies remain private, you have no way of telling what a user can/will do with the data on that user’s computer.

The methods you suggest are mere annoyances to users who only want to copy interesting content for private use. The only 100% effective method is not to publish at all.

hanan elsman

thank you
Please code could block the download