Anti-copy code
JavaScript Code to Prevent Copying Text on Website, No Right Click, No Text Coverage If you want to prevent users from copying or pasting the content on your website, you can use JavaScript to prevent text selection and disable the copy command. You can use the following code:
1. Prevent dragging + prohibit Ctrl + A, C in a single code.
<script>
function disableselect(e) {
return false;
}
function reEnable() {
return true;
}
document.onselectstart = new Function ("return false");
if (window.sidebar) {
document.onmousedown = disableselect;
document.onclick = reEnable;
}
document.addEventListener("keydown", function (event) {
if (event.ctrlKey && (event.key === "a" || event.key === "c")) {
event.preventDefault();
alert("Sorry! This command cannot be used.");
}
});
</script>
This code uses JavaScript to prevent text selection and disable certain keyboard shortcuts (such as Ctrl+A and Ctrl+C) on a web page. The code works like this:
disableselect(e)
→ Use to restorefalse
To prevent text selectionreEnable()
→ Restoretrue
To enable re-clicking (used in some older browsers)document.onselectstart
→ It is an Event Handler that runs when text selection is started (selectstart
)new Function("return false")
→ Close text selection by resettingfalse
window.sidebar
→ Use to check if the browser is Firefox (Mozilla's old browser)document.onmousedown = disableselect;
→ Disable text selection when mouse is presseddocument.onclick = reEnable;
→ Enable clicks (so that all clicks are not blocked)event.ctrlKey
→ Check that the buttonCtrl
Are you being suppressed?event.key === "a"
→ Check if the user pressedCtrl + A
(Select all)event.key === "c"
→ Check if the user pressedCtrl + C
(Copy)event.preventDefault();
→ Block the operation of shortcut keysalert("Sorry! This command cannot be used.");
→ Show notification message
2. Do not drag-cover the text.
<script>
document.addEventListener("selectstart", function (event) {
let target = event.target;
// Disable text selection for other sections
event.preventDefault();
});
</script>
ใช้ document.addEventListener(“selectstart”, function (event) {…})
- Capture the event
"selectstart"
This occurs when the user tries to select text. event.preventDefault();
→ Disable text selection across all web pages
3. Do not cover text and exclude some classes.
<script>
document.addEventListener("selectstart", function (event) {
let target = event.target;
// Check if the target class starts with "wp-block-code".
while (target) {
if (target.classList && [...target.classList].some(cls => cls.startsWith("wp-block-code"))) {
return; // Allow text selection
}
target = target.parentElement;
}
// Disable text selection for other sections
event.preventDefault();
});
</script>
Capture the event "selectstart"
- Occurs when the user attempts to drag to select text.
Check if the user is trying to select text in <pre class="wp-block-code">
Or not
- use
while (target)
To check the element and parent element - if
target
haveclass
at Starting with"wp-block-code"
→ Allow text selection - use
target.classList.some(cls => cls.startsWith("wp-block-code"))
To check
If not selected in wp-block-code
→ Close text selection
event.preventDefault();
Disable text selection everywhere exceptwp-block-code
4. Copy protection code on web pages
<script>
document.addEventListener('copy', function (e) {
e.preventDefault();
alert('Sorry! The content cannot be copied.');
});
</script>
1️⃣ Event capture copy
- use
document.addEventListener('copy', function (e) {...})
- When the user presses
Ctrl + C
Or right click and select “Copy” → This function will work.
2️⃣ e.preventDefault();
- Block copying (
copy event
) - Users will Unable to copy text Go to clipboard
3️⃣ Notification (alert(...)
)
- Show message
"Sorry! The content cannot be copied."
- Notify users that copying is blocked
5. Disable Right Click with Notification
<script>
var message = "Do not click right..!!";
document.addEventListener("contextmenu", function (e) {
e.preventDefault();
alert(message);
});
</script>
This code works to prevent right click on web page using JavaScript. The given function will catch the right click event (context menu event
) and blocks the right-click menu from appearing. It also displays an alert box to inform the user that right-clicking is blocked. The alert message is predefined in a variable and is displayed when a right-click is made.
6. Disable right click without displaying notification message.
<script>
document.addEventListener("contextmenu", function (e) {
e.preventDefault();
});
</script>
This code uses JavaScript to disable the right-click menu on a web page by listening for an event. context menu
This occurs when a user right-clicks on a web page and uses the command e.preventDefault()
To prevent the right-click menu from being displayed in browsers.
- Capture the event
context menu
: When a user right-clicks on a web page e.preventDefault()
: Disables the browser's right-click menu, which normally allows users to access functions such as "Copy", "View Source", etc.
The result is Users will not be able to right-click. On the web page that uses this code.
7. Use CSS to prevent text copying.
/* Prevent text selection with CSS */
body {
user-select: none;
}
This code is used CSS To lock text selection on a web page by setting user-select: none;
In the style of tags <body>
. The code works as follows:
Unable to drag the mouse to select text.
user-select: none;
: Prevent users from selecting text on a web page.
Use with tags <body>
Prevent all text on the page from being selected.
8. Prevent dragging and dropping text (Disable Drag & Drop)
This code prevents dragging or dropping of content or images elsewhere.
<script>
document.addEventListener("dragstart", function (e) {
e.preventDefault();
});
document.addEventListener("drop", function (e) {
e.preventDefault();
});
</script>
This code is used JavaScript To prevent dragging and dropping on web pages, use: preventDefault()
To disable both events
9. Disable Inspect Element and View Source.
Disable Ctrl + U (View Source), Ctrl + Shift + I (Developer Tools), F12 (DevTools)
<script>
document.addEventListener("keydown", function (e) {
if (e.ctrlKey && (e.key === "u" || e.key === "s" || e.key === "i" || e.key === "j")) {
e.preventDefault();
alert("Sorry! This command cannot be used.");
}
if (e.key === "F12") {
e.preventDefault();
alert("Developer tools are disabled");
}
});
</script>
This code is used JavaScript To prevent the use of certain commands that can help users access developer tools and functions on a web page, such as opening View Source, Inspect Element, or Developer Tools Through various shortcuts by capturing events keydown
And prevent those orders too. preventDefault()
.
Inspection Ctrl + U
, Ctrl + S
, Ctrl + I
, Ctrl + J
:
If the user presses Ctrl Hold and press IN, S, I, or J (This is a shortcut that is often used to open the source of a web page or development tool.)
e.preventDefault()
Disable this command- Show notification message:
"Sorry! This command cannot be used."
Button Check F12
:
If the user presses F12 Which is the shortcut to open Developer Tools
e.preventDefault()
Disable Developer Tools- Show notification message:
"Developer tools are disabled"
10. Disable automatic text selection in browsers.
css Copy Edit/* Disable text selection and data dragging */
body, p, span, div {
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
This CSS code is used to Prevent text selection (text selection) and dragging data (dragging) on web pages using user-select: none;
and support for different browsers
Job details:
user-select: none;
- Prevents users from selecting text within a specified HTML element (in this case:
body
,p
,span
,div
)
- Prevents users from selecting text within a specified HTML element (in this case:
-webkit-user-select: none;
- For use with browsers WebKit engine (such as Chrome, Safari)
- It allows you to prevent text selection in these browsers.
-moz-user-select: none;
- Used for Firefox
- Prevent text selection in Firefox
-ms-user-select: none;
- Used for Internet Explorer
- Prevent text selection in IE
How to use Go to the header section – Insert the code inside the head.
How to add anti-copy code to WordPress

- Go to your WordPress admin area, go to Appearance/Theme File Editor menu.
- Right-hand side, choose the theme (you are currently using)
- Find the theme header (header.php)
- Place the code in
How to insert anti-copy code into Blogger

- Go to the Theme menu. Blogger
- Click the drop-down button Edit HTML
- Enter the code inside
note
althoughcodeThese will reduce the chances of content being copied from your website, but they are not 100% foolproof, as users can still access the information using developer tools or browser extensions. So, if you want to protect your content, consider taking additional measures, such as adding a watermark to your images or using a membership system to restrict access to sensitive content.
Hopefully these codes will be useful to prevent copying text on your website! 🚀