U leest de Engelstalige versie van deze inhoud, omdat er nog geen vertaling voor deze taal beschikbaar is. Help ons dit artikel te vertalen!
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
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
write | Chrome Full support 76 | Edge No support No | Firefox
Full support
63
| IE No support No | Opera Full support 63 | Safari No support No | WebView Android Full support 76 | Chrome Android Full support 76 | Firefox Android
Full support
63
| Opera Android No support No | Safari iOS No support No | Samsung Internet Android ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.