Clipboard: write() method

Baseline 2024
Newly available

Since June 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The write() method of the Clipboard interface writes arbitrary ClipboardItem data such as images and text to the clipboard, fulfilling the returned Promise on completion. This can be used to implement cut and copy functionality.

The method can in theory write arbitrary data (unlike writeText(), which can only write text). Browsers commonly support writing text, HTML, and PNG image data.

Syntax

js
write(data)

Parameters

data

An array of ClipboardItem objects containing data to be written to the clipboard.

Return value

A Promise which is resolved when the data has been written to the clipboard. Note that if the underlying OS does not support multiple native clipboard items on the system clipboard, then only the first ClipboardItem in the array is written.

The promise is rejected if the clipboard is unable to write to the clipboard.

Exceptions

NotAllowedError DOMException

Thrown if writing to the clipboard is not allowed.

Security considerations

Writing to the clipboard can only be done in a secure context.

Additional security requirements are covered in the Security consideration section of the API overview topic.

Examples

Write text to the clipboard

This example function replaces the current contents of the clipboard with a specified string when a button is pressed. Note that for this particular case, you could just as readily use Clipboard.writeText().

js
button.addEventListener("click", () => setClipboard("<empty clipboard>"));

async function setClipboard(text) {
  const type = "text/plain";
  const clipboardItemData = {
    [type]: text,
  };
  const clipboardItem = new ClipboardItem(clipboardItemData);
  await navigator.clipboard.write([clipboardItem]);
}

The setClipboard() function specifies a "text/plain" MIME type in the type constant, then specifies a clipboardItemData object with a single property — its key is the MIME type, and its value is the passed in text that we want to write to the clipboard. We then construct a new ClipboardItem object into which the clipboardItemData object is passed.

Finally, write() is called with await to write the data to the clipboard.

Write canvas contents to the clipboard

This example draws a blue rectangle to the canvas. You can click the rectangle to copy the content of the canvas into the clipboard as an image, and then select another element and paste in the content from the clipboard.

HTML

The HTML just defines our <canvas> element and the <div> element with id target where the canvas image will be pasted.

html
<canvas id="canvas" width="100" height="100"></canvas>

<div id="target">Paste here.</div>
js
const logElement = document.querySelector("#log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}

JavaScript

First we define an async function to copy a canvas to a blob. This wraps the old callback-style HTMLCanvasElement.toBlob() method into the more intuitive Promise based function.

js
// Async/await method replacing toBlob() callback
async function getBlobFromCanvas(canvas) {
  return new Promise((resolve, reject) => {
    canvas.toBlob((blob) => {
      if (blob) {
        resolve(blob);
      } else {
        reject(new Error("Canvas toBlob failed"));
      }
    });
  });
}

Next we set up our canvas and add an event listener for the click event.

When you click the blue rectangle, the canvas displaying the rectangle is copied into a blob, and then the blob is added to a ClipboardItem and then written to the clipboard.

js
const canvas = document.getElementById("canvas");

// Set up canvas
const ctx = canvas.getContext("2d");
ctx.fillStyle = "cornflowerblue";
ctx.fillRect(0, 0, 100, 100);

canvas.addEventListener("click", copyCanvasContentsToClipboard);
const target = document.getElementById("target");

async function copyCanvasContentsToClipboard() {
  // Copy canvas to blob
  try {
    const blob = await getBlobFromCanvas(canvas);
    // Create ClipboardItem with blob and it's type, and add to an array
    const data = [new ClipboardItem({ [blob.type]: blob })];
    // Write the data to the clipboard
    await navigator.clipboard.write(data);
    log("Copied");
  } catch (error) {
    log(error);
  }
}

Note that if you are fetching a less-common file type or a resource that you don't know the type in advance, you may want to use ClipboardItem.supports() to check if the file type is supported, and provide a good error message to the user in case it isn't.

We then define an event listener for paste events on then element where we want to display the clipboard contents as an image. The FileReader API allows us to read the blob using the readAsDataUrl method and create an <img> element with the canvas contents:

js
target.addEventListener("paste", (event) => {
  const items = (event.clipboardData || window.clipboardData).items;
  const blob = items[0].getAsFile();
  const reader = new FileReader();

  reader.addEventListener("load", (event) => {
    const img = new Image();
    img.src = event.target.result;
    target.appendChild(img);
  });

  reader.readAsDataURL(blob);
});

Result

The result is shown below. First click on the blue square, and then select the text "Paste here" and use your OS-specific keyboard combinations to paste from the clipboard (such as Ctrl+V on Windows).

Specifications

Specification
Clipboard API and events
# dom-clipboard-write

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
write
Chrome – Full support
Chrome 76 (Release date: 2019-07-30)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 127 (Release date: 2024-06-11)
footnote
footnote This method must be called within user gesture event handlers.
footnote Web extensions with the clipboardWrite permission in their manifest can write data without a user gesture.
Opera – Full support
Opera 63 (Release date: 2019-08-20)
footnote
footnote From version 93, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 93, the user must grant the clipboard-write permission.
Safari – Full support
Safari 13.1 (Release date: 2020-03-24)
footnote Full support
Chrome Android – Full support
Chrome Android 76 (Release date: 2019-07-30)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
Firefox for Android – Full support
Firefox for Android 127 (Release date: 2024-06-11)
footnote
footnote This method must be called within user gesture event handlers.
footnote Web extensions with the clipboardWrite permission in their manifest can write data without a user gesture.
Opera Android – Full support
Opera Android 54 (Release date: 2019-10-18)
footnote
footnote From version 73, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 73, the user must grant the clipboard-write permission.
Safari on iOS – Full support
Safari on iOS 13.4 (Release date: 2020-03-24)
footnote Full support
Samsung Internet – Full support
Samsung Internet 12 (Release date: 2020-06-19)
footnote
footnote From version 21.0, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 21.0, the user must grant the clipboard-write permission.
WebView Android – Full support
WebView Android 76 (Release date: 2019-07-30)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
WebView on iOS – Full support
WebView on iOS 13.4 (Release date: 2020-03-24)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also