64

With flash on the way out in many environments (iPhone, Android, IE10, etc...), is there any new solution forthcoming in any browsers that will allow a safe copy of information to the clipboard without flash installed?

I've been using ZeroClipboard so far, but I'm worried about more viewers that don't have flash and this functionality is going to be broken and I'd love to not depend on Flash whenever possible.

Bartosz Firyn
  • 2,664
  • 2
  • 21
  • 18
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 3
    There's the [Clipboard API](http://www.w3.org/TR/clipboard-apis/) from the [W3C WebApps WG](http://www.w3.org/2008/webapps/) but I don't believe any of that has been implemented. There's a ton of security implications associated with giving a webapp access to the user's clipboard. – steveax May 24 '12 at 03:27
  • 2
    @steveax - I've seen that before. Is it implemented anywhere? I also understand the security implications, but Flash managed to find a usable way to surface some functionality by requiring a user action. – jfriend00 May 24 '12 at 04:27
  • https://cbabhusal.wordpress.com/2015/11/15/ror-copy-some-arbitrary-text-into-users-clipboard/ – Shiva Nov 16 '15 at 05:15

6 Answers6

20

The reasoning is that automatic copying to clipboard can be very dangerous, thus most browsers (except IE)* make it difficult unless you use flash.

Much like your ZeroClipboard, there is Clipboard LMCButton which also uses a small flash script running in the background.

A common solution would be to do this:

 function copyToClipboard (text) {
     window.prompt ("Copy to clipboard: Ctrl+C, Enter", text);
 }

Which I found from Jarek Milewski when some one else asked the question here

*Yes I found one solution for IE, however does not work in most modern browsers, check here.

Update:

According to this: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand Firefox 41+, Chrome 42+, and IE 9+ support the copy command with execCommand. For firefox and chrome it will only work if triggered by a user action such as a click, and for IE it will give the user a warning dialog asking them for permission to copy to the clipboard.

Beau Bouchard
  • 835
  • 11
  • 28
  • 7
    If we can do it through Flash, then why it would be a security hole to be able to do it through HTML5? Reading FROM clipboard definitely is, but calling writing TO clipboard a security hole seems a bit far-fetched since it requires the user to actually paste it into something dangerous. – freeall Aug 21 '12 at 14:04
  • 5
    I will agree that copying from the clipboard is a security issue, but I don't see writing to really is. After all, it does require the user to paste it somewhere and then probably perform another action to actually send/execute it. I don't see as a much bigger threat than me writing a text on my website that says "Please copy 'rm /* --recursive' (you know what I mean), and ask the user to execute it. – freeall Aug 31 '12 at 14:21
  • 21
    Just to finish up, then... :) If we as web developers are able to do something in Flash then I don't see why we shouldn't allow ourselves to do it in HTML(5) too. If it's a hole it's already there in probably over 95% of all browsers so we aren't opening up a new hole, and those who want to exploit it are still able to do it, just only in Flash. – freeall Sep 04 '12 at 10:59
  • 9
    If I had previously cut important information, and any website can overwrite my clipboard, that would result in my loosing information. There's the security hole. – WhyNotHugo Jul 16 '13 at 17:53
  • 4
    @freeall Lots of users simply don't install flash in the first place and avoid those security holes. Making it standard HTML5 would impose the issue onto everyone, and browser vendors actually care enough so as NOT to add security holes. – WhyNotHugo Jul 16 '13 at 17:54
  • Why wouldn't a browser allow the script to copy data into clipboard only during a "native" (triggered by the user, not programmatically) click event and then maybe show the copied contents in a similar fashion to how hovered link URLs are displayed (at the bottom of the window)? Just a thought. – Ignas Oct 07 '13 at 14:10
  • Thank you so much..its working..is there any way to preselect the text? – Dibish Dec 13 '13 at 11:09
  • 1
    @Ignas Because even that can be unwanted behavior. My biggest gripe is news sites and blogs that auto-append their URL whenever I try to copy a segment of text. – geofflee Jun 18 '14 at 23:49
  • @geofflee, yeah, that is annoying:) – Ignas Jun 19 '14 at 10:51
  • @freeall that's a fallacy, it's not because they made an error in the past that we need to make the same error again. The proposed solution works for all browser and the user clearly know what happens. – Guillaume Massé Aug 22 '14 at 16:12
  • @GuillaumeMassé if it was truly a security risk it would not be in Flash. This is a two years old thread and it actually seems that there is something HTML5 to allow to manipulate the clipboard, http://datatables.net/blog/2014-01-31#Clipboard-API – freeall Aug 25 '14 at 12:23
  • @freeall the link you provided uses ZeroClipboard project. – Tricky Aug 28 '14 at 08:30
  • @RichardMartin it mentions how it would work with the Clipboard API. Parts of that is part of a bug report filed by James Greene of ZeroClipboard, but it would work in html5. But from what I can see no browsers have implemented it yet. – freeall Aug 29 '14 at 12:55
  • @RichardMartin actually, I think maybe Firefox have something. At least they have a ClipboardEvent. – freeall Aug 29 '14 at 13:00
  • @freeall sorry i mis-read the link, this one shows implementation status across browsers which seems promising: http://caniuse.com/#feat=clipboard – Tricky Aug 29 '14 at 13:19
  • @RichardMartin that looks good. However, I don't seem to have "ClipboardEvent" in Chrome, so I guess their partial support doesn't include copy/paste events. – freeall Sep 01 '14 at 09:04
