W3cubDocs

/Web APIs

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

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>".

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

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

Specifications

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
66["From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 107, the user must grant the clipboard-write permission."]
79["From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 107, the user must grant the clipboard-write permission."]
63["This method must be called within user gesture event handlers.", "Web extensions require the clipboardRead permission in manifest to read data. With this permission, the extension does not require a user interaction and does not display a paste prompt.", "A paste prompt is displayed when the clipboard is read. If the clipboard contains same-origin content, the prompt is suppressed."]
53["From version 93, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 93, the user must grant the clipboard-write permission."]
13.1This method must be called within user gesture event handlers.
66["From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 107, the user must grant the clipboard-write permission."]
63["This method must be called within user gesture event handlers.", "Web extensions require the clipboardRead permission in manifest to read data. With this permission, the extension does not require a user interaction and does not display a paste prompt.", "A paste prompt is displayed when the clipboard is read. If the clipboard contains same-origin content, the prompt is suppressed."]
47["From version 73, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 73, the user must grant the clipboard-write permission."]
13.4This method must be called within user gesture event handlers.
9.0["From version 21.0, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 21.0, the user must grant the clipboard-write permission."]
66["From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.", "Before version 107, the user must grant the clipboard-write permission."]
13.4This method must be called within user gesture event handlers.

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText