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.

JS

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.

JS

jQuery

 

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.

CSS

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.

JS

jQuery

 

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.

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