FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Browser APIs & BOM

Advanced

Clipboard API

The Clipboard API lets pages read or write clipboard data, but only in secure, permission-aware contexts.

await navigator.clipboard.writeText("Copied text");

const text = await navigator.clipboard.readText();

Browser APIs & BOM

Clipboard access needs user trust and graceful fallback.

navigator.clipboard is available in secure contexts and is usually tied to user gestures and browser permissions.

Writing text is common for copy buttons. Reading the clipboard is more sensitive and should be requested only when clearly needed.

Clipboard code should always provide feedback and fallback because support and permission behavior vary.

writeText

Writes plain text to the clipboard.

readText

Reads plain text with permission constraints.

Secure context

Clipboard APIs generally require HTTPS.

User gesture

Browsers often require a click or similar action.

Examples

Browser APIs should be used with capability checks, permissions and fallbacks.

Copy with feedback and fallback

try {
  await navigator.clipboard.writeText(value);
  status.textContent = "Copied.";
} catch {
  status.textContent = "Select and copy manually.";
}

Reading clipboard unexpectedly

const text = await navigator.clipboard.readText();
sendToServer(text);

Code patterns

Reusable examples for quick reference.

These examples focus on practical browser APIs: URL state, storage, cookies, clipboard, permissions, observers and offline-capable platform tools.

Check support

Feature-detect clipboard access.

if (!navigator.clipboard) {
  showManualCopyFallback();
}

Write text

Use inside a click handler.

await navigator.clipboard.writeText(value);

Handle failure

Permissions can reject.

try {
  await navigator.clipboard.writeText(value);
} catch {
  showManualCopyFallback();
}

Avoid surprise reads

Reading clipboard is sensitive.

// Ask only after a clear user action.
const text = await navigator.clipboard.readText();

Rules that matter

Treat the browser as a platform.

Browser APIs are powerful because they touch privacy, navigation, storage, permissions and performance. Use them deliberately.

Feature-detect clipboard

Support depends on browser and context.

Use HTTPS

Clipboard APIs require secure contexts.

Prefer writeText for copy buttons

It is the common safe use case.

Ask before reading

Clipboard contents can be private.

Show feedback

Users need to know whether copying worked.

Provide fallback

Manual copy should still be possible.

Production thinking

A strong browser API layer makes features feel integrated with the platform instead of bolted onto the page.

Why does this matter?

Clipboard interactions are small but trust-sensitive; users should never be surprised by them.

Accessibility

Copy buttons should be real buttons and status feedback should be readable text.

Production note

Production clipboard code needs permission handling, fallbacks and no surprise reads.

SEO note

Clipboard APIs do not affect SEO directly, but trustworthy interactions affect user confidence.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Change the success message.
  • Select the input text when fallback is needed.
  • Explain why readText is more sensitive than writeText.

Practice assignment

Do this before moving to the next topic.

  1. Check clipboard support.
  2. Write text after a button click.
  3. Show success or fallback feedback.

Try it yourself

Build clipboard fallback feedback

Live preview

Self-check

Before you continue, prove that you understand Clipboard API.

Advanced

If you can explain support, permissions, fallback and cleanup, you are ready to use the API responsibly.

  1. Can you explain Clipboard API?
  2. Can you explain secure context?
  3. Can you explain user gesture requirements?
  4. Can you explain writeText?
  5. Can you explain why readText is sensitive?

Senior audit upgrade

Extra production context for Clipboard API.

Compatibility note: Clipboard

Last reviewed: 11 June 2026. Clipboard access depends on secure contexts, user gestures and permissions. Always design a manual fallback.

Privacy and fallback checklist

  • Ask for permission only when the user understands why.
  • Handle denied, unsupported and unavailable states.
  • Avoid storing sensitive data in browser storage by default.
  • Do not make critical content depend on a permission prompt.