JavaScript copy to clipboard (navigator.clipboard.writeText, secure context)

Copy text to the clipboard with navigator.clipboard.writeText, secure contexts, NotAllowedError, readText, and why execCommand is legacy. Node uses a stub clipboard in examples so snippets run; browsers need HTTPS and user activation.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript copy to clipboard (navigator.clipboard.writeText, secure context)

Modern browsers expose the Clipboard API through navigator.clipboard.writeText() (and related read APIs) whenever the page runs in a secure context—HTTPS or localhost. Real-world flows await the returned Promise, surface NotAllowedError when the user has not granted permission or a gesture is missing, and usually trigger the copy from a click or keyboard handler rather than on page load (MDN: Clipboard API, MDN: writeText). When the string you copy is assembled from table or list data, companion guides on copying arrays in JavaScript and converting strings to arrays are often useful next steps.

The first runnable example uses globalThis.navigator?.clipboard ?? { … } so it resolves in Node.js as well as the browser; on HTTPS pages you normally call navigator.clipboard directly.

Tested on: Node.js v20.18.2 (stubbed clipboard in snippets). In real browsers, expect secure-context and permission behavior as described in each section.


Quick reference

writeText is for pasting plain text; readText is stricter; always check isSecureContext and navigator.clipboard before calling.

Need API
Write plain text await navigator.clipboard.writeText(text)
Read plain text await navigator.clipboard.readText() (stricter permissions)
Know if APIs may run window.isSecureContext plus a feature check on navigator.clipboard

writeText — JavaScript copy string to clipboard

The usual javascript copy string to clipboard path is Clipboard.prototype.writeText: pass a string, await the Promise, and branch on success vs NotAllowedError or SecurityError. This pattern is what most copy to clipboard javascript tutorials mean by an async clipboard write.

javascript
(async () => {
  const clipboard =
    globalThis.navigator?.clipboard ??
    { writeText: () => Promise.resolve() };
  try {
    await clipboard.writeText("Hello, world!");
    console.log("Text copied to clipboard");
  } catch (error) {
    console.log("Failed:", error.name);
  }
})();
Output

You should see one line: Text copied to clipboard (under Node with the stub; failures would print Failed: …).


readText — JavaScript clipboard reads

Reading the javascript clipboard with readText() is more sensitive than writing: browsers may require stronger permissions or a user gesture, and the returned string reflects whatever the user last copied. The snippet below uses a stub clipboard object so copy clipboard javascript readers can see the Promise shape without a live browser clipboard.

javascript
const clipboard = {
  readText() {
    return Promise.resolve("pasted line");
  },
};
clipboard.readText().then((text) => console.log("From clipboard:", text));
Output

You should see one line: From clipboard: pasted line.

In production, replace clipboard with navigator.clipboard; the string you read depends on what the user last copied.


Secure context check — guard for copy to clipboard JS

Before you call js copy to clipboard APIs, guard on secure context and API presence: window.isSecureContext should be true on HTTPS, and navigator.clipboard should exist. This avoids half of “copy to clipboard javascript not working” reports that are really environment issues (HTTP, embedded iframe policy, or non-browser runtimes).

javascript
console.log(
  Boolean(globalThis.isSecureContext),
  typeof globalThis.navigator?.clipboard?.writeText
);
Output

You should see one line like: false undefined when run in Node (no browser navigator). In a page served over HTTPS, isSecureContext is typically true and writeText is a function.


NotAllowedError when copy clipboard JavaScript is blocked

When copy clipboard javascript is blocked—no recent click, policy denial, or non-secure page—writeText rejects with NotAllowedError. Catching by DOMException name keeps copy clipboard js UX predictable: show a message or offer a manual “select and copy” fallback.

javascript
(async () => {
  const clipboard = {
    writeText() {
      return Promise.reject(new DOMException("Denied", "NotAllowedError"));
    },
  };
  try {
    await clipboard.writeText("x");
  } catch (e) {
    console.log(e.name);
  }
})();
Output

You should see one line: NotAllowedError.


Summary

navigator.clipboard.writeText is the modern plain-text write path—await it, run from user gesture on HTTPS, and catch NotAllowedError; avoid new execCommand('copy') unless legacy forces it.

  • Prefer await navigator.clipboard.writeText(text) for javascript copy to clipboard of plain strings in secure contexts.
  • readText() covers reading clipboard text in JavaScript but is stricter than writeText.
  • Check isSecureContext and navigator.clipboard before calling APIs in copy to clipboard js code paths.
  • Handle NotAllowedError when user activation or permissions block the write.
  • Avoid new work on document.execCommand('copy') unless legacy support forces it.

References

MDN references for the Clipboard API, secure contexts, and the exact signatures behind javascript copy string to clipboard and read flows.


Frequently Asked Questions

1. Why does javascript copy to clipboard fail on http://?

The Clipboard API is limited to secure contexts (HTTPS or localhost in modern browsers). Check window.isSecureContext before calling navigator.clipboard.

2. copy to clipboard javascript without a user click?

Browsers expect transient user activation for many clipboard writes. Trigger copy from a click or keyboard handler rather than on page load, or handle NotAllowedError with a visible message.

3. javascript copy string to clipboard with await?

writeText returns a Promise. Use await inside an async function or .then/.catch so failures surface as NotAllowedError or SecurityError instead of silent rejects.

4. js copy to clipboard readText vs writeText permissions?

readText is more restricted than writeText because it reads user data; expect prompts or denied errors unless permissions and user gesture requirements are met.

5. Should I still use document.execCommand for copy clipboard js?

execCommand is deprecated. Prefer navigator.clipboard with a secure context; keep a legacy fallback only if you must support very old browsers.

6. clipboard js in an iframe?

Parent pages can restrict iframes with Permissions-Policy (clipboard-write / clipboard-read). The feature may be undefined or denied inside the frame.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …