Disabling text selection, cut, copy, and right-click on a Page with jQuery and JavaScript
This post will discuss 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 you can encrypt HTML so that source code can only be readable to the browsers.
1. Disable Text Selection with JavaScript/jQuery
You can use the following 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
|
1 2 3 4 5 |
<body onmousedown = 'return false' onselectstart = 'return false'> // … // … // … </body> |
If you want to allow text selection but still prevent copy-cut, use the following code. You can also use jQuery’s bind() to achieve the same by specifying cut and copy events fired when the user cuts or copies a text.
JS
|
1 2 3 4 5 |
<body oncopy = 'return false' oncut = 'return false'> // … // … // … </body> |
jQuery
|
1 2 3 4 5 |
$(document).ready(function () { $('body').bind('cut copy', function (e) { e.preventDefault(); }); }); |
2. Disable Text Selection with CSS
The user-select property indicates where users can select text within an element. If set to none, it blocks selection from starting on that element. You can use this property and apply it on the <body> tag to prevent text selection.
CSS
|
1 2 3 4 5 6 7 8 9 |
/* Prevent text selection of an <body> element in all major browsers */ body { -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 */ } |
Instead of applying these properties to the whole body, you can move them to a class and apply them to the elements you want to disable select.
3. Disable Right Click:
You can use the following 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 the mouse right-click as well from the keyboard.
JS
|
1 |
document.oncontextmenu = new Function("return false;"); |
jQuery
|
1 2 3 4 5 |
$(document).ready(function () { $("body").on("contextmenu",function(e) { return false; }); }); |
4. HTML Encryption:
All the above-mentioned methods will work fine, but your HTML source code is still vulnerable. You 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 up to a great extent. The one limitation of it is that you will see your page on JavaScript enabled browsers only.
That’s all about disabling text selection, cut, copy, and right-click on a Page.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)