The oncopy attribute is an event-handler attribute. It runs JavaScript when the copy event fires — the user starts a copy through the browser UI (for example Ctrl+C / Cmd+C, or a Copy menu item).
By default, the selection is copied to the clipboard. Your handler can replace that data with event.clipboardData.setData(…) and preventDefault(). It cannot read the clipboard. Related events: oncut and onpaste.
Baseline · Widely available.
Not deprecated and not experimental. The copy event is stable across modern browsers since about July 2015. It bubbles, is cancelable, and is composed.
Prefer addEventListener in apps.
Inline oncopy is fine for learning. Production code usually uses el.addEventListener("copy", handler). Blocking copy is a weak “protection” — users can still capture content other ways.
Syntax
HTML Syntax
syntax.html
<element oncopy="script">
script is JavaScript that runs when copy fires. Equivalent JS: el.oncopy = handler or el.addEventListener("copy", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Select text in the box, then copy (Ctrl+C / Cmd+C). Toggle options to transform or block the copy.
On copy
Paste somewhere else to verify uppercase or blocked modes.
Selectable text
Select this sample text, then copy it.
Waiting for copy…
Code
HTML Examples
Short, copy-ready snippets. Use Try It Yourself for a live editor.
Example 1 — Inline oncopy on a Textarea
Show a status message when the user copies from the field.
HTML
<label for="notes">Notes:</label>
<textarea id="notes" rows="3" cols="40" oncopy="onCopyNote()">
Select some text and copy it.
</textarea>
<p id="status">Waiting for copy…</p>
<script>
function onCopyNote() {
document.getElementById("status").textContent = "copy event fired.";
}
</script>
User copies via keyboard or UI — copy fires on the element.
oncopy runsonCopyNote() and updates status.
Default still copies the selection unless you call preventDefault().
Example 2 — Put Uppercase Text on the Clipboard
Replace the copied selection with a transformed string.
HTML
<div id="source" contenteditable="true">
Copy text from this box.
</div>
<p id="hint">Paste elsewhere to see UPPERCASE.</p>
<script>
document.getElementById("source").addEventListener("copy", function (event) {
var selection = document.getSelection();
event.clipboardData.setData(
"text/plain",
selection.toString().toUpperCase()
);
event.preventDefault();
});
</script>
preventDefault() cancels the default clipboard write.
Status updates so the student sees the event still ran.
Not real DRM — users can still retype, screenshot, or view source.
Edge Cases & Pitfalls
Most mistakes are trying to read the clipboard on copy, or treating preventDefault as content protection.
read
Cannot read clipboard in copy
The handler may write with setData, but it cannot read clipboard contents. Reading belongs to other APIs / paste flows with permissions.
setData
Remember preventDefault with setData
If you set custom data but forget to cancel the default, the browser may still write the raw selection.
synthetic
Synthetic copy events
Dispatching a fake copy event does not change the system clipboard. Only a real user-initiated copy (or the Async Clipboard API) does.
vs cut
Copy ≠ cut
oncut also removes the selection from the document. Use the matching event for your UX.
a11y
Do not block copy without a reason
Blocking copy frustrates users and assistive workflows. Prefer clear licensing text over disabling clipboard.
Compatibility
Browser Support
oncopy is an HTML event-handler attribute for the Clipboard API in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
oncopy
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the copy event on selectable content.
97%Global support (approx.)
Google Chrome1+
Full support
Mozilla Firefox22+
Full support
Apple Safari3+
Full support
Microsoft Edge12+
Full support
Opera15+
Full support
Samsung InternetModern · Full
Full support
Internet Explorer9+
Supported
oncopyWidely supported
Bottom line: Safe to use for reacting to user copy. Prefer addEventListener in production, and use setData + preventDefault only when you intentionally change clipboard content.
Remember
Key Takeaways
Role: run script when the user initiates a copy.
Write:clipboardData.setData + preventDefault to customize.
No read: the copy handler cannot read the clipboard.
Family: pair with oncut / onpaste as needed.
One line:oncopy reacts to a user copy — you can change what is written, but you cannot read the clipboard here.
Frequently Asked Questions
It is an event-handler attribute. When the user starts a copy action through the browser UI (for example Ctrl+C / Cmd+C or a Copy menu item), the copy event fires and the JavaScript in oncopy runs.
Yes. In the handler, call event.clipboardData.setData("text/plain", yourText) and event.preventDefault() so your data replaces the default selection copy.
No. A copy event handler cannot read clipboard contents. It can only write via clipboardData.setData after canceling the default action.
oncopy runs when copying; oncut when cutting (copy + remove); onpaste when pasting into a target. All are Clipboard API events on the element (and they bubble).
Both work. For learning, oncopy in HTML is fine. In production code, prefer element.addEventListener("copy", handler) so you can attach multiple listeners and keep script out of markup.
No. The copy event and oncopy handler are standard and Baseline widely available since about July 2015.