1. Copy button

Copy a code snippet string.

Copy

Click Copy and the status updates.

await navigator.clipboard.writeText(text);

2. Copy link pattern

Copy current URL for share buttons.

URL

Uses page URL in secure context.

await navigator.clipboard.writeText(window.location.href);

3. Fallback textarea

When Clipboard API unavailable.

Note

Legacy: select textarea and document.execCommand('copy') inside click handler.

Safari older versions may need fallback.

textarea.select();
document.execCommand('copy');

Frequently asked questions

Clipboard requires HTTPS?
writeText needs secure context (localhost counts as secure.
Copy without click?
Browsers require user activation, so you cannot copy on page load alone.
Paste from clipboard?
readText needs permission prompt. Use sparingly.