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.
var promise = navigator.clipboard.write(dataTransfer)
dataTransfer
DataTransfer
object containing data to be written to the clipboard.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.
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.
Specification | Status | Comment |
---|---|---|
Clipboard API and events The definition of 'write()' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | No | ? | 63
|
? | No | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | No | No | ? | 63
|
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