W3cubDocs

/DOM

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 compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support No ? 63
Disabled
63
Disabled
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.
? No ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support No No ? 63
Disabled
63
Disabled
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.
No ? ?

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write