Skip to content

Clipboard API Not Available

Quick summary

You called navigator.clipboard.writeText but clipboard is undefined — the page is not in a secure context or the browser lacks support.

Why this happens

  • Page served over HTTP (clipboard requires HTTPS or localhost)

  • Older browser without the async Clipboard API

  • Document not focused at the time of the call

Minimal example

✗ Broken code
navigator.clipboard.writeText('hello'); // TypeError: Cannot read properties of undefined (reading 'writeText')
✓ Fixed code
async function copy(text) {
  if (navigator.clipboard) {
    await navigator.clipboard.writeText(text);
  } else {
    const ta = document.createElement('textarea');
    ta.value = text; document.body.appendChild(ta); ta.select();
    document.execCommand('copy'); ta.remove();
  }
}

On HTTP, navigator.clipboard is undefined so writeText throws. Feature-detecting and falling back to execCommand('copy') handles older and non-secure contexts.

How to diagnose

  • Check typeof navigator.clipboard === 'undefined'

  • Verify location.protocol is https: or hostname is localhost

  • Ensure the call happens after a user gesture while the document is focused

How to fix

  • Feature-detect: if (navigator.clipboard) ... else fallback to execCommand

  • Serve over HTTPS so the Clipboard API is available

  • Use document.execCommand('copy') as a legacy fallback

How to prevent

  • Always feature-detect navigator.clipboard before using it

  • Provide a fallback for non-secure contexts and old browsers

Related Resources

Related Glossary

Related Lessons

  • HTTP

    Learn about clipboard API.

← Back to language