Clipboard.write()

The Clipboard method write() writes arbitrary data, such as images, to the clipboard. This can be used to implement cut and copy functionality.

Before you can write to the clipboard, you need to use the Permissions API to get the "clipboard-write" permission.

Note: Browser support for the asynchronous clipboard APIs is still in the process of being implemented. Be sure to check the compatibility table as well as Clipboard availability in Clipboard for more information.

Syntax

var promise = navigator.clipboard.write(dataTransfer)

Parameters

dataTransfer
A DataTransfer object containing data to be written to the clipboard.

Return value

A Promise which is resolved when the data has been written to the clipboard. The promise is rejected if the clipboard is unable to complete the clipboard access.

Example

This example function replaces the current contents of the clipboard with a specified string.

function setClipboard(text) {
  let data = new DataTransfer();

  data.items.add("text/plain", text);
  navigator.clipboard.write(data).then(function() {
    /* success */
  }, function() {
    /* failure */
  });
}

The code begins by creating a new DataTransfer object into which the text will be placed for sending to the clipboard. DataTransferItemList.add() is called to add the text to the DataTransfer, then write() is called, specifying both a fulfillment function and an error function.

Specifications

Specification Status Comment
Clipboard API and events
The definition of 'write()' in that specification.
Working Draft Initial definition.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support No ?631 2 ? No ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support No No ?631 2 No ? ?

1. Currently works exactly like writeText(), including the availability limitations currently imposed by Firefox.

2. From version 63: this feature is behind the dom.events.asyncClipboard.dataTransfer preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Document Tags and Contributors

Contributors to this page: Sheppy, samouss, ExE-Boss, jpmedley
Last updated by: Sheppy,