W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2015

[clipops][editing] document.execCommand('copy', false, 'some data') ?

From: Hallvord Reiar Michaelsen Steen <hsteen@mozilla.com>
Date: Fri, 10 Apr 2015 13:44:40 +0200
Message-ID: <CAE3JC2ypkgqkaZJ0zMoj-9K8PecA=mA_QKPi-eXP6-smrCF7+A@mail.gmail.com>
To: WebApps WG <public-webapps@w3.org>
It's pretty obvious that the currently spec'ed code for "place 'foo' on the
clipboard when user clicks my <button>"  is not nice at all. Per  [1] the
code you need now is more or less

document.addEventListener('copy', function(e){
  e.clipboardData.setData('text/plain', 'foo');
  e.preventDefault(); // default behaviour is to copy any selected text
element.onclick = function(){
  document.execCommand('copy', false, ''); // only works in click handler
or other user-triggered thread

There is a very annoying disconnect between the code that knows what the
user clicked and the code that actually places content on the clipboard.
This requires rather poor hacks and workarounds like having global
variables (or variables in a closure both functions can access) for storing
data temporarily.

However, document.execCommand() is spec'ed as having a "value" argument.
What about actually using it here? Simplifying the above code to:

element.onclick = function(){
    document.execCommand('copy', false, 'foo');

If we wanted to get fancy, we could even allow

element.onclick = function(){
    document.execCommand('copy', false, {'text/plain': 'foo',


[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1151429#c2
Received on Friday, 10 April 2015 12:13:27 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 18:14:55 UTC