Copy button?

Hi all, how do you code for the copy button that you see on some sites please? The one where, when you click on the button, it highlights all the text below and copies it to the users clipboard.

There’s an old SitePoint article here that may be of use and another article here.

Suffice to say that copying to the clipboard is not straight forward or cross browser compatible it seems without using flash.

It is definitely not cross browser compatible if you use flash as no all modern browsers support flash any more.

There are security issues with allowing the browser access to the clipboard which most browsers have now blocked (if they ever allowed it in the first place)

Yes, I meant to qualify that :smile:

Thanks for the help. Is there any way of doing it, that is compatible with all browsers?

No. There are security issues with access to the clipboard from web pages and so most browsers have never provided such access and those that have (or where a plugin was able to be used to provide it) the browsers are disabling that access - for example Firefox recently completely disabled flash (not specifically to block its use to access the clipboard but because of other security issues that it also introduces into any browser running it).

What you can do is to select all of the content you want them to be able to copy and then they can just Ctrl-C to copy it to the clipboard. Just one extra key combo.

Interesting.
Over at meta @riking pointed out that there is a difference between script-initiated and user-initiated

https://meta.discourse.org/t/it-says-copied-to-clipboard-but-there-is-nothing-in-my-clipboard/31244/9?u=mittineague

Thanks for the comments. I was almost certain I had seen these copy buttons somewhere? Perhaps one of the phpbb forums?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.