navigator.clipboard.writeText()
await navigator.clipboard.writeText(text)
Writes text to the clipboard and returns a Promise. It works only in a secure context (HTTPS or localhost) and browsers usually allow it only as a reaction to a user action (a click).
The example works with the HTML shown above the code.
Parameters
- text – the text to copy.
Return value
A Promise; a refusal rejects it with a NotAllowedError.
Examples
console.log(window.isSecureContext, typeof navigator.clipboard?.writeText);
const copy = async (text) => {
try {
await navigator.clipboard.writeText(text);
return 'copied';
} catch (e) {
return 'cannot copy: ' + e.name;
}
};
console.log(typeof copy);
Output:
true function function
Without a user click (run from code) browsers usually refuse the write – the result differs by browser and settings:
try {
await navigator.clipboard.writeText('https://www.garth.cz/');
console.log('copied');
} catch (e) {
console.log('refused:', e.name);
}
Output (for example):
refused: NotAllowedError
Things to watch out for
- Call it directly in the click handler (
click), not after a long wait – the browser would lose the connection with the click. - On a page served over HTTP (not localhost)
navigator.clipboarddoes not exist – for older situations only the deprecateddocument.execCommand('copy')remains. - Reading the clipboard (
readText()) requires the permission of the user.
Browser support
Baseline – widely available: in all major browsers since March 2020, considered widely available since September 2022. Since version: Chrome 66, Firefox 63, Safari 13.1; not in Node.js (a browser API). As of October 2026 (MDN browser-compat-data).
Related:
click event
Promise
try...catch