You are here: Reference > JavaScript > client-side > event handling > events > oncopy

oncopy event | copy event

Browser support:
Occurs before the selection is copied to the clipboard.
  • In Internet Explorer, the oncopy event occurs on the deepest element in the DOM hierarchy that contains the entire selection.
  • In Firefox, Google Chrome and Safari, the oncopy event occurs on the deepest element in the DOM hierarchy that contains the start point of the selection.
If the oncopy event is canceled, the contents of the current selection will not be placed on the clipboard. Canceling the oncopy event clears the clipboard in Google Chrome and Safari.
If you need access to the clipboard, use the clipboardData object in Internet Explorer, or for a cross-browser solution to manipulate the contents of the clipboard, see the page for the clipboardData object.

How to register:

<ELEMENT oncopy="handler">

In JavaScript:
object.oncopy = handler;
object.addEventListener ("copy", handler, useCapture);
object.attachEvent ("oncopy", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles Yes
Cancelable Yes
Event object

Actions that invoke the oncopy event:

  • Pressing CTRL + C.
  • Selecting the Copy command from the Edit menu.
  • Opening the context menu (right mouse button) and selecting the Copy command.

Events related to the clipboard:

Example HTML code 1:

This example illustrates the use of the oncopy event:
    <script type="text/javascript">
        function OnCopy () {
            if (window.clipboardData) {
                window.clipboardData.setData ("Text", "My clipboard data");
            return false;   // cancels the default copy operation
<body oncopy="return OnCopy ()">
    When you try to copy some text to the clipboard, 
    the 'My clipboard data' string will be placed instead in Internet Explorer.
    In Firefox, the contents of the clipboard are left untouched.
    In Google Chrome and Safari, an empty string will be placed.
    <br />
    Try it!
    Select some text on this page and press CTRL + C!
    <br /><br />
    Insert the text from the clipboard into this field (CTRL + V):
    <input type="text" />
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content