On ramrom.io, the little copy button next to a download link is one of the most clicked things on the site. A visitor resolves a video, gets a link, and wants it on their clipboard with one tap. It sounds like a five-minute job. It mostly is — but only if you handle the two ways browsers actually do it.
The modern way is navigator.clipboard.writeText(). It is async, it returns a promise, and it is clean:
async function copyText(text) {
await navigator.clipboard.writeText(text);
showCopied();
}
That is the whole happy path. But it comes with conditions that are easy to miss. The Clipboard API only works in a secure context, which in practice means HTTPS or localhost. It also needs a user gesture — a click or a tap. If you call it from a timer, or from a promise chain that has drifted away from the click, the browser will refuse. And in some embedded contexts, like an iframe without the right permissions, it is simply not there at all.
So the first thing my copy function does is check whether the API exists:
if (navigator.clipboard && window.isSecureContext) {
// modern path
}
If it is not available, I fall back to the old way: a hidden textarea and document.execCommand('copy'). It is deprecated, and the docs will tell you so, but it is still the thing that works in older browsers and in the awkward corners where the new API does not. The trick is doing it without flashing anything on screen or stealing the user's scroll position:
function fallbackCopy(text) {
const ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.position = 'fixed';
ta.style.top = '0';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.focus();
ta.select();
// iOS needs an explicit selection range
ta.setSelectionRange(0, text.length);
const ok = document.execCommand('copy');
document.body.removeChild(ta);
return ok;
}
A few details matter here. The textarea must be readonly, not disabled, or the selection will not work. Positioning it fixed at the top with zero opacity keeps it out of sight without removing it from the layout, which some browsers need to allow the selection. And setSelectionRange after select() is the line that makes it work on iOS Safari, where select() alone is not always enough.
Both paths can fail, so the function has to be honest about that. The modern call rejects its promise if permission is denied; the fallback returns false from execCommand. Either way, I show the same message: the copy did not work, and here is the link in a text box you can select by hand. A silent failure is the worst outcome — the visitor taps the button, assumes the link is copied, pastes somewhere else, and gets nothing.
The feedback matters just as much as the copy itself. I change the button label to "Copied" for a couple of seconds, then put it back. No toast library, no animation framework, just a label swap and a timeout. On a tool page, the visitor is in a hurry; the confirmation has to be visible in the same place their eyes already are.
One more thing I learned the hard way: do not wrap the copy call in a setTimeout or wait for an animation to finish first. The user gesture has a short shelf life. If you delay the call, the browser treats it as no longer part of the click and the Clipboard API refuses. Do the copy first, then the feedback.
The whole thing is about twenty lines, no dependency, and it works on desktop Chrome, Android Chrome, and the older browsers my visitors still turn up with. Sometimes the boring, dependency-free version is the right one.
Top comments (0)