This script is designed to block users from selecting and copying text on a webpage. It prevents users from highlighting, right-clicking, or copying content from the site, thereby protecting your text-based content from being easily copied.
- Disable Text Selection: Prevents users from selecting text on the page, making it difficult to copy or highlight.
- Disable Context Menu (Right-click): Blocks the right-click menu, which typically allows users to copy text or inspect elements.
- Disable Copy Event: Even if users manage to highlight text (e.g., via dev tools), the copy function is disabled.
- Cross-browser Compatibility: The script uses CSS properties with vendor prefixes to ensure compatibility across major browsers like Chrome, Safari, Firefox, and Internet Explorer.
The script performs the following actions:
- Injects a Style Block: A
<style>
element is dynamically created and injected into the page's<head>
. This style block disables text selection using theuser-select
CSS property with vendor prefixes for browser compatibility. - Blocks Copy Event: The script listens for the "copy" event and prevents its default behavior, stopping users from copying text even if it’s somehow selected.
- Disables Context Menu: The "contextmenu" event (triggered by right-clicking) is also blocked, preventing users from accessing the right-click options like "Copy" or "Inspect".
To use this script on your website:
- Copy the provided JavaScript code.
- Paste it into your HTML file within a
<script>
tag or link to it from an external JavaScript file. - Ensure that the script is executed after the page loads to ensure proper functioning.
The script should be placed in the <head>
or just before the closing </body>
tag. Here is a sample of how to include it:
<script> // Insert the JavaScript code here </script>
- Not Foolproof: This script can discourage casual users from copying content but can be bypassed by more experienced users using developer tools or by disabling JavaScript.
- Non-intrusive: The script doesn't affect page performance significantly and doesn't interfere with the normal browsing experience (aside from disabling copy and right-click).
- Content Protection: While this adds a layer of protection to your content, it is important to remember that no method can completely secure text on the web.