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(data)

Parameters

data
A ClipboardItem 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 ClipboardItem({ "text/plain": text });

  navigator.clipboard.write(data).then(function() {
    /* success */
  }, function() {
    /* failure */
  });
}

The code begins by creating a new ClipboardItem object into which the text will be placed for sending to the clipboard. The key of the object passed to the ClipboardItem constructor indicates the content type, the value indicates the content. The content could be a text or even a Blob (e.g. for copying images to the clipboard). Then write() is called, specifying both a fulfillment function and an error function.

Example of copying canvas contents to the clipboard

function copyCanvasContentsToClipboard(canvas, onDone, onError) {
  canvas.toBlob(function (blob) {
    let data = new ClipboardItem({ [blob.type]: blob });

    navigator.clipboard.write(data).then(function () {
      onDone();
    }, function (err) {
      onError(err);
    })
  });
}

Specifications

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

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
writeChrome Full support 76Edge Full support 79Firefox Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Currently works exactly like writeText(), including the availability limitations currently imposed by Firefox.
Disabled 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.
IE No support NoOpera Full support 63Safari Full support 13.1WebView Android Full support 76Chrome Android Full support 76Firefox Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Currently works exactly like writeText(), including the availability limitations currently imposed by Firefox.
Disabled 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.
Opera Android Full support 54Safari iOS Full support 13.4Samsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.