CKTIS / HTML
oncopy — copy event handler
The oncopy attribute assigns JavaScript code to handle the copy event. It can appear on any HTML element to react to copying, for example by updating a message on the page.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
oncopy="JavaScript statements"oncopy is a global event handler attribute for copy, the event associated with copying. Its value contains JavaScript statements, not a Boolean value or merely a function name. Within the attribute code, the event parameter provides the event object.
Distinguish the HTML attribute from the JavaScript property element.oncopy, which accepts a function or null. That property is also available on Document and Window objects. Assigning again replaces the property's previous function; addEventListener('copy', handler) registers independent listeners.
Copying selected text in the example paragraph does not guarantee that its handler runs. For a non-editable selection, the Clipboard API specification targets the copy event at the focused node, or at body if no node has focus. The example paragraph is not focusable. If its handler runs, the message changes to copy; otherwise, it stays Waiting. The example does not change the copied content. Returning false from the handler cancels the event if it is cancelable. Content Security Policy can block code in attributes; adding oncopy does not bypass that restriction.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>oncopy demo</title>
<p id="sample" oncopy="document.getElementById('status').textContent = event.type">Select and copy this text.</p>
<output id="status">Waiting</output>
</html>Result
copy
The result `copy` was observed in an isolated Chromium environment. In other browsers, the message may stay `Waiting`.