oncopy event

Definition and Usage

The oncopy event occurs when the user copies the content of an element.

Tip:The oncopy event also occurs when the user copies elements created with the <img> element (such as images).

Tip: The oncopy event is mainly used for <input> elements with type="text".

Tip:Here are three ways to copy elements/element content:

  • CTRL + C
  • select copy from the browser's edit menu
  • select the copy command from the right-click menu

Example

Example 1

execute JavaScript when copying the text of the <input> element

<input type="text" oncopy="myFunction()" value="Try to copy this text">

try it yourself

Example 2

execute JavaScript when copying some text of the <p> element

<p oncopy="myFunction()">Try to copy this text</p>

try it yourself

Example 3

execute JavaScript when copying an image

<img src="codew3c.gif" oncopy="myFunction()">

try it yourself

syntax

In HTML:

<element oncopy="myScript">

try it yourself

In JavaScript:

object.oncopy = function(){myScript};

try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("copy", myScript);

try it yourself

Σημείωση:Internet Explorer 8 or earlier versions do not support addEventListener() method.

technical details

bubbling: Υποστήριξη
cancellable: Υποστήριξη
event types: ClipboardEvent
supported HTML tags: all HTML elements

browser support

event Chrome IE Firefox Safari Opera
oncopy Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη

Σημείωση:Παράδειγμα: Αν προσπαθήσετε να αντιγράψετε εικόνα, το συμβάν oncopy μπορεί να μην λειτουργεί όπως αναμένεται σε ορισμένους περιηγητές (δείτε παρακάτω για περισσότερα παραδείγματα).

Σχετικές σελίδες

Εγχειρίδιο HTML DOM:Συμβάν onpaste

Εγχειρίδιο HTML DOM:Συμβάν oncut