Tapas Adhikary
Educator @tapaScript | Founder CreoWis & ReactPlay - Writer - YouTuber - Open Source
Copy-Paste is a fundamental need in application usage. Our users need this feature in the applications and websites as much as we developers need it while programming π. In this article, we will learn the JavaScript APIs available to help with copy-...
blog.greenroots.info2 min read
Even I use Clipboard API but with the support for fallback like below,
(FB/Instagram browser doesn't support just clipboard API)
async function copyLink(){
try{
await navigator.clipboard.writeText(link);
console.log("Link Copied");
}
catch(e){
copyToClipbordFallback(link);
console.log("Link Copied");
}
}
function copyToClipbordFallback(text){
const textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
textArea.remove();
}
so this fallback is mainly for FB/IG Browsers and other old browsers
Glad it's being suggested to use the Clipboard API and not the now deprecated one that you sometimes see floating around on "JavaScript Tips" on Twitter.
For reference:
execCommand('copy')
don't use that
boystrong1001 eddy
Positive lifestyle
Very good article, thanks