navigator.clipboard returns a Clipboard object — the modern entry point to copy and paste from JavaScript. Learn secure-context rules, writeText(), readText(), permissions, five examples, and try-it labs.
01
Kind
Read-only property
02
Returns
Clipboard
03
Baseline
Widely available
04
Context
Secure (HTTPS)
05
Copy
writeText()
06
Paste
readText()
Fundamentals
Introduction
Almost every app needs “Copy” and “Paste.” The Clipboard API gives your page a clean, promise-based way to talk to the system clipboard — without the old document.execCommand("copy") hacks.
Your entry point is navigator.clipboard. That property is read-only and returns a Clipboard object with async methods such as writeText() and readText().
💡
Secure context required
MDN: the Clipboard API works in secure contexts (HTTPS / localhost). Prefer calling write/read from a user gesture (button click) for reliable permission behavior.
Concept
Understanding the clipboard Property
Think of navigator.clipboard as a handle to the system clipboard for this page. You do not assign to it — you call methods on the object it returns.
writeText(text) — copy a string to the clipboard.
readText() — read text from the clipboard (may need permission).
write() / read() — richer data (for example images) via ClipboardItem.
All methods return Promises and reject if access is denied.
Foundation
📝 Syntax
General form of the property:
JavaScript
navigator.clipboard
Value
A Clipboard object used to access the system clipboard.
Common patterns
JavaScript
// Copy (from a click handler):
await navigator.clipboard.writeText("Hello CodeToFun");
// Paste text:
const text = await navigator.clipboard.readText();
console.log(text);
// Feature-detect:
if (navigator.clipboard && window.isSecureContext) {
// safe to use Clipboard API
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
Get Clipboard
navigator.clipboard
Copy text
await navigator.clipboard.writeText(str)
Read text
await navigator.clipboard.readText()
Secure page?
window.isSecureContext
Detect API
!!navigator.clipboard
Handle deny
try / catch around awaits
Snapshot
🔍 At a Glance
Four facts to remember about navigator.clipboard.
Returns
Clipboard
API entry
Baseline
widely
Modern browsers
HTTPS
required
Secure context
Style
async
Promises
Compare
📋 Clipboard API vs execCommand
navigator.clipboard
document.execCommand
Style
Async Promises
Old sync boolean
Status
Modern recommended
Legacy / discouraged
Text copy
writeText()
Select + "copy"
Permissions
Clearer model
Hacky / inconsistent
Best for
New apps
Legacy only
Hands-On
Examples Gallery
Examples follow MDN Navigator.clipboard / Clipboard API patterns. Prefer Try It Yourself for interactive copy/paste. Use View Output for expected messaging.
Serve clipboard features over HTTPS (or localhost during development).
📈 Practical Patterns
Copy text, read text, and a reusable helper.
Example 3 — writeText() Copy
Copy a string from a button click.
JavaScript
async function copyCode() {
if (!navigator.clipboard) {
return "clipboard unavailable";
}
await navigator.clipboard.writeText("const x = 42;");
return "copied to clipboard";
}
// Call from a button click in Try It
navigator.clipboard is Baseline Widely available across modern browsers (since about March 2020 per MDN). Always use a secure context. Some richer Clipboard methods still vary by browser.
✓ Baseline · Widely available
Navigator.clipboard
Prefer writeText/readText for text copy-paste. Handle permission errors and serve over HTTPS.
UniversalWidely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
clipboardExcellent
Bottom line: Use navigator.clipboard for modern copy/paste. Keep a fallback message when the context is insecure or access is denied.
Wrap Up
Conclusion
navigator.clipboard is the modern Clipboard API entry point. Detect it, require HTTPS, call writeText / readText from user actions, and handle permission errors gracefully.
Modern Clipboard API entry — async copy and paste.
5
Core concepts
📋01
Returns
Clipboard
API
📋02
Copy
writeText
Write
📄03
Paste
readText
Read
🔒04
HTTPS
secure context
Security
⚡05
Async
Promises
Style
❓ Frequently Asked Questions
It is a read-only Navigator property that returns a Clipboard object — the entry point to the Clipboard API for reading and writing the system clipboard (copy, cut, and paste features).
Yes. The Clipboard API is available only in secure contexts (HTTPS or localhost) in supporting browsers.
Call await navigator.clipboard.writeText("hello"). Prefer running it from a user gesture such as a button click so browsers are more likely to allow the write.
Call await navigator.clipboard.readText(). Reading often requires permission and may prompt the user. Handle promise rejections when access is denied.
No. The property is read-only. You use methods on the returned Clipboard object such as writeText() and readText().
No. MDN marks navigator.clipboard as Baseline Widely available. Some advanced methods (for example reading images via read()) can still vary by browser.
Did you know?
Never drop clipboard text straight into innerHTML. Treat pasted content as untrusted input — use textContent or sanitize before rendering HTML.