Tapas Adhikary
Educator @tapaScript | Founder CreoWis & ReactPlay - Writer - YouTuber - Open Source
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