Clipboard: writeText() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2020.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The writeText() method of the Clipboard interface writes the specified text to the system clipboard, returning a Promise that is resolved once the system clipboard has been updated.

Syntax

js
writeText(newClipText)

Parameters

newClipText

The string to be written to the clipboard.

Return value

A Promise that is resolved once the clipboard's contents have been updated.

Exceptions

NotAllowedError DOMException

Thrown if writing to the clipboard is not allowed.

Security considerations

Writing to the clipboard can only be done in a secure context.

Additional security requirements are covered in the Security consideration section of the API overview topic.

Examples

This example sets the clipboard's contents to the string "<empty clipboard>".

js
button.addEventListener("click", () => writeClipboardText("<empty clipboard>"));

async function writeClipboardText(text) {
  try {
    await navigator.clipboard.writeText(text);
  } catch (error) {
    console.error(error.message);
  }
}

Specifications

Specification
Clipboard API and events
# dom-clipboard-writetext

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
writeText
Chrome – Full support
Chrome 66 (Release date: 2018-04-17)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
Firefox – Full support
Firefox 63 (Release date: 2018-10-23)
footnote
footnote This method must be called within user gesture event handlers.
footnote Web extensions with the clipboardWrite permission in their manifest can write data without a user gesture.
Opera – Full support
Opera 53 (Release date: 2018-05-10)
footnote
footnote From version 93, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 93, the user must grant the clipboard-write permission.
Safari – Full support
Safari 13.1 (Release date: 2020-03-24)
footnote
footnote This method must be called within user gesture event handlers.
Chrome Android – Full support
Chrome Android 66 (Release date: 2018-04-17)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
Firefox for Android – Full support
Firefox for Android 63 (Release date: 2018-10-23)
footnote
footnote This method must be called within user gesture event handlers.
footnote Web extensions with the clipboardWrite permission in their manifest can write data without a user gesture.
Opera Android – Full support
Opera Android 47 (Release date: 2018-07-23)
footnote
footnote From version 73, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 73, the user must grant the clipboard-write permission.
Safari on iOS – Full support
Safari on iOS 13.4 (Release date: 2020-03-24)
footnote
footnote This method must be called within user gesture event handlers.
Samsung Internet – Full support
Samsung Internet 9 (Release date: 2018-09-15)
footnote
footnote From version 21.0, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 21.0, the user must grant the clipboard-write permission.
WebView Android – Full support
WebView Android 66 (Release date: 2018-04-17)
footnote
footnote From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
footnote Before version 107, the user must grant the clipboard-write permission.
WebView on iOS – Full support
WebView on iOS 13.4 (Release date: 2020-03-24)
footnote
footnote This method must be called within user gesture event handlers.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also