19

I know this answer is coming a bit late, but there is now a new modern alternative to ZeroClipboard (which is Flash based). Clipboard.js is a 2kB pure JavaScript alternative that has no dependencies.

Baraa
  • 1,341
  • 11
  • 8
8

I have created a pure JavaScript solution called clip-j. Here it is. Basically what it does is it takes advantage of document.execCommand('copy'); with a few other commands that make it so you don't see anything. Here's the code:

function clip(text) {   
    var copyElement = document.createElement('input');      
    copyElement.setAttribute('type', 'text');   
    copyElement.setAttribute('value', text);    
    copyElement = document.body.appendChild(copyElement);   
    copyElement.select();   
    document.execCommand('copy');   
    copyElement.remove();
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
Triforcey
  • 453
  • 5
  • 10
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Kenster Sep 07 '15 at 02:14
  • Thanks for the tip! I'll fix that. – Triforcey Sep 07 '15 at 02:15
  • this is only supported in latest browsers for more info see https://cbabhusal.wordpress.com/2015/11/15/ror-copy-some-arbitrary-text-into-users-clipboard/ – Shiva Nov 16 '15 at 05:15
  • 1
    Why this may be true, soon the latest browsers will be the medium. This is a simple solution that I will admit is ahead of its time, but as time goes on and more people update this becomes more useful. – Triforcey Nov 16 '15 at 05:25
  • If you use a `div` (instead of `input`) with the `contentEditable` attribute set to `true`, you can also copy to the clipboard as `text/html` and thus keep the formatting. See this answer: https://stackoverflow.com/a/30905277/434742 – Krisztián Balla Nov 07 '17 at 14:57
3

You can look at this blog post for an in-depth discussion of how to work with the clipboard in HTML5. Unfortunately, you still can't portably copy to the clipboard on click. However, for chrome and firefox you can create a browser extension that can give your site permission to access the clipboard, and I believe IE will let you copy to the clipboard, but will prompt the user to grant permission.

Update:

According to this: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand Firefox 41+, Chrome 42+, and IE 9+ support the copy command with execCommand. For firefox and chrome it will only work if triggered by a user action such as a click, and for IE it will give the user a warning dialog asking them for permission to copy to the clipboard.

Thayne
  • 6,619
  • 2
  • 42
  • 67
0

To use the execCommand, you must first select() something on the page, so you do not just copy whatever was last put into the clipboard. With this function, I pass the id of the input textbox into the function and select() it, then perform the copy command. No need to add listeners or further complicate your code. The document.execCommand() returns false if not enabled or supported, thus you can use the window.prompt as the backup method.

function copyToClipboard(id) {
    var blnCopied;
    document.getElementById(id).select();
    blnCopied = document.execCommand("copy", false, null);
    if (blnCopied)
        alert('Link copied to clipboard');
    else
        window.prompt ("Copy to clipboard: Ctrl+C, Enter", jQuery("#"+id).val());
}

Use a standard "a" anchor tag with an onclick="copyToClipboard('some_id')"

0

There are great answers to this question, and I chose to use this snippet:

function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
}

However, if there's bootstrap-select on your page, the $temp.val($(element).text()).select() line will throw an error:

Widget can only work on select elements

You can use .trigger('select') instead, as stated in the jQuery documentation for .select(), like this:

$temp.val($(element).val()).trigger('select');
Dmytro
  • 103
  • 4