Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Clipboard.write — asynchronous clipboard writing

Clipboard.write writes ClipboardItem objects to the system clipboard. It accepts alternative MIME representations, but requires a secure context and browser authorization.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

await navigator.clipboard.write([new ClipboardItem({ "text/plain": new Blob([text], { type: "text/plain" }) })]);

Clipboard.write, called as navigator.clipboard.write(items), accepts ClipboardItem objects. One item can contain multiple MIME representations of the same content. writeText(text) is simpler for text alone. The returned promise fulfills with undefined after writing; denied access can cause NotAllowedError.

This is a Web API, not ECMAScript; Node 24 does not establish browser support. Detect the API and test target browsers. Use a secure context: HTTPS or a trustworthy local origin such as localhost. HTTPS alone does not guarantee a secure context when the page is embedded beneath an insecure ancestor; check window.isSecureContext.

Write directly from a click in a focused document and handle failures; permission policy varies by browser. Do not confuse multiple representations with multiple items: a platform may retain only the first item. The example overwrites the clipboard after an intentional user click.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Clipboard.write</title>
<button id="copy" type="button">Copy: Hello, clipboard!</button>
<output id="status" aria-live="polite"></output>
<script>
const button = document.querySelector('#copy');
const status = document.querySelector('#status');

button.addEventListener('click', async () => {
  if (!window.isSecureContext ||
      typeof navigator.clipboard?.write !== 'function' ||
      typeof ClipboardItem !== 'function') {
    status.textContent = 'Unavailable';
    return;
  }

  try {
    const blob = new Blob(['Hello, clipboard!'], { type: 'text/plain' });
    const item = new ClipboardItem({ 'text/plain': blob });
    await navigator.clipboard.write([item]);
    status.textContent = 'Copied';
  } catch (error) {
    status.textContent = `Failed: ${error.name}`;
  }
});
</script>

Result

The status is initially empty. After the user clicks the button, it displays `Copied` only if the clipboard write succeeds. If the context is insecure or the required API is unavailable, it displays `Unavailable`. If an error occurs while preparing or writing the clipboard item, it displays `Failed: ` followed by the error name.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources