10 JavaScript codes to prevent copying ❌ Do not copy text on the website.

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:

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 restore false To prevent text selection
  • reEnable() → Restore true 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 resetting false
  • window.sidebar → Use to check if the browser is Firefox (Mozilla's old browser)
  • document.onmousedown = disableselect; → Disable text selection when mouse is pressed
  • document.onclick = reEnable; → Enable clicks (so that all clicks are not blocked)
  • event.ctrlKey → Check that the button Ctrl Are you being suppressed?
  • event.key === "a" → Check if the user pressed Ctrl + A (Select all)
  • event.key === "c" → Check if the user pressed Ctrl + C (Copy)
  • event.preventDefault(); → Block the operation of shortcut keys
  • alert("Sorry! This command cannot be used."); → Show notification message

ใช้ 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

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 have class 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 except wp-block-code

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

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.

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.

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.

This code prevents dragging or dropping of content or images elsewhere.

This code is used JavaScript To prevent dragging and dropping on web pages, use: preventDefault() To disable both events

Disable Ctrl + U (View Source), Ctrl + Shift + I (Developer Tools), F12 (DevTools)

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"

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:

  1. user-select: none;
    • Prevents users from selecting text within a specified HTML element (in this case: body, p, span, div)
  2. -webkit-user-select: none;
    • For use with browsers WebKit engine (such as Chrome, Safari)
    • It allows you to prevent text selection in these browsers.
  3. -moz-user-select: none;
    • Used for Firefox
    • Prevent text selection in Firefox
  4. -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.


  • 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
Do not copy text.
  • 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! 🚀

Facebook Comments Box
Previous Article

How to create a Blogger (in 3 minutes) for beginners and secret tips you don't know

Next Article

18 WordPress Translation Plugin Alternatives🈵Create a Multilingual Site