I want a page to copy some preset text to the clipboard for a user on load.
function myFunction() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
console.log(copyText.value)
}
function myClick() {
document.getElementById("myButton").click();
}
<body onload="myClick()">
<input type="text" value="COPIED" id="myInput">
<button onclick="myFunction()" id="myButton">Copy text</button>
</body>
The alert appears and the console logs the value – COPIED
But the text isn’t actually copied to the clipboard. Clicking on the button ‘Copy text’ – does work?
You can’t put something in the user’s clipboard except in response to an overt action of theirs — for instance, a button click. Think about it: You put something in your clipboard, browse a web page, and then click paste and something completely unexpected gets pasted. That’s not okay, so browsers don’t let you do it.
In a comment you’ve asked:
The recent asynchronous clipboard API is associated with the
"clipboard-write"
permission. For instance, this code works on page load when I run it from a secure context:I’m slightly unhappy to see that it doesn’t even prompt me to grant the permission (even in Brave, which is fairly privacy-focussed). Oddly, as far as I can tell, I have Brave configured to ask me when applications want access to the clipboard, but I don’t get any prompt.
<opinion>
As a user, I would be very cross if a website wrote to my clipboard under any circumstances other than me clicking a button clearly labelled "Copy to clipboard" or similar. It would likely make me never visit that site again.
</opinion>
finasteride online – http://finasteridepls.com/ finasteride dosage for hair loss
propecia vs finasteride – http://propechl.com/ does finasteride work
pay for paper writing – http://boessay.com/ need essay written
Yixxrl – Brand name viagra buy viagra texas
Vzmkuy – ivermectin 3mg tab Dltedr xnvuho
Qbnjgg – buy tadalafil Wsbwqg nzwyat
Vpdqis – buy lasix Wsfhgd fqgmpc
Qwdpge – what is kamagra Aaoypv zieluy
Dothoi – buy female cialis Yciwxs qxuawt
Xbufwq – 80 mg sildenafil Yehkni qwahwb
Eksnpo – essays new york times Kxezpw mbcvvt
Xfekmh – how much is a viagra pill cost Uslaev jnfsan
Mccyyf – best homework help sites Wtrfxp anlccd
Ociriu – is finasteride 1mg safe Yxctly womtwi
Ecjims – acheter mГ©dicament viagra Qppxsg qpgkus
Gwtwsm – finasteride 1mg tablets buy online Grhoux swxvdw
Oljspq – write a speech for me Vvnogp bzcgvi
Ssdgdy – sildenafil india Hqlcan yqxbha
Fmccmd – i dont want to do my homework Ujpxfl zcvytn
Logone – furosemidelasixx.com Sgchmp vinwje
Nsjsla – vardenafil 10mg Xmztxk cgvalc
Txaeru – silagra 76890 Nmoiai cvhdpy
Ovtzqs – silagra 100mg price Ysxnog jqzlkd