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
  • right-click menu and select copy command

example

Example 1

execute JavaScript when copying text of <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 <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

Uwaga:Internet Explorer 8 or earlier versions do not support addEventListener() method.

technical details

bubbling: Wsparcie
cancellable: Wsparcie
event types: ClipboardEvent
supported HTML tags: all HTML elements

browser support

event Chrome IE Firefox Safari Opera
oncopy Wsparcie Wsparcie Wsparcie Wsparcie Wsparcie

Uwaga:Próba skopiowania obrazu, zdarzenie oncopy może nie działać zgodnie z oczekiwaniami w niektórych przeglądarkach (zobacz poniżej więcej przykładów).

Strony związane

Przeglądarka HTML DOM Referencje:Zdarzenie onpaste

Przeglądarka HTML DOM Referencje:Zdarzenie oncut