Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Clipboard — asynchronous clipboard access

Clipboard exposes the system clipboard through navigator.clipboard; operations are asynchronous and subject to security restrictions.

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

navigator.clipboard.readText(): Promise<string>
navigator.clipboard.writeText(text): Promise<undefined>
navigator.clipboard.read(): Promise<ClipboardItem[]>
navigator.clipboard.write(items): Promise<undefined>

Clipboard, accessed through navigator.clipboard, is a Web API interface, not ECMAScript. readText() returns text, writeText() writes text; read() and write() handle ClipboardItem objects. Methods return promises.

A secure context and document focus are required. Permissions and user activation can restrict access; clicking does not guarantee success. Handle rejections, including NotAllowedError. Detect features; the W3C draft does not guarantee browser support, nor does Node 24 prove it.

This differs from synchronous copy, cut, paste events using event.clipboardData. Clipboard contents may include secrets: avoid unsolicited reads. The example overwrites the clipboard only after clicking.

Examples

const button = document.createElement('button');
const status = document.createElement('p');
button.textContent = 'Copy CKTIS';
status.setAttribute('role', 'status');
document.body.append(button, status);

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

  button.disabled = true;
  try {
    await navigator.clipboard.writeText('CKTIS');
    status.textContent = 'Copied: CKTIS';
  } catch (error) {
    status.textContent = `Copy failed: ${error.name}`;
  } finally {
    button.disabled = false;
  }
});

Result

On an allowed, successful click: Copied: CKTIS
Clipboard text: CKTIS

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